输入框问题 [英] Trouble with InputBoxes
问题描述
我目前正在使用 MS Access VBA 中的 InputBoxes.我正在检查验证和处理用户如何通过按下 OK 或 Cancel 按钮与 InputBox 交互.
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
如果用户按下取消按钮,这将运行良好.
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
我收到一个 Type Mismatch: Runtime Error '13'
这是为什么?当我调试代码并查看返回的内容时,我发现 userInputValue
实际上是 0,这就是我要检查的内容.那么问题是InputBox实际上是返回一个字符串吗?
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 that the InputBox is actually returning a string?
推荐答案
如有疑问,请查看内置的 VBA 帮助 ;)
When in doubt, check the inbuilt VBA help ;)
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
这篇关于输入框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!