HandsOn Executing a Function Procedure from a Subroutine

This hands-on requires prior completion of Hands-On 4-1.

1. In the same module where you entered the JoinText function procedure, enter the following EnterText subroutine:

Sub EnterText()

Dim strFirst As String, strLast As String, strFull As String strFirst = InputBox("Enter your first name:") strLast = InputBox("Enter your last name:") strFull = JoinText(strFirst, strLast)

MsgBox strFull End Sub

2. Place the cursor anywhere inside the code of the EnterText procedure and press F5 to run it.

Line Feed Carriage Return

Chr(10)

Chr(13)

Part I

As Visual Basic executes the statements of the EnterText procedure, it collects the data from the user and stores the values of the first and last names in the variables strFirst and strLast. Then these values are passed to the JoinText function. Visual Basic substitutes the variables' contents for the arguments of the JoinText function and assigns the result to the name of the function (JoinText). When Visual Basic returns to the EnterText procedure it stores the function's value in the strFull variable. The MsgBox function then displays the contents of the strFull variable in a message box. The result is the full name of the user (first and last name separated by a space).

0 0

Post a comment