麻烦InputBoxes麻烦、InputBoxes

2023-09-08 10:58:00 作者:乱世一个你

我目前正与在MS Access VBA InputBoxes。我检查验证和处理用户如何与输入框进行交互,通过pressing确定或取消按钮。

I'm currently working with InputBoxes in MS Access VBA. I'm examining validation and handling how the user interacts with the InputBox through pressing the OK or Cancel buttons.

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

Correct me if I'm wrong but InputBoxes can return any data type and by default return a string? For example:

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

如果用户presses取消按钮,这将会运行得很好。

If the user presses the Cancel button this will run fine.

但是,当我换这对于像这样一个整数值:

But when I swap this for an integer value like so:

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

我收到类型不匹配:运行时错误'13'这是为什么?当我调试code和看什么被退回我发现 userInputValue 实际上是0,这就是我检查。那么,这个问题实际上是在输入框实际上是返回一个字符串?

I receive a Type Mismatch: Runtime Error '13' Why is this? When I debug the code and look at what is being returned I find that the userInputValue is actually 0, which is what I'm checking for. So is the problem actually that the InputBox is actually returning a string?

感谢

推荐答案

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

When in doubt, check the inbuilt VBA help ;)

输入框()返回一个字符串

您可以尝试一下本作整数

You can try this for 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
 
精彩推荐
图片推荐