By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,583 Members | 3,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,583 IT Pros & Developers. It's quick & easy.

URGENT NEED - cancel opening of report via form command button

100+
P: 114
**Urgent Need**

I'll throw out the basics and any assistance is very, very, very much appreciated!

Access 2003 on XP
On a form (frmMain) is an option group of check boxes (ReportFrame) from which a user can choose a specific report to generate. The user selects the report of choice then presses a comand button (CmdRunRpt). The command button runs using the code below:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdRunRpt_Click()
  2. If ReportFrame = 1 Then DoCmd.OpenReport "Rpt 1", acViewPreview
  3. If ReportFrame = 2 Then DoCmd.OpenReport "Rpt 2", acViewPreview
  4. If ReportFrame = 3 Then DoCmd.OpenReport "Rpt 3", acViewPreview
  5. If ReportFrame = 4 Then DoCmd.OpenReport "Rpt 4", acViewPreview
  6. If ReportFrame = 5 Then DoCmd.OpenReport "Rpt 5", acViewPreview
  7. If ReportFrame = 6 Then DoCmd.OpenReport "Rpt 6", acViewPreview
  8. If ReportFrame = 7 Then DoCmd.OpenReport "Rpt 7", acViewPreview
  9. If ReportFrame = 8 Then DoCmd.OpenReport "Rpt 8", acViewPreview
  10. If ReportFrame = 9 Then DoCmd.OpenReport "Rpt 9", acViewPreview
  11. End Sub
This works perfectly. The report opens by first opening a form (Search Start Detail) that provides criteria for the report, which runs on a query.
On the criteria form (Search Start Detail), there are two command buttons: OK and CANCEL. Selecting OK gives the data to the query and generates the report - this works perfectly.
Here's the issue:
Selecting CANCEL should close the form and cancel the generation of any report (as there is no criteria to generate). When I click CANCEL, the form closes correctly but then there is an error message stating:
Expand|Select|Wrap|Line Numbers
  1. Run-time error '2501':
  2. The OpenReport action was canceled.
I'm sure this is a simple twist of the IF code but I'm not sure. Thank you very much for your help - I need to have this straightened out by Wednesday morning.
martin
Oct 30 '07 #1
Share this Question
Share on Google+
6 Replies


missinglinq
Expert 2.5K+
P: 3,532
What's your code for cancelling the report?

Linq ;0)>
Oct 30 '07 #2

100+
P: 114
Here's the code for the click event of the cancel button on the form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Cancel_Click()
  2.     'Purpose:   Clear all the search boxes in the Form Detail and close the form.
  3.     Dim ctl As Control
  4.     'Clear all the controls in the Form Detail section.
  5.     For Each ctl In Me.Section(acDetail).Controls
  6.         Select Case ctl.ControlType
  7.         Case acTextBox, acComboBox
  8.             ctl.Value = Null
  9.         Case acCheckBox
  10.             ctl.Value = False
  11.         End Select
  12.     Next
  13.     'Remove the form's filter.
  14.     Me.FilterOn = False
  15.     'Close the form.
  16.     DoCmd.close
  17. End Sub
Here is the code on the report:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Report_Close()
  4.   DoCmd.close acForm, "Search Start Detail"
  5. End Sub
  6.  
  7. Private Sub Report_Open(Cancel As Integer)
  8. ' Set public variable to true to indicate that the report
  9. ' is in the Open event
  10.   bInReportOpenEvent = True
  11.  
  12. ' Open Search Start Detail Dialog
  13.   DoCmd.OpenForm "Search Start Detail", , , , , acDialog
  14.  
  15. ' Cancel Report if User Clicked the Cancel Button
  16.   If IsLoaded("Search Start Detail") = False Then Cancel = True
  17.  
  18. ' Set public variable to false to indicate that the
  19. ' Open event is completed
  20.   bInReportOpenEvent = False
  21. End Sub
And here is the supporting module for the report code:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Public bInReportOpenEvent As Boolean ' Is report in the Open event?
  4.  
  5. Function IsLoaded(ByVal strFormName As String) As Boolean
  6. ' Returns True if the specified form is open in Form view or
  7. ' Datasheet view.
  8.   Dim oAccessObject As AccessObject
  9.   Set oAccessObject = CurrentProject.AllForms(strFormName)
  10.   If oAccessObject.IsLoaded Then
  11.     If oAccessObject.CurrentView <> acCurViewDesign Then
  12.       IsLoaded = True
  13.     End If
  14.   End If
  15. End Function
Hope that helps - thanks!
martin
Oct 31 '07 #3

missinglinq
Expert 2.5K+
P: 3,532
In the Sub Report_Open() replace the line

If IsLoaded("Search Start Detail") = False Then Cancel = True

with the lines

DoCmd.SetWarnings False
If IsLoaded("Search Start Detail") = False Then Cancel = True
DoCmd.SetWarnings True

and see if that surpresses the message.

Linq ;0)>
Oct 31 '07 #4

100+
P: 114
I'm afraid not, Linq. When I click debug it comes back to the code for the main form (where the user selects and runs a report):
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdRunRpt_Click()
  2. If ReportFrame = 1 Then DoCmd.OpenReport "Rpt 1", acViewPreview
  3. If ReportFrame = 2 Then DoCmd.OpenReport "Rpt 2", acViewPreview
  4. If ReportFrame = 3 Then DoCmd.OpenReport "Rpt 3", acViewPreview
  5. If ReportFrame = 4 Then DoCmd.OpenReport "Rpt 4", acViewPreview
  6. If ReportFrame = 5 Then DoCmd.OpenReport "Rpt 5", acViewPreview
  7. If ReportFrame = 6 Then DoCmd.OpenReport "Rpt 6", acViewPreview
  8. If ReportFrame = 7 Then DoCmd.OpenReport "Rpt 7", acViewPreview
  9. If ReportFrame = 8 Then DoCmd.OpenReport "Rpt 8", acViewPreview
  10. If ReportFrame = 9 Then DoCmd.OpenReport "Rpt 9", acViewPreview
  11. End Sub
Oct 31 '07 #5

missinglinq
Expert 2.5K+
P: 3,532
So try turning the warnings on and off at this point.
Oct 31 '07 #6

100+
P: 114
So try turning the warnings on and off at this point.
Same results here: Error 2501. I have tried setting the warnings to false on all three objects involved: the report, the main form (where the vba returns on debug), and the criteria form (where I actually select Cancel).
Any other ideas?
Oct 31 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.