stopping a macro by writing the code

R

Rob

I have a form that requires data to be entered into each field. On this form
there is a button that is clicked to run a macro. Before the macro runs, at
the update event property of the form, I have this function.

If IsNull(Me![ReleaseNumber]) Then
MsgBox "'Release #' is a required field."
Cancel = True
Me.[ReleaseNumber].SetFocus

End If

This continues for each of the fields. I want to write a simple statement
to stop the macro that is run if this condition exists and to SetFocus to the
field where the error occurred. The latter is working fine but how do I get
the macro to stop completely if this condition occurs.

Rob
 
S

Sreedhar

Hi Rob,

You may try this:

Private Sub cmdButton_Click()
On Error GoTo ErrHandle

Dim ctl As Control

For Each ctl In Me.Controls
If IsNull(ctl) Then
MsgBox "Your Message goes here"
GoTo ExitHandle
End If
Next

DoCmd.RunMacro MyMacro

ExitHandle:
Exit Sub

ErrHandle:
MsgBox Err.Description
Resume ExitHandle

End Sub
 

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