2013-04-17 61 views
4

我目前正在使用MS Access VBA中的InputBoxes。我正在检查验证并处理用户如何通过按下“确定”或“取消”按钮与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实际上返回一个字符串的问题呢?

回答

5

有疑问时,请检查内置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 
+0

这是确定汉斯:) –

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),并点击OK。无论哪种方式,InputBox都会返回一个空字符串(“”)。而且Val("")也返回零。如果这将是一个问题,请使用自定义表单来收集用户输入...,但不限于InputBox

+0

+ 1我想知道为什么你删除你的答案....反正:) –

+1

看来,我们一秒钟的时间内提交了答案,但我想你在第一次拿到,所以删除了我的。但后来我决定采取稍微不同的方法,所以也许两者都足够用来生存。 :-) – HansUp

12

这里有一种方法可以捕捉与对话交互的大部分结果;

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 
+0

'StrPtr()'在VBA中? – HansUp

+1

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

+0

当然,它的核心语言的一部分,用于检测从未分配到的字符串 –

1

注意:以下内容仅适用于Excel。该Application.InputBox功能不可用在Access:

Application.InputBox回报“假”如果用户单击取消。

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 
+1

“假”不是(从不)返回,只有当你输入它。按取消将返回一个空字符串。 – Gustav

+1

@Gustav对不起,但你错了。至少在你投降前尝试一些东西。你会发现我是对的。这是** Application.Inputbox **,如果该变量被声明为一个字符串,它就会这样工作。如果变量声明为Variant,它的工作方式会有所不同。 – swmcdonnell

+0

在Access中没有_Application.InputBox_,所以我认为你的意思是_VBA.InputBox_ ... – Gustav