HandsOn Using the Msg Box Function with Arguments

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

2. In the Code window, enter the MsgYesNo subroutine as shown below.

Sub MsgYesNo()

Dim question As String Dim myButtons As Integer question = "Do you want to open a new report?" myButtons = vbYesNo + vbQuestion + vbDefaultButton2 MsgBox question, myButtons End Sub

In the above subroutine, the question variable stores the text of your message. The settings for the buttons argument are placed in the myButtons variable. Instead of using the names of constants, you can use their values, as in following:

However, by specifying the names of the buttons argument constants, you make your procedure easier to understand for yourself and others who may work with this procedure in the future.

The question and myButtons variables are used as arguments for the MsgBox function. When you run the procedure, you see the result displayed as shown in Figure 4-5. Notice that the No button is now selected. It's the default button for this dialog box. If you press Enter, Visual Basic removes the message box from the screen. Nothing happens because your procedure does not have more instructions following the MsgBox function. To change the default button, use the vbDefaultButton1 setting instead.

Introduction to Access 2003 VBA Programming

The third argument of the MsgBox function is title. While this is also an optional argument, it's very handy because it allows you to create procedures that don't provide visual clues to the fact that you programmed them with Microsoft Access. Using this argument, you can set the title bar of your message box to any text you want.

Suppose you want the MsgYesNo procedure to display the text "New report" in its title. The following MsgYesNo2 procedure demonstrates the use of the title argument.

Sub MsgYesNo2()

Dim question As String Dim myButtons As Integer Dim myTitle As String question = "Do you want to open a new report?" myButtons = vbYesNo + vbQuestion + vbDefaultButton2 myTitle = "New report" MsgBox question, myButtons, myTitle

End Sub

The text for the title argument is stored in the myTitle variable. If you don't specify the value for the title argument, Visual Basic displays the default text "Microsoft Access." Notice that the arguments are listed in the order determined by the MsgBox function.

If you would like to list the arguments in any order, you must precede the value of each argument with its name, as shown below:

MsgBox title:=myTitle, prompt:=question, buttons:=myButtons

The last two arguments, helpfile and context, are used by more advanced programmers who are experienced with using help files in the Windows environment. The helpfile argument indicates the name of a special help file that contains additional information you may want to display to your VBA procedure user. When you specify this argument, the Help button will be added to your message box. When you use the helpfile argument, you must also use the context argument. This argument indicates which help subject in the specified help file you want to display. Suppose HelpX.hlp is the help file you created and 55 is the context topic you want to use. To include this information in your MsgBox function, you would use the following instruction:

MsgBox title:=myTitle, prompt:=question _ buttons:=myButtons _ helpfile:= "HelpX.hlp", context:=55

The above is a single VBA statement broken down into several lines with the line continuation character.

Part I

Returning Values from the MsgBox Function

When you display a simple message box dialog with one button, clicking the OK button or pressing the Enter key removes the message box from the screen. However, when the message box has more than one button, your procedure should detect which button was pressed. To do this, you must save the result of the message box in a variable. Table 4-2 shows values that the MsgBox function returns.

Table 4-2: Values returned by the MsgBox function

Button Selected
























The MsgYesNo3 procedure is a revised version of MsgYesNo2. It shows how you can find out which button the user chose.

0 0

Post a comment