Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

how to use the BeforeUpdate event of a bound form in ACCESS 2010

I am trying to use more bound forms, but am having some issues.  For continuous form, if several of the fields are required, where and how would be the best way to validate the data before the record is saved?  That is, if the user has only entered three of five required fields, I need to return a message box telling them which fields still need to be completed.  I have, on my unbound form, a procedure that loops through the fields on the form with the Required tag and produces a Message box that displays those fields that are left to fill.  It also dislays little warning icons next to the label for the fields with missing data.  But if the form is continuous, what would be the best way to do this?
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

In a continuous form, you cannot make unbound controls visible (warning icons).  However, you could set the conditional formatting of required fields to a color if they currently have no value.

Then, you could use the Form_BeforeUpdate event to check for your required fields, display a message box and then set the Cancel argument to true if one of the required fields is missing.  something like:
Private Sub Form_BeforeUpdate(Cancel as integer)

    if me.txt_RequireField & "" = "" then
        msgbox "Required field must be entered"
        me.txt_RequiredField.setfocus
        Cancel = true
    Elseif me.txt_RequiredField2 & "" = "" then
        msgbox "Required field #2 must be entered"
        me.txt_RequiredField2.setfocus
        Cancel = true
    End If

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To give a bit more commentary on Before Update:

The BU event can be cancelled, which means you can force the user to remain on that record until they either (a) enter valid data in all required fields or (b) revert their changes (or revert their record addition, if they're adding a new record). In a Bound form, that's the place to make validation, as the others have said.

Remember too that each Control has a Before Update event, and the Form has a Before Update event. If you want granular control, use the Control's BU event. If you want the user to enter a valid value in each control before they move to a different control, use the Control's BU event. Note, however, that this can get somewhat annoying to users, so be certain that's what you want. If you want to validate only when the user tries to save their changes/addition, then use the Form's BU event.

You can still use the methodology you described here:

"a procedure that loops through the fields on the form with the Required tag and produces a Message box that displays those fields that are left to fill."

This gives the user information necessary to fulfill the requirements. I don't think you can use the icon method, or the concept of coloring the background (unless you use Conditional Formatting, as Dale suggests).
As the others have already mentioned, you can use the same loop technique with a continuous form.  The problem will be with colorization.  Continuous forms have more possibilities than do datasheet forms.  With datasheet forms, you are pretty much restricted to conditional formatting.  Although by adding hidden controls to a continuous form, you can get conditional formatting to work for you.  You just use hiddenctl1 in the conditional formatting statement for visiblectl1 to control formatting.  With the ds view form you don't have hidden controls although you can make the width 0 so that effectively the columns are hidden.  The problem is that the user could undo that by choosing show all in the column properties of the form.

Access keeps only one set of properties for a form so when you are working with continuous and ds forms where you are seeing multiple instances of the same form, formatting done with VBA will appear to affect all visible rows which is why you need to try to use conditional formatting which circumvents the problem.
.


in the code i posted above, you can change the backcolor of the controls that are nulls.
Avatar of Sandra Smith

ASKER

I want to thank all for the explanations and clarifications.  This is really helping in reducing my coding, won't get rid of unbound forms completely, but I can take advantage a little more of bound forms.