Microsoft Access Database Development With VBA

Message Boxes

   

Introduction

A message box is a special dialog box used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything on the message box. There are usually two kinds of message boxes you will create: one that simply displays information and one that expects the user to make a decision.

A message box is created using the MsgBox function. Its syntax is:

MsgBox([Message] [Buttons] [Title] [HelpFile] [Context])

The MsgBox function takes five arguments and only the first one is required: the Message.

The Message of a Message Box

The Message argument is the string that the user will see displaying on the message box. As a string, you can display it in double quotes, like this "That's All Folks". Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your credentials have been checked."
End Sub

This would produce:

Message Box

You can also create it from other pieces of strings. The Message argument can be made of up to 1024 characters. To display the Message on multiple lines, you can use either the constant vbCrLf or the combination Chr(10) & Chr(13) between any two strings. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked." & _
            vbCrLf & "To complete your application, please " & _
            "fill out the following survey"
End Sub

This would produce:

Message Box

The Buttons on a Message Box

The Buttons argument specifies what button(s) should display on the message box. There are different kinds of buttons available and Visual Basic recognizes them by a numeric value assigned to each. The Buttons argument is a value of the VbMsgBoxStyle enumeration. It can be one of the following constants:

VbMsgBoxStyle Member Constant Value Button(s) Displayed
vbOKOnly 0 OK
vbOKCancel 1 OK Cancel
vbAbortRetryIgnore 2 Abort Retry Message Box Button: Ignore
vbYesNoCancel 3 Yes Message Box Button: No Cancel
vbYesNo 4 Yes Message Box Button: No
vbRetryCancel 5 Retry Cancel

Here is an example that displays the Yes and the No buttons on the message box:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           VbMsgBoxStyle.vbYesNo
End Sub

This would produce:

Message Box

You can use the name of the member of the VbMsgBoxStyle enumeration directly, that is, without qualifying it. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", vbYesNo
End Sub

Or you can use the constant value of the member of the enumeration if you know it. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", 4
End Sub

These three formats would produce the same result.

The Icons on a Message Box

Besides the buttons, to enhance your message box, you can display an icon in the left section of the message box. To display an icon, you can use a member of the VbMsgBoxStyle. The available members for the icons are:

VbMsgBoxStyle Member Integer Value Description
vbCritical 16
vbQuestion 32 Question
vbExclamation 48 Exclamation
vbInformation  64 Information

To use one of these icons, you have two options. You can combine its VbMsgBoxStyle button with the VbMsgBoxStyle icon member. To perform this combination, you use the Or operator. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           VbMsgBoxStyle.vbYesNo Or VbMsgBoxStyle.vbQuestion
End Sub

This would produce:

Message Box

Once again, you can use the name of the member of the VbMsgBoxStyle enumeration without qualifying it. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           vbYesNo Or vbQuestion
End Sub

The second alternative it to use the integral constants instead of the members of the  enumeration. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           4 Or 32
End Sub

Alternatively, you can add (using the arithmetic addition) the integral value of the button to the integral value of the icon. For example, the integral value of the Yes/No button is 4 and the integral value of the question icon is 32. If you add both, you get 4 + 32 = 36. Therefore, if you use 36 for the second argument, you would get the question icon, the Yes, and the No button. This would be done as follows:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", 36
End Sub

The Default Button of a Message Box

If you create a message box with more than one button, the most left button usually has a thick border, indicating that it is the default. If the user presses Enter after viewing the button, the effect would be the same as if he had clicked the default button. If you want, you can designate another button as the default. To do this, you can use one more member of the a member of the VbMsgBoxStyle enumeration. The available members are:

VbMsgBoxStyle Member Integral Constant If the message box contains more than one button, the default would be
vbDefaultButton1  0 The first button
vbDefaultButton2  256 The second button
vbDefaultButton3  512 The third button
vbDefaultButton4  768 The fourth button

Once again, to specify a default value, use the Or operator to combine a VbMsgBoxStyle Member with any other combination. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           VbMsgBoxStyle.vbYesNoCancel Or VbMsgBoxStyle.vbQuestion _
           Or VbMsgBoxStyle.vbDefaultButton2
End Sub

This would produce:

Message Box

These additional buttons can be used to further control what the user can do:

Constant  Value Effect
vbApplicationModal 0 The user must dismiss the message box before proceeding with the current database
vbSystemModal 4096 The user must dismiss this message before using any other open application of the computer

Once again, you can use the name of the member of the VbMsgBoxStyle enumeration directly without qualifying it. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           vbYesNoCancel Or vbQuestion Or vbDefaultButton2
End Sub

Also, remember that you can use the constant integer of a member. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           3 Or 32 Or 256
End Sub

You can also arithmetically add the constant values of the desired members.

 
 
 

The Title or Caption of a Message Box

The Title argument is the caption that would display on the title bar of the message box. It is a string whose word or words you can enclose between parentheses or that you can get from a created string. The Title argument is optional. As you have seen so far, if you omit, the message box is equipped with the "Microsoft Office Access" string as its default value. Otherwise, if you want a custom title, you can provide it as the third argument to the MsgBox() function. The caption can be a simple string. Here is an example:

Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           vbYesNoCancel Or vbQuestion Or vbDefaultButton2, _
           "Crofton Circle of Friends - Membership Application"
End Sub

This would produce:

Message Box

Notice that the caption is now customized instead of the routine "Microsoft Office Access". The caption can also be a string created from an expression or emanating from a variable or value.

Help for a Message Box

If your application is using a help file, you can specify this and let the message box use it. The HelpFile argument is a string that specifies the name of the help file, and the Context argument provides the number that corresponds to the appropriate help topic for the message box.

The Return Value of a Message Box

The MsgBox() function can be used to return a value. This value corresponds to the button the user clicks on the message box. Depending on the buttons the message box is displaying, after the user has clicked, the MsgBox() function can return one of the following values:

If the user click The function returns Numeric Value
OK vbOK 1
Cancel vbCancel 2
Abort vbAbort 3
Retry vbRetry 4
Ignore vbIgnore 5
Yes vbYes 6
No vbNo 7
 

Practical LearningPractical Learning: Creating Message Boxes

  1. Start Microsoft Access
  2. From the resources that accompany these lessons, open the Exercise2 database
  3. Double-click the Messages form to open it
     

  4. Switch the form to Design View
  5. Right-click the Message 1 button and click Build Event...
  6. On the Choose Builder dialog box, click Code Builder
  7. Click OK
  8. In the Code Editor, implement it as follows:
    Private Sub cmdMessage1_Click()
        MsgBox "This is Visual Basic as simple as it can get"
    End Sub
  9. To test the form, return to Microsoft Access and switch the form to Form View
  10. On the form, click the Message 1 button
  11. Notice that a message box displays. Also notice the caption on the title bar displays Microsoft Access
  12. Click OK to close the message box
  13. Click the Message 2 button and notice that nothing happens
  14. Return to the Code Editor
  15. Instead of the title bar displaying Microsoft Access as the caption, you can set your own caption. This is done through the 3rd argument of the MsgBox function. To see an example, on the Object combo box, select cmdMessage2 and implement its Click event as follows:
    Private Sub cmdMessage2_Click()
        MsgBox "Before formatting a floppy disk, " & _
               "make sure you know its content", , _
               "Disk Formatting Instructions"
    End Sub
  16. Return to the form
  17. Click the Message 2 button
  18. Return to the Code Editor
  19. When creating a message box using the MsgBox function, you can decide which button you want to use, using one of the constants we have listed earlier.
    To see an example, on the Object combo box, select cmdMessage3 and implement its Click event as follows:
    Private Sub cmdMessage3_Click()
        MsgBox "This will be your only warning", _
               vbOKOnly + vbExclamation, _
               "Attention! Attention!! Attention!!!"
    End Sub
  20. Test the form and the Message 3 button. Return to the Code Editor
  21. If you want to display a message on different lines, you can use the vbCrLf constant.
    As an example, on the Object combo box, select cmdMessage4 and implement its Click event as follows:
    Private Sub cmdMessage4_Click()
        MsgBox "You are about to embark on a long journey." & _
               vbCrLf & "If your courage is still fresh, " & _
               "now is the time to let us know!", _
               vbOKCancel + vbQuestion, _
               "Accept or Cancel the Mission"
    End Sub
  22. Test the form and experiment with the Message 4 button. Then return to the Code Editor
  23. You can also display a message on various lines using the Chr() function. To see an example, on the Object combo box, select cmdMessage5 and implement its Click event as follows:
    Private Sub cmdMessage5_Click()
        MsgBox "This message usually appears when trying " & _
               "to format a floppy disk while the floppy drive " & _
               "is empty. " & Chr(13) & Chr(10) & _
               "When or if this happens, make sure you have a " & _
               " floppy disk in the floppy drive.", _
               vbAbortRetryIgnore + vbCritical, _
               "Floppy Disk Formatting"
    End Sub
  24. Test the form and the Message 5 button. Then return to the Code Editor
  25. The usefulness of the MsgBox function is demonstrated in your ability to perform an action based on the button the user has clicked on the message box. Indeed, the implementations we have used so far were on the MsgBox method. If you want to get the button that the user has clicked, you have to use the function itself. The true capture of the clicked button is revealed by your finding out the clicked button. This is done using conditional statements that we have not learned so far. Therefore, we will just learn how to implement the function and how to assign a response button to it; throughout this tutorial, and whenever necessary, we will eventually see what to do when a certain button has been clicked. To see an example, on the Object combo box, select cmdMessage6 and implement its Click event as follows:
    Private Sub cmdMessage6_Click()
        Dim intResponse As Integer
        
        intResponse = MsgBox("Your printing configuration " & _
                             "is not fully set." & vbCrLf & _
                             "If you are ready to print, click" & vbCrLf & _
                             "(1) Yes: To print the document anyway" & vbCrLf & _
                             "(2) No: To configure printing" & vbCrLf & _
                             "(3) Cancel: To dismiss printing", _
                             vbYesNoCancel + vbInformation, _
                             "Critical Information")
    End Sub
  26. Test the form and the Message 6 button. Then return to the Code Editor
  27. When a message box displays, one of the buttons, if more than one is displaying, has a thicker border than the other(s); such a button is called the default button. By default, this is the 1st or most left button on the message box. If you want to control which button would be the default, use one of the default constant buttons listed above. To see an example, on the Object combo box, select cmdMessage7 and implement its Click event as follows:
    Private Sub cmdMessage7_Click()
        Dim intAnswer As Integer
        
        intAnswer = MsgBox("Do you want to continue this " & _
                           "operation?", _
                           vbYesNoCancel + vbQuestion + vbDefaultButton2, _
                           "National Inquiry")
    End Sub
  28. Test the form and the Message 7 button. Then return to the Code Editor
  29. Although the user cannot type on a message box, not only can you decide what it displays, but you can also use string variables that would be available only when the form is running. As an example, on the Object combo box, select cmdMessage8 and implement its Click event as follows:
    Private Sub cmdMessage8_Click()
        Dim strEmplName As String
        Dim intInquiry As Integer
        
        strEmplName = CStr(txtEmployeeName)
        intInquiry = MsgBox("Hi, " & strEmplName & Chr(13) & _
                            "I think we met already." & vbCrLf & _
                            "I don't know when. I don't know where." & vbCrLf & _
                            "I don't know why. But I bet we met somewhere.", _
                            vbYesNo + vbInformation, _
                            "Meeting Acquaintances")
    End Sub
  30. To test the form, return to Microsoft Access
  31. On the Employee Name text box, type Joseph Douglas
  32. Click the Message 8 button and see the result
  33. Click one of the buttons to close the message box
  34. Close the running form, click its close button Close
 
 
   
 

Home Copyright © 2002-2013 FunctionX, Inc. Home