HandsOn Breaking Up Large Procedures

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

2. In the Code window, enter the code of the AboutUser procedure as shown below.

Sub AboutUser()

Dim fullName As String Dim firstName As String Dim lastName As String Dim space As Integer

' get input from user fullName = InputBox("Enter first and last name:")

' get first and last name strings space = InStr(fullName, " ")

firstName = Left(fullName, space - 1)

lastName = Right(fullName, Len(fullName) - space)

Part I

' display last name, first name MsgBox lastName & ", " & firstName End Sub

The AboutUser procedure can be divided into smaller tasks. The first task is obtaining the user's full name. The next task requires that you divide the user-supplied data into two strings: last name and first name. These tasks can be delegated to separate functions (for example: GetLast and GetFirst). The last task is displaying a message showing the reordered full name string. Now that you know what tasks you should focus on, let's see how you can accomplish each task.

3. Insert a new module and save it as MasterProcedures.

4. Enter the following AboutUserMaster procedure in the MasterProcedures module window:

Sub AboutUserMaster()

Dim first As String, last As String, full As String

Call GetUserName(full)

first = GetFirst(full) last = GetLast(full)

Call DisplayLastFirst(first, last) End Sub

The master procedure shown above controls the general flow of your program by calling appropriate subprocedures and functions. The master procedure begins with the declaration of variables. The first statement, Call GetUserName (full), calls the GetUserName subroutine (see step 5 below), and passes an argument to it — the contents of the full variable. Because the variable full is not assigned any value prior to the execution of the Call statement, it has the value of an empty string (" "). Notice that the name of the subprocedure is preceded by the Call statement. Although you are not required to use the Call keyword when calling a procedure, you must use it when the call to the procedure requires arguments. The argument list must be enclosed in parentheses.

5. In the MasterProcedures module, enter the following GetUserName subroutine:

Sub GetUserName(fullName As String)

fullName = InputBox("Enter first and last name:") End Sub

The GetUserName procedure demonstrates two very important Visual Basic programming concepts: how to pass arguments to a subprocedure and how to pass values back from a subprocedure to a calling procedure.

Introduction to Access 2003 VBA Programming

In the master procedure (see step 4), you called the GetUserName procedure and passed one argument to it — the variable full. This variable is received by a fullName parameter declared in the GetUserName subprocedure's Sub statement. Because the variable full contained an empty string, when Visual Basic called the GetUserName subprocedure, the fullName parameter received the same value — an empty string (" "). When Visual Basic displays the dialog box and gets the user's last name, the name is assigned to the fullName parameter. A value assigned to a parameter is passed back to the matching argument after the subprocedure is executed. Therefore, when Visual Basic returns to the master procedure, the full variable will contain user's last name.

Arguments passed to a subprocedure are received by parameters. Notice that the parameter name (fullName) is followed by the declaration of the data type (As String). Although the parameter's data type must agree with the data type of the matching argument, different names may be used for an argument and its corresponding parameter.

0 0

Post a comment