Access中的VBA。如果已经回答了另一个字段,如何要求字段

时间:2016-07-28 16:17:50

标签: vba ms-access

我正在使用Access数据库,如果他们已将数据放入ScrapCodes1,则需要要求ScrapAmount1有答案。我想要弹出一条错误消息,提醒他们将废品量高于0.我正在使用VBA,这就是我所拥有的不起作用。 PS。 ScrapCodes1是他们选择的下拉列表,默认情况下ScrapAmount为0。

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

End Sub

Thank you for your answers in advance. :)

2 个答案:

答案 0 :(得分:0)

您是否使用Nz检查ScrapAmount1是否为空。 ScrapAmount1默认为0,因此它不为空。

我自己有第二个If条件:

'check for null
If Nz(Me.ScrapAmount1, "") = "" Then
      Cancel = True
      MsgBox "If Scrap Code is selected, then Scrap Amount must have a value.", vbExclamation, "Data Required"
'checks for 0 and negative numbers
ElseIf Me.ScrapAmount1 <= 0 then
    Cancel = True
    MsgBox "Scrap Amount must be higher than 0"
End If

If Cancel then
    'do cancel stuff, I have no idea what you use this var for
end if

答案 1 :(得分:0)

我认为你不需要任何东西:

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