输入框的麻烦

Kat*_*a24 6 ms-access vba ms-access-2007 access-vba

我目前正在使用MS Access VBA中的InputBoxes.我正在检查验证和处理用户如何通过按OK或取消按钮与InputBox交互.

纠正我,如果我错了,但InputBoxes可以返回任何数据类型,默认情况下返回一个字符串?例如:

Dim userInputValue As String

'Text to display, Title, Default Value
userInputValue = InputBox("Please enter a #", "Determine Limit", 10000)

If userInputValue = "" Then
    MsgBox ("You pressed the cancel button...")
End If
Run Code Online (Sandbox Code Playgroud)

如果用户按下取消按钮,则运行正常.

但当我交换这个整数值时,如下所示:

Dim userInputValue As Integer
'Text to display, Title, Default Value
userInputValue = InputBox("Please enter a #", "Determine Limit", 10000)

If userInputValue = 0 Then
    MsgBox ("You pressed the cancel button...")
End If
Run Code Online (Sandbox Code Playgroud)

我收到一个Type Mismatch: Runtime Error '13'为什么这个?当我调试代码并查看返回的内容时,我发现userInputValue实际上是0,这正是我正在检查的内容.那么InputBox实际上返回一个字符串的问题是什么?

Ale*_* K. 17

这是一种捕捉与对话交互的大多数结果的方法;

Dim value As String
value = InputBox("Please enter a #", "Determine Limit", 10000)

If (StrPtr(value) = 0) Then
    MsgBox "You pressed cancel or [X]"

ElseIf (value = "") Then
    MsgBox "You did not enter anything"

ElseIf (Val(value) = 0 And value <> "0") Then
    MsgBox "Invalid number"

Else
    MsgBox "You entered " & value

End If
Run Code Online (Sandbox Code Playgroud)

  • @HansUp:StrPtr(如 VarPtr、VarPtrArray、VarPtrStringArray 和 ObjPtr)是一个未公开的函数,用于获取变量的底层内存地址 http://vb.mvps.org/tips/varptr.asp (2认同)

Sid*_*out 7

如有疑问,请检查内置的VBA帮助;)

InputBox() 返回一个String

您可以尝试使用Integers

Sub Sample()
    Dim Ret As String, userInputValue As Integer

    'Text to display, Title, Default Value
    Ret = InputBox("Please enter a #", "Determine Limit", 10000)

    If Ret = "" Then
        MsgBox ("You pressed the cancel button... or you pressed OK without entering anything")
    Else
        If IsNumeric(Ret) Then
            userInputValue = Val(Ret)
        Else
            MsgBox ("Incorrect Value")
        End If
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)