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

Microsoft Access VBA using a variable field in a calculation

我有一个表格,其中包含名为 QOH 的现有库存字段。我有 30 个字段,每个字段都包含每日需求,每天都有一个字段。这些字段是 D1 到 D30。我想要做的是创建一个循环,从 QOH 中扣除需求(D1 到 D30),直到 QOH 低于零。我正在计算天数,因为它循环 y = y + 1 以得出覆盖天数。然后我将 return 计算的结果放入表格的一个字段中

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