Don't Open Report unless form is open

M

Mrstacy

I'd like a form to not open unless a form is open. I attached this
code to the report's open event, but it gives an error. Any help is
appreciated.

If Not CurrentProject.AllForms("Frm_Date_Range").IsLoaded Then
MsgBox "Data Range Form Not Open", vbCritical, "Error"
DoCmd.Close acReport, Me.Report

End If
 
F

fredg

I'd like a form to not open unless a form is open. I attached this
code to the report's open event, but it gives an error. Any help is
appreciated.

If Not CurrentProject.AllForms("Frm_Date_Range").IsLoaded Then
MsgBox "Data Range Form Not Open", vbCritical, "Error"
DoCmd.Close acReport, Me.Report

End If

It usually does no good to tell us it gives an error.
What line is highlighted in the code window?
What error? What is the Error text and Error Number?

Always give your Access Version number.
Are you using Access 2000 or newer?

Any event that has Cancel as an argument, you can use Cancel = True to
halt that event.

Private Sub Report_Open(Cancel as Integer)

If Not CurrentProject.AllForms("Frm_Date_Range").IsLoaded Then
MsgBox "Data Range Form Not Open", vbCritical, "Error"
Cancel = True
End If

End Sub
 
M

Mrstacy

It usually does no good to tell us it gives an error.
What line is highlighted in the code window? This line was: DoCmd.Close acReport, Me.Report

It is working now thank you.
What error? What is the Error text and Error Number?
Access 2003
 
K

Klatuu

fredg is correct.
As he said, version, error number, and line on which it occurs is important
to solving the problem.

Note, when you get this error resolved and you use Cancel = True, you get an
error 2501 where ever it is you try to open the report. That is normal.
What you can do is use an error handler in your code to ignore that error.
But, a better plan would be to check to see if the form is open before you
open the report.

If CurrentProject.AllForms("Frm_Date_Range").IsLoaded Then
Docmd.OpenReport .....
Else
MsgBox "Data Range Form Not Open", vbCritical, "Error"
End If
 

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