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.

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 

Validating User Input

Another way to build user interaction is to provide feedback to the user when the data entered into a control is not of the correct data type. By correcting the data error up front, you can avoid a database error later or avoid a data integrity problem caused by invalid values. One way to implement data validation in Access is to set the ValidationRule and ValidationText properties for the control to specify the type of data that is valid and the message that should be displayed when the data does not conform to the rule.

You can also handle data validation from VBA code, such as in the BeforeUpdate event or AfterUpdate event of the control.

Try It Out-Adding Data Validation to the frmPayments Form

image from book

Here you modify the sample application to implement a validation event for the frmPayments form.

  1. Open frmPayments in design view.

  2. Add a text box control to the form and name it txtValue.

  3. In the BeforeUpdate event for the text box, add the following code:

      Private Sub txtValue_BeforeUpdate(Cancel As Integer) If txtValue = "" Or IsNull(txtValue) Then     MsgBox "You must specify a value for this field."     Cancel = True ElseIf Len(txtValue) > 10 Then     MsgBox "The value for this field must be 10 characters or less."     Cancel = True End If End Sub 

  4. Run the form, select the field and enter a value with more than 10 characters, and then press the tab key to leave the field. You should see a message indicating that the value must be 10 characters or less.

  5. Delete the value entirely so that the field is blank. Then tab to another field. You should see a message indicating that a value must be specified for the field.

How It Works

In this example, you added a text box control to the form. You then added code for the BeforeUpdate event to make sure that the user entered a valid value in this required field.

image from book




Beginning Access 2007 VBA
Beginning Access 2007 VBA
ISBN: 0470046848
EAN: 2147483647
Year: 2004
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net