Close form

  • Thread starter Thread starter Nick
  • Start date Start date
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
 
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"
 
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:
 
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:



.
 
You're welcome, glad we could help.

--
Jeff Conrad
Access Junkie
Bend, Oregon

"Nick" wrote in message:
 
Back
Top