On Error GoTo code acting weird

  • Thread starter Thread starter Angi
  • Start date Start date
A

Angi

This code is pretty straightforward and I've been using it for years,
but it started acting funny when I added a line.

I have a searchfrm with a Search Again btn. When the parameter prompt
comes up and I enter info, I can press the enter key and the query will
run. If I press the btn I can do it again and again. My
Exit_CmdFind_Click used to say Exit Sub, but that just brought me back
to the form. I wanted it to close the form, so I changed it to
docmd.close. Now, the form runs right the first time you open it (you
can use the enter key) but when you use the search again btn and then
press enter, the form now closes. It also closes when the cancel
button is clicked. I have the code set to only do this when the user
presses the cancel button (Err 2001), but now it's doing it all the
time. The ok is the default. Obviously because it doesn't happen when
that line isn't in there.

Can someone tell me why it's doing this or another way to close the
form if the user presses the cancel button on the parameter prompt?

Private Sub SearchAgainBtn_Click()
On Error GoTo Err_cmdFind_Click

Me.Requery

Err_cmdFind_Click:
If Err.Number = 2001 Then Resume Exit_cmdFind_Click

Exit_cmdFind_Click:
DoCmd.Close

End Sub

TIA!
 
Angi, the last command the code executes is the close command. Thats
why the form is closing. If its another form you want to close then
use the full expression:

DoCmd.Close acForm, "MyFormName"

Hope this helps.
 
No, it's closing the right form. That's not the problem. The problem
is
the form runs right the first time you open it (you can use the enter
key) but when you use the search again btn (requery the form) and then
press enter, the form now closes. I have the code set to only do this
when the user presses the cancel button (Err 2001), but now it's doing
it all the time.
 
Angi said:
No, it's closing the right form. That's not the problem. The problem
is
the form runs right the first time you open it (you can use the enter
key) but when you use the search again btn (requery the form) and then
press enter, the form now closes. I have the code set to only do this
when the user presses the cancel button (Err 2001), but now it's doing
it all the time.

Angi, in the code you posted, the statements
Exit_cmdFind_Click:
DoCmd.Close

will be executed every time the procedure is executed, no matter what
happens. Even if there's no error, the sequential execution of
statements will reach that line. Maybe this is what you really wanted:

'----- start of revised code -----
Private Sub SearchAgainBtn_Click()

On Error GoTo Err_cmdFind_Click

Me.Requery

Exit_cmdFind_Click:
Exit Sub

Err_cmdFind_Click:
If Err.Number = 2001 Then
DoCmd.Close, acForm, Me.Name
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_cmdFind_Click

End Sub
'----- end of revised code -----
 
In your code anything between Exit_cmdFind_Click: and End Sub if done everytime
you click the button. You need to move the Err_cmdFind_Click: after the
Exit_cmdFind_Click: and move the DoCmd.Close to your If like below:

Private Sub SearchAgainBtn_Click()
On Error GoTo Err_cmdFind_Click
Me.Requery

Exit_cmdFind_Click:
Exit Sub

Err_cmdFind_Click:
If Err.Number = 2001 Then 'user pressed the cancel button
DoCmd.Close
Resume Exit_cmdFind_Click
end if
End Sub

HTH,
Debbie


| No, it's closing the right form. That's not the problem. The problem
| is
| the form runs right the first time you open it (you can use the enter
| key) but when you use the search again btn (requery the form) and then
| press enter, the form now closes. I have the code set to only do this
| when the user presses the cancel button (Err 2001), but now it's doing
| it all the time.
|
 
Back
Top