Close form

N

Nick

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
 
F

fredg

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

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"
 
J

Jeff Conrad

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.

--
Jeff Conrad
Access Junkie
Bend, Oregon

in message:
 
N

Nick

Thanks very much Jeff. work a treat.

Nick
-----Original Message-----
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.

--
Jeff Conrad
Access Junkie
Bend, Oregon

in message:



.
 
J

Jeff Conrad

You're welcome, glad we could help.

--
Jeff Conrad
Access Junkie
Bend, Oregon

"Nick" wrote in message:
 

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