【问题标题】:Trouble with InputBoxes输入框的问题
【发布时间】:2013-04-10 07:19:34
【问题描述】:

我目前正在使用 MS Access VBA 中的 InputBox。我正在检查验证并处理用户如何通过按下 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 实际上是返回一个字符串的问题吗?

【问题讨论】:

    标签: ms-access vba ms-access-2007


    【解决方案1】:

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

    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
    

    【讨论】:

    • @HansUp:StrPtr(类似于 VarPtr、VarPtrArray、VarPtrStringArray 和 ObjPtr)是一个未公开的函数,用于获取变量 vb.mvps.org/tips/varptr.asp 的底层内存地址
    • 当然,它是核心语言的一部分,用于检测从未分配给的字符串
    • + 1 另一种好方法 :) offtopic:想知道“是”在您的个人资料中意味着什么:P
    • @SiddharthRout 我想我只是添加它以获得“填写您的个​​人资料”青铜徽章:)
    • lol@Alex:对于未来的访问者:VarPtr、StrPtr 和 ObjPtr 在 Off2007 和 Off2010 的 VBA 中仍然存在。对于 64 位版本的 Office 2010 中的 64 位指针,您可以在 msdn 中找到文档:msdn.microsoft.com/en-us/library/ee691831.aspx(Sorry Alex for hijacking this comment)
    【解决方案2】:

    如有疑问,请查看内置的 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
    

    【讨论】:

      【解决方案3】:

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

      在立即窗口中试试这个。

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

      对于代码中的测试,检查 userInputValue 的数值等值是否等于 0。

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

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

      【讨论】:

      • 似乎我们在几分之一秒内提交了我们的答案,但我认为你的答案是第一个,所以删除了我的。但后来我决定我们采取稍微不同的方法,所以也许两者都足以生存。 :-)
      【解决方案4】:

      注意:以下内容仅适用于 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
      

      【讨论】:

      • "False" 不会(从不)返回,只有当您键入它时。按取消返回一个空字符串。
      • @Gustav 对不起,你错了。至少在你投反对票之前先尝试一下。你会发现我是对的。这是 Application.Inputbox,如果变量被声明为字符串,它就会以这种方式工作。如果变量声明为 Variant,它的工作方式会有所不同。
      • Access 中没有 Application.InputBox 所以我假设你的意思是 VBA.InputBox ...
      • 好吧,我的错,它在 Excel 中,但由于某种原因它不在 Access 中。
      • @moderator Excel 的同一问题被标记为重复并重定向到此问题,但 Access 和 Excel 的答案不同。
      【解决方案5】:

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

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

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

      例程需要以 3 种方式之一响应: 1. 如果按下“取消”按钮,程序结束。 2. 如果按下“确定”按钮, IF ALLOW_EMPTY_STRING THEN 给出一条消息并重新开始输入 别的 返回一个空字符串 万一 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
      

      结束函数'Input_String`

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多