【问题标题】:IF statement not working in VBAIF 语句在 VBA 中不起作用
【发布时间】:2020-07-13 23:33:45
【问题描述】:

我正在自学 VBA 并尝试编写一个小程序来执行以下操作

1.要求用户在文本框中输入 10 到 20 之间的数字
2.当一个按钮被点击时,代码会检查文本框中输入的数字。如果它在10到20之间,则会显示一条消息。如果输入的数字不在 10 到 20 之间,则将邀请用户尝试增益,并且在文本框中输入的任何内容都将被删除。

Private Sub Command0_Click()
Me.Text3.SetFocus
inumber = Val(Text3.Text)
If inumber >= 10 & inumber <= 20 Then
MsgBox ("The number you entered is: ") & inumber
Else
Text3.Text = ""
MsgBox ("Please try again")
End If

End Sub

但是,我认为我的代码的 else 部分不起作用。如果我输入 5,它将显示 5 而不是消息框。如果我遗漏了什么,谁能告诉我。

提前致谢。节日快乐。

【问题讨论】:

  • 如果你在某处定义Form_customer_test ,那么If应该是If Form_customer_test &gt;= 10 And inumber &lt;= 20 Then。但我认为你的意思是If inumber &gt;= 10 And inumber &lt;= 20 Then

标签: ms-access if-statement vba


【解决方案1】:

试试下面。 &amp; 用于 VB 中的字符串连接。 And应该在VB中使用

Private Sub Command0_Click()
    Me.Text3.SetFocus
    inumber = Val(Text3.Text)
    If inumber >= 10 And inumber <= 20 Then
        MsgBox ("The number you entered is: ") & inumber
    Else
        Text3.Text = ""
        MsgBox ("Please try again")
    End If
End Sub

【讨论】:

  • 非常感谢!就是这样!谢谢,现在可以了。 :)
【解决方案2】:
If Form_customer_test >= 10 & inumber <= 20 Then

应该是:

If inumber >= 10 & inumber <= 20 Then

【讨论】:

  • 感谢您指出。对不起,这是一个错字,我实际上将它作为 innumber 而不是 form_customer_test。我不知道那是怎么进去的。谢谢
【解决方案3】:

确认:对逻辑运算符使用“与”或“或”

【讨论】:

    猜你喜欢
    • 2018-05-11
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多