INPUT BOXES

INPUT BOXES

The InputBox provides a convenient user-interface for obtaining a single value from the user. A simplified version of its syntax is:

v = InputBox(prompt [, title] [, defaultString])

where v is a variable to which the input value entered by the user is assigned, prompt is a string displayed as the message in the dialog box, title is an optional string displayed in the title bar of the dialog box (if it is omitted, the default is the application name), and defaultString is a string displayed in the text box as the default response. If it is omitted, the text box is displayed empty.

The simpliest case is to dispay amessage with no frills:

UserName = InputBox("Please enter your username")

A dialog box with an empty display and two buttons: OK and Cancel. WHen the user enters a name and clicks OK, the name will be returned to your program and assigned to the variable UserName.

A more complex example is:

Msg = "Please enter the initial velocity"
velocity - InputBox(Msg, "Velocity (m/s)", 0)

This will result in a dialog box having a suitable title and a 0 displayed as the default value in the text box.

However one has to be careful about data typing when getting input from the user. Consider the following example"

Option Explicit
Sub test()
Dim v as Double
Dim dataType as Integer
Dim msg as String
v = InputBox("Units: /m/s", "Enter velocity: ")
dataType = VarType(v)
Select Case dataType
    Case Is = 2
        msg = "Enter value has data type: Integer"
    Case Is = 3
        msg = "Enter value has data type: Long"
    Case Is = 4
        msg = "Enter value has data type: Single"
    Case Is = 5
        msg = "Enter value has data type: Double"
    Case Is = 8
        msg = "Enter value has data type: String"
    Case Is = 11
        msg = "Enter value has data type: Boolean"
End Select
MsgBox msg
MsgBox "The value of v entered is: " & v
End Sub

When the program is run, and the user enters a number at the InputBox, there is no problem. The program accepts correctly the number, and its data type as Double as expected.

However if the same program is run and the user clicks the Cancel button when the message box appears, then the program stops executing due to a run-time error. The error message reports a "Type mismatch" problem. The type of data brought back from when the Cancel button is clicked turns out to be an empty String, thus it is incompatible with the data type Double declared for variable v.

This can be seen by removing the Option Explicit and the Dim statements and re-running the program.

You can also use the F8 key to step through the program until after the Cancel button is clicked.

You can place the cursor over the variable v in the program to find that v is an empty String.

A number of remedies can be devised. One solution is to use the Val function to return the numbers contained in s String as a numeric value of the appropriate type. The Val function converts a null string to its numerical equivalent, zero, and the program will not encounter a run-time error.

Nevertheless we still have a problem: we do not know if a user really intended to set the velocity to 0, or rather wanted to hit the Cancel button.

Another way is to try the following:

Option Explicit
Sub test2()
Dim vtemp as Variant, v as Double
vtemp = InputBox("Units: /m/s", "Enter velocity: ")
dataType = VarType(v)
If vtemp = "" Then
    End
Else
    v = Val(vtemp)
End If
MsgBox "The value of v entered is: " & v
End Sub