6.3. Building User Interaction

After you have chosen the right controls, you must program ways for users to interact with those controls. You can program the application to interact with a user in various ways. For example, you have already learned how to write event procedure code that executes when the user takes a particular action on the form. In this section, you will look at a couple of additional examples of how you can improve user interaction in your applications. You will look at using the MsgBox function for more complex scenarios and at validating user input.

6.3.1. The MsgBox Function

You have already seen examples of the MsgBox function to display a message. The MsgBox function is a lot more powerful than the simple examples I have illustrated so far. You can actually ask questions with multiple-choice answers and then process those choices to take the appropriate action.

For example, to ask the user a Yes/No question and then take a different action depending on the response, you can specify the vbYesNo value for the message box style parameter. The following example prompts the user to confirm that the deletion should be processed:

Dim intResponse As Integer

'display a Yes/No message box to the user
intResponse = MsgBox("Are you sure you want to delete this record?", vbYesNo, "Delete?")

'determine how the user responded and act accordingly
If intResponse = vbYes Then
    MsgBox "Delete confirmed."
ElseIf intResponse = vbNo Then
    MsgBox "Delete cancelled."
End If

Get Beginning Access™ 2007 VBA now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.