7

我目前正在使用 MS Access VBA 中的 InputBoxes。我正在检查验证并处理用户如何通过按 OK 或 Cancel 按钮与 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

如果用户按下取消按钮,这将运行良好。

但是当我把它换成这样的整数值时:

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

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

4

5 回答 5

23

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

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
于 2013-04-17T15:19:26.407 回答
7

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

InputBox()返回一个字符串

你可以试试这个整数

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
于 2013-04-17T13:43:19.313 回答
4

InputBox无论用户输入的数字如何,都返回一个字符串。如果他们单击取消,它会返回一个空字符串。

在立即窗口中尝试此操作。

? TypeName(InputBox("Please enter a #", "Determine Limit", 10000))
String

对于代码中的测试,请检查 的数值等价物是否userInputValue等于零。

If Val(userInputValue) = 0 Then
    MsgBox ("You pressed the cancel button...")
End If

请注意,InputBox您无法区分用户是单击了取消还是删除了起始值 (10000) 并单击了确定。无论哪种方式,InputBox都返回一个空字符串 ("")。并且Val("")还返回零。如果这将是一个问题,请替换自定义表单来收集用户输入......这不像InputBox.

于 2013-04-17T13:43:19.400 回答
2

注意:以下内容仅适用于 Excel。Application.InputBox 函数在 Access 中不可用:

如果用户单击取消,Application.InputBox将返回“False” 。

Dim userInputString As String
Dim userInputValue As Integer
'Text to display, Title, Default Value
userInputString = Application.InputBox("Please enter a #", "Determine Limit", 10000)
If userInputString = "False" Then
    MsgBox ("You pressed the cancel button...")
Else
    userInputValue = CInt(Trim(userInputString))
End If
于 2016-11-06T11:42:51.723 回答
0

与其从 InputString 取回一个数字,不如将默认字符串设置为“_______________”。然后我可以测试是否输入了任何内容,并且“取消”键将返回一个空字符串。

我还添加了一个测试值“Allow_Empty_String”(真/假),因为有时我想要一个空字符串。

必须将“Flag_Msg_Err”重置为 False,因为“Msg_Err”将其设置为 true 以捕获来自循环的调用。

例程需要以 3 种方式之一响应: 1. 如果按下“取消”按钮,则程序结束。2. 如果按下“OK”按钮,IF ALLOW_EMPTY_STRING THEN 会给出一条消息并重新开始输入 ELSE 返回一个空字符串 END IF 3. 该字符串已输入,然后返回 TRIM(STRING)。

代码:

`Function Input_String(Prog, Message, Optional Allow_Empty_String = False) ' 返回一个字符串或单词“Cancal”。' 2/28/19 创建。WML

If Trace Then Prog = Prog & "(*)"
Call Name_Put("Flag_Msg_Err", False)

Do
    Test_String = "_____________"
    Input_String = InputBox(Prompt:=Message, _
                            Default:=Test_String, Title:=Prog)

    Select Case Input_String

        Case "TRACE"
            ' for Debugging
            Old_Trace = Named("Flag_Trace")
            New_Trace = Not Old_Trace
            Call Name_Put("Flag_Trace", New_Trace)
            Msg = "TRACE is now set to " & New_Trace & "."
            Call Name_Put("Flag_Msg_Err", False)

        Case Test_String
            If Allow_Empty_String Then
                Msg = "You must enter something,|" & _
                      " or select CANCEL."
                Call Msg_Err(Prog, Msg, , True)
                Call Name_Put("Flag_Msg_Err", False)
            Else
                Input_String = ""
                Finished = True
            End If

        Case ""
            If Trace Then
               Stop: Exit Function
            Else
               End
            End

        Case Else
            ' If entered a space or clicked "Ok".
            Input_String = Trim(Input_String)
            Finished = True

    End Select

 Loop

结束函数“输入字符串”

于 2019-02-27T18:22:50.810 回答