HandsOn Converting Data Types

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

2. In the Code window, enter the AddTwoNums procedure as shown below.

Sub AddTwoNums()

Dim myPrompt As String Dim value1 As String Dim mySum As Single

Const myTitle = "Enter data"

myPrompt = "Enter a number:"

value1 = InputBox(myPrompt, myTitle, 0)

MsgBox mySum & " (" & value1 & " + 2)" End Sub

3. Place the cursor anywhere inside the code of the AddTwoNums procedure and press F5 to run it.

Part I

The above procedure displays the dialog box shown in Figure 4-7. Notice that this dialog box has two special features that are obtained by using the InputBox function's optional arguments: title and default value. Instead of the default title, "Microsoft Access," the dialog box displays a text string as defined by the contents of the myTitle constant. The zero (0) entered as the default value in the edit box suggests that the user enter a number instead of text. Once the user provides the data and clicks OK, the input is assigned to the variable valuel.

Figure 4-7: To suggest that the user enter a specific type of data, you may want to provide a default value in the edit box.

valuel = InputBox(myPrompt, myTitle, 0)

Enter data

1*1

Enter a number:

QK

CancĀ«l |

i

i i

Figure 4-7: To suggest that the user enter a specific type of data, you may want to provide a default value in the edit box.

Was this article helpful?

0 0

Post a comment