调试Access 2013中的if语句

时间:2016-08-01 17:07:00

标签: ms-access access-vba

我有一段代码可以确保用户输入"废品金额"如果他们输入了#"废料代码"。当我申请并使用Scrap1的代码时,一切都很好。当我尝试将相同的代码应用于下一组变量名称时,我收到一条错误,指出我的代码"检测到一个模糊的名称。我检查了我的变量名和代码,直到我的眼睛流血。一切看起来都很好。有没有人看到我错过的错误?

'check to see that there is a scrap amount if a code has been entered #1.

Private Sub Form_BeforeUpdate(Cancel As Integer)
     If Me.ScrapCodes1.Value Then
     If Me.ScrapAmount1 = 0 Then
          Cancel = True
          MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
     End If
     End If
End Sub


'check to see that there is a scrap amount if a code has been entered #2.
Private Sub Form_BeforeUpdate(Cancel As Integer)
     If Me.ScrapCodes2.Value Then
     If Me.ScrapAmount2 = 0 Then
          Cancel = True
          MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
     End If
     End If
End Sub

1 个答案:

答案 0 :(得分:1)

是的,汇集在一个子目录中:

Private Sub Form_BeforeUpdate(Cancel As Integer)

    ' check to see that there is a scrap amount if a code has been entered #1.
    If Me.ScrapCodes1.Value Then
        If Me.ScrapAmount1 = 0 Then
            Cancel = True
        End If
    End If

    ' check to see that there is a scrap amount if a code has been entered #2.
    If Me.ScrapCodes2.Value Then
        If Me.ScrapAmount2 = 0 Then
            Cancel = True
        End If
    End If

    If Cancel = True Then
        MsgBox "If Scrap Code is selected, then Scrap Amount must have a value."
    end If

End Sub