Close report with code

  • Thread starter Thread starter chuck
  • Start date Start date
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
 
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
 
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
 
Back
Top