I have a button on a form. When I click the button it
opens a report. What I would like to do is close the form
after the report has opened. I used the code below but the
form won't close.
Can any one help?
Nick
Private Sub cmdNo_Click()
On Error GoTo Err_cmdNo_Click
Dim stDocName As String
stDocName = "SelectedLabels"
DoCmd.OpenReport stDocName, acPreview
Exit_cmdNo_Click:
Exit Sub
Err_cmdNo_Click:
MsgBox Err.Description
Resume Exit_cmdNo_Click
DoCmd.Close
End Sub
fredg - 31 Mar 2005 05:12 GMT
> I have a button on a form. When I click the button it
> opens a report. What I would like to do is close the form
[quoted text clipped - 19 lines]
> DoCmd.Close
> End Sub
You placed your code to close the form in the form's error handler. If
there is no error, the code doesn't run. Plus, even if there was an
error, you placed it after the Resume Exit_cmdNo_Click, so your code
would never get to it anyway.
Instead, place the code in the Report's Close event. when the report
closes it will then close the form:
DoCmd.Close acForm, "Name of Form"

Signature
Fred
Please only reply to this newsgroup.
I do not reply to personal email.
Jeff Conrad - 31 Mar 2005 05:21 GMT
Change your code to this:
Private Sub cmdNo_Click()
On Error GoTo Err_cmdNo_Click
Dim stDocName As String
stDocName = "SelectedLabels"
DoCmd.OpenReport stDocName, acPreview
DoCmd.Close acForm, Me.Name
Exit_cmdNo_Click:
Exit Sub
Err_cmdNo_Click:
MsgBox Err.Description
Resume Exit_cmdNo_Click
End Sub
Where you had the close code before it would only reach that
point if you had an error. You need to move it somewhere above
the "Exit Sub" line. Also, the line I posted explicity tells Access
I want to close a form and I want to close *this* form running the
code. I usually enter the name of the form as well so there are no
mistakes.

Signature
Jeff Conrad
Access Junkie
Bend, Oregon
> I have a button on a form. When I click the button it
> opens a report. What I would like to do is close the form
[quoted text clipped - 19 lines]
> DoCmd.Close
> End Sub
Nick - 31 Mar 2005 09:16 GMT
Thanks very much Jeff. work a treat.
Nick
>-----Original Message-----
>Change your code to this:
[quoted text clipped - 49 lines]
>
>.
Jeff Conrad - 31 Mar 2005 17:50 GMT
You're welcome, glad we could help.

Signature
Jeff Conrad
Access Junkie
Bend, Oregon
> Thanks very much Jeff. work a treat.
>
> Nick