HandsOn Using the Input Box Function

1. In the Visual Basic Editor window, choose Insert | Module to add a new module.

2. In the Code window, type the Informant subroutine shown below.

Sub Informant()

InputBox prompt:="Enter your place of birth:" & Chr(13) _ & " (e.g., Boston, Great Falls, etc.) " End Sub

The above procedure displays a dialog box with two buttons. The input prompt is displayed on two lines (see Figure 4-6). Similar to using the MsgBox function, if you plan on using the data entered by the user in the dialog box, you should store the result of the InputBox function in a variable.

Figure 4-6: A dialog box generated by the Informant subroutine.

3. The Informant2 procedure shown below assigns the result of the InputBox function to the town variable.

Sub Informant2()

Dim myPrompt As String Dim town As String

Const myTitle = "Enter data"

myPrompt = "Enter your place of birth:" & Chr(13) _ & "(e.g., Boston, Great Falls, etc.)" town = InputBox(myPrompt, myTitle)

MsgBox "You were born in " & town & ".", , "Your response" End Sub

Notice that this time, the arguments of the InputBox function are listed in parentheses. Parentheses are required if you want to use the result of the InputBox function later in your procedure. The Informant2 subroutine uses a constant to specify the text to appear in the title bar of the dialog box. Because the constant value remains the same throughout the execution of your procedure, you can declare the input box title as a constant.

However, if you'd rather use a variable, you still can. When you run a procedure using the InputBox function, the dialog box generated by this function always appears in the same area of the screen. To change the location of the dialog box, you must supply the xpos and ypos arguments, explained earlier.

Microsoft Office Access

Enter your place of birth:


(e.g. Boston, Great Falls, etc.)

Canul |

1' 1

Introduction to Access 2003 VBA Programming

4. To display the dialog box in the top left-hand corner of the screen, modify the InputBox function in the Informant2 procedure, as follows.

town = InputBox(myPrompt, myTitle, , 1, 200)

Notice that the argument myTitle is followed by two commas. The second comma marks the position of the omitted default argument. The next two arguments determine the horizontal and vertical position of the dialog box. If you omit the second comma after the myTitle argument, Visual Basic will use the number 1 as the value of the default argument. If you precede the values of arguments by their names (for example, prompt:=myPrompt, title:=myTitle, xpos:=1, ypos:=200), you won't have to remember to place a comma in the place of each omitted argument.

What will happen if, instead of the name of town, you enter a number? Because users often supply incorrect data in the Input dialog box, your procedure must verify that the data the user entered can be used in further data manipulations. The InputBox function itself does not provide a facility for data validation. To validate user input, you must use other VBA instructions that are presented in the next chapter.

0 0

Post a comment