7

現在、MS Access VBA で InputBoxes を使用しています。ユーザーが [OK] または [キャンセル] ボタンを押して 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) を削除して [OK] をクリックしたのかを区別できないことに注意してください。どちらの場合も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 に答える