Before Update vs OnExit

  • Thread starter Uschi via AccessMonster.com
  • Start date
U

Uschi via AccessMonster.com

I have a form where all of the fields must be completed before 1. going back
to the Find dialog box (to find a new record to update) or 2. closing the
form. In reviewing the threads for the code to use on the fields I am
confused as to when one uses Before Update as opposed to OnExit for a field.

I should also tell you that the person entering the data likes to "click
around".

Any thoughts on this? I will also need help with the code.

Thanks so much,
Uschi
 
J

John W. Vinson

I have a form where all of the fields must be completed before 1. going back
to the Find dialog box (to find a new record to update) or 2. closing the
form. In reviewing the threads for the code to use on the fields I am
confused as to when one uses Before Update as opposed to OnExit for a field.

The Form's BeforeUpdate event, hands down. It fires when the user has
*changed* something in the record, and it can be cancelled if the record is
invalid (after a message to the user and an opportunity to fix the problem,
preferably!)

A textbox or other control has an Exit event, but you can't force the user to
even *enter* the control, so it's quite possible that the Exit event will not
fire at all.
I should also tell you that the person entering the data likes to "click
around".

Any thoughts on this? I will also need help with the code.

Something like

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Me!txtThis & "" = "" Then
MsgBox "Please fill in This!", vbOKOnly
Cancel = True
Me!txtThis.SetFocus
Exit Sub
End If
<similar code for other controls>
End Sub

You can and should certainly get more sophisticated (looping through the
form's Controls collection frex) but this is a start.
 
U

Uschi via AccessMonster.com

John,

Many thanks for reply. I get an error message (Error: =) on the first line
of the code.
What am I doing wrong?

Uschi
 
J

John W. Vinson

John,

Many thanks for reply. I get an error message (Error: =) on the first line
of the code.
What am I doing wrong?

Please post your code, so I or another volunteer can see it. Indicate the
fieldnames and control names on the form as well.
 
U

Uschi via AccessMonster.com

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Me!txtThis & ""DateIssued"" Then
MsgBox "Please enter the Date Issued.",vbOKOnly
Cancel = True
Me!txtThis.SetFocus
Exit Sub
End If
 
J

John W. Vinson

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Me!txtThis & ""DateIssued"" Then
MsgBox "Please enter the Date Issued.",vbOKOnly
Cancel = True
Me!txtThis.SetFocus
Exit Sub
End If

Well, that is NOT what I suggested.

Try:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Me!DateIssued & "" = "" Then ' concatenate the control to an empty string
' and compare the result with an empty string
MsgBox "Please enter the Date Issued.",vbOKOnly
Cancel = True
Me!DateIssued.SetFocus
Exit Sub
End If

Since I did not know the name of the control you had in mind, I used an
example (txtThis) assuming you would change it. I should have explicitly
suggested that you do so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top