Close report with code

C

chuck

I have a report which is generated from data in a form. If the report is
opened directly, without going through the form, some values will be missing
from the report. To prevent this, I want to add some code which will be
executed with the report's On Open event. Essentially, if the report is
opened from a form, then open the report. If the report is opened from the
Reports window in Access, then a message is displayed and the report should
close.

Sounds simple enough except I always get this error message when the code
tries to close the report:

"Run-time error '2585': This action can't be carried out while processing a
form or report event".

I tried closing the report with " DoCmd.Close acReport 'myReport' " and "
DoCmd.RunCommand acCmdClose " but these both give the same error.

Any ideas on how I can close the report?



Thanks
 
R

Rod

Put this in the report open event:

If Not (IsLoaded("Yourform")) Then
Cancel = True
MsgBox "Your message.", vbExclamation, "Msgbox heading"
Exit Sub
End If

You'll also need this external module (not part of form
code):

Function IsLoaded(strFrmName As String) As Boolean
' Determines if a form is loaded.
Const conFormDesign = 0
Dim intX As Integer

IsLoaded = False
For intX = 0 To Forms.Count - 1
If Forms(intX).FormName = strFrmName Then
If Forms(intX).CurrentView <> conFormDesign Then
IsLoaded = True
Exit Function ' Quit function once form has
been found.
End If
End If
Next
End Function
 
C

chuck

Excellent! Works like a charm. Thanks


Rod said:
Put this in the report open event:

If Not (IsLoaded("Yourform")) Then
Cancel = True
MsgBox "Your message.", vbExclamation, "Msgbox heading"
Exit Sub
End If

You'll also need this external module (not part of form
code):

Function IsLoaded(strFrmName As String) As Boolean
' Determines if a form is loaded.
Const conFormDesign = 0
Dim intX As Integer

IsLoaded = False
For intX = 0 To Forms.Count - 1
If Forms(intX).FormName = strFrmName Then
If Forms(intX).CurrentView <> conFormDesign Then
IsLoaded = True
Exit Function ' Quit function once form has
been found.
End If
End If
Next
End Function
 

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