error handling in Access 2002

P

Peter Mollica

how do I force an error to occur? for example...in
the "lost focus" event for a text box I have some VBA
code that inspects the value. What I want to happen is
to be able to display a msgbox and return control to the
text box in error cancelling any subsequent actions
access would have done. If I was saving the record, I
want access to display the message, go to the text box
and stop, not saving the record.

What I have is...
If Weekday(week_ending) = vbSaturday Then
Else
MsgBox ("Week Ending Must be a Saturday!")
DoCmd.GoToControl "week_ending"
End If

The code executes perfectly but if I change the value of
the text box (week_ending) and "tab" to the next field it
does display the msgbox but it does not go to the
week_ending text box, the cursor goes to the field that
was next in the tab list, I assume because I hit
the "tab" button. How do I interrupt executions and
redirect focus?
 
G

Graham R Seach

Peter,

Check the online Help for Err.Raise.

However, I think you'd be better to use the BeforeUpdate event instead of
the LostFocus event. The BeforeUpdate event has a Cancel argument, which
when set to True, cancels any further (subsequent) event processing that
would normally have occurred as a result of the BeforeUpdate event. So in
essence, you can undo the changes and cancel the event.

Private Sub txtMyTextBox_BeforeUpdate(Cancel As Integer)
If Me.txtMyTextBox.Text <> somevalue Then
DoCmd.Beep
MsgBox "You got it wrong!"

Me.txtMyTextBox.Undo
Cancel = True
Else
'Processing continues on to the AfterUpdate event.
End If
End Sub

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

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