2016-07-28 20 views
0

我正在使用Access数据库,并且需要要求ScrapAmount1在将数据放入ScrapCodes1中时有答案。我想要弹出一个错误消息,提醒他们使废品数量高于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. :) 

回答

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 
+0

Hi @Random_answer_guy!谢谢您的回答!这很好!我使用这个代码,它完美的工作!非常感谢! '私人小组Form_BeforeUpdate(取消作为整数) 如果Me.ScrapAmount1 = 0然后 取消=真 MSGBOX“如果选择废料码,然后废料量必须有一个值。”,vbExclamation,“数据所需” 完如果 End Sub' – Flammie

+0

是的,它不是条件的,因为默认值,它永远不会为空。但是,删除默认值可能会更好(因为0实际上是一个值),然后将检查更改为“If IsNull(Me.ScrapAmount)Then”,然后提示输入正确的值。 –

+0

只是抬起头来,如果他删除了默认的0并且没有输入新的数字(返回null),它将会抛出一个错误。您应该在文本框的代码(强制用户输入数字,或将空值变为0)或读取值(再次说明空值的可能性)的某个位置考虑这种可能性。 – CyberClaw

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 
+0

嗨Cyber​​Claw。谢谢你的回答!这是有道理的,我的默认“0”会使该字段不为空。我试过你的代码,它给了我一个错误,说**编译错误:必须是第一条语句在行**和代码行'Else If Me.ScrapAmount1 <= 0 Then'点亮红色。我尝试了没有Else,它仍然给我一个编译错误。 – Flammie

+0

对不起,我的坏。在VBA中,它是'elseif'(一个词)和'end if'(单词)。我倾向于混合起来。记住'elseif'的更好方法是一起的,因为它只用于'if',所以它只是一个命令。另一方面,'end'用于很多事情,所以'end'如果是单独的,因为命令'end'可以用于'if','sub'等。 – CyberClaw

相关问题