Go Back   Access World Forums > Microsoft Access Discussion > Reports

 
Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 03-31-2005, 08:56 AM   #1
BJS
Registered User
 
Join Date: Aug 2002
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
BJS
Error 2191 - You can't set the recordsource property after printing has started

I have some code, which opens a report containing a subreport. The recordsource for the subreport is dynamically set with the on_open event of the subreport.

On the click of a button on my form, the report opens and the subreport displays the correct information as per the recordsource.

BUT....

Now I have added one line of code after the report is opened:

This code works by itself...the subreport displays correct information when the report is opened
Code:
DoCmd.OpenReport "rptReturnToWorkNotice", acViewPreview
As soon as I add this line of code following the above line of code, I get "Runtime Error 2191 - You can't set the recordsource property after printing has started"

Code:
DoCmd.SendObject acSendReport, "rptMainReport", "Snapshot Format", Me.Email, , , Me.Subject & " - " & Me.ename, Me.Body, 0
I tried using the SendObject method in different places, but no luck.
Any ideas????

Thanks in advance,
BJS

BJS is offline   Reply With Quote
Old 03-31-2005, 10:09 AM   #2
BJS
Registered User
 
Join Date: Aug 2002
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
BJS
As I continue to struggle with this, I have so far determined that it has to do with the SendObject method not working if I dynamically set the recordsource for the subreport.

The SendObject method works if I do not set the recordsource for the subreport dynamically.

Any ideas yet....anyone???
BJS is offline   Reply With Quote
Old 03-31-2005, 12:43 PM   #3
BJS
Registered User
 
Join Date: Aug 2002
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
BJS
Thumbs up

I got it working....after searching the internet for this error message; someone came up with this explanation:

"Access is probably firing the
Open event of the subreport twice.

The first occurance fires *before* the main report opens, so any attempt to
reference anything in the main report fails.

The second occurance fires after the report is already in progress (as
non-sensical as that sounds), and so attempts to assign the recordsource in
this 2nd instance of Report_Open fails."

The solution was to create a global variable as a flag to indicate the main report was opened. Then on the On_Open Event of the subform, test the variable to see if the report was already open, in which case the recordsource for the subreport was already set. By doing this, it does not try to set the recordsource for the subreport again, and has no issue executing the SendObject method.

I hope this helps others who may run across this same error message.
I am a happy camper!

BJS

BJS is offline   Reply With Quote
Old 03-31-2005, 12:46 PM   #4
BJS
Registered User
 
Join Date: Aug 2002
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
BJS
Thumbs up

I got it working....after searching the internet for this error message; someone came up with this explanation:

"Access is probably firing the
Open event of the subreport twice.

The first occurance fires *before* the main report opens, so any attempt to
reference anything in the main report fails.

The second occurance fires after the report is already in progress (as
non-sensical as that sounds), and so attempts to assign the recordsource in
this 2nd instance of Report_Open fails."

The solution was to create a global variable as a flag to indicate the main report was opened. Then on the On_Open Event of the subform, test the variable to see if the report was already open, in which case the recordsource for the subreport was already set. By doing this, it does not try to set the recordsource for the subreport again, and has no issue executing the SendObject method.

I hope this helps others who may run across this same error message.
I am a happy camper!

BJS
BJS is offline   Reply With Quote
Old 04-22-2007, 08:25 PM   #5
associates
Newly Registered User
 
Join Date: Jan 2006
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
associates is an unknown quantity at this point
Hi BJS,

I hope you still visit this forum or anyone might be able to help me. I have the same problem as BJS with the following error

runtime error '2191' - you can't set the control source property in print preview or after printing has started.

and tried to put his solution into code. I got confused with declaring the global variable in VBA. Because it didn't work.

Here is my code for the Main Report
Code:
Option Compare Database
Public myOpenFlag as boolean

Private Sub Report_Open(Cancel As Integer)
 myOpenFlag = True
 msgbox "Pass Main Report"
End Sub
Here is the code for Sub-Report
Code:
Private Sub Report_Open(Cancel As Integer)

 If myOpenFlag = True Then
    MsgBox "openFlag true"
 Else
    MsgBox "openFlag false"
 End If
 
 YearBase$ = Form_CHW_Startup.CB_Year.Value
 YearValue = Val(Mid$(YearBase$, 3, 2))
 YearInit$ = Left$(YearBase$, 2)
 
 If openFlag = False Then
 CurrentYear$ = YearInit$ + Right$("00" + Trim(Str$(YearValue)), 2) + "/" + Right$(Trim(Str$(YearValue + 1)), 2)
 Me.TX_Year1.Caption = CurrentYear$
 Me.TB_Year1.ControlSource = CurrentYear$
 Me.TB_Year1_Sum.ControlSource = "=sum([" + CurrentYear$ + "])"

 YearValue = YearValue + 1
 CurrentYear$ = YearInit$ + Right$("00" + Trim(Str$(YearValue)), 2) + "/" + Right$(Trim(Str$(YearValue + 1)), 2)
 Me.TX_Year2.Caption = CurrentYear$
 Me.TB_Year2.ControlSource = CurrentYear$
 Me.TB_Year2_Sum.ControlSource = "=sum([" + CurrentYear$ + "])"
I got "Pass Main Report" messagebox but then I got "openFlag false" messagebox when it should be "openFlag true". I think my global variable doesn't work.

Can anyone help me out please?

Thank you in advance
associates is offline   Reply With Quote
Old 04-22-2007, 08:52 PM   #6
The_Doc_Man
Happy Retired Curmudgeon
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 12,926
Thanks: 65
Thanked 1,316 Times in 1,213 Posts
The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold
This is a guess.

Once the report starts printing, all reports and subreports are opened because of the desire (by Access) to protect data via locking. Well, when you attempt to explicitly open the sub-form with what appears to be a filter, you can't - because it is locked. EITHER leave the sub-form unbound and dynamically bind it - or don't impose a filter from code. The issues is likely to be a binding-time inconsistency.

__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over one year and survived being home all day with the wife. She must really love me.
If I have helped you, please either click the thanks or click the scales.
The_Doc_Man is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump




All times are GMT -8. The time now is 05:56 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World