Microsoft Access VBA在计算中使用变量字段

问题描述

我有一个表单,其中包含一个名为QOH的现有库存字段。我有30个字段,每个字段包含一个日常需求,每天有一个字段。字段是D1到D30。我想做的是创建一个循环,从QOH中扣除需求(D1至D30),直到QOH降至零以下。我正在计算天数,因为它会循环y = y + 1来得出覆盖天数。然后,我将计数结果返回到表单上的字段

Private Sub PDS_AfterUpdate()

    Dim w As Integer 'generate the appropriate field reference'
    Dim y As Integer 'My counter (The Value) that I return to the form'
    Dim z As Double 'Quantity On Hand QOH'
    Dim a As String
    Dim strFieldName As String


    z = Me.P0 ' PO is the field that contains the QOH - Quantity On Hand

    If z < 0 Then ' If the QOH is already below zero return a -1
    y = -1
    End If

    If z >= 0 Then

    Do Until z < 0
    w = w + 1 'This is used to get the number to add to the field to get the correct field'
    a = "D" & w ' I then add the number to the field which starts with D to get the field that I want 
    to pull the value from'
    strFieldName = "me." & a
    z = z - strFieldName '<--- This is where I am stuck how do I get the value from the field I am 
    referencing in order to subtract it from the QOH'
    y = y + 1 'This is the counter that I return to the field in my form'

    Loop

    End If

    Me.DOH = y ' the field that the result is passed to on the form'

End Sub

解决方法

尝试一下:

Do Until z <= 0
    w = w + 1 
    a = "D" & CStr(w)
    z = z - Me(a).Value
    y = y + 1 
Loop