Report RecordSource

  • Thread starter Thread starter Warrio
  • Start date Start date
W

Warrio

Hello!!

Is it possible to change a report's recordsource before open it?

when I click on buton located on my form, I have the following code:

Report_myReport.RcordSource = "Select * from my Table"
DoCmd.OpenReport "myReport, acPreview

The code runs untill the line where the recordsource is changed and an
Visual C++ Runtime error appears and tells that Access asks to close the
application. and then MS Access closes!!

Is there an easy way to change the RecordSource of a report before
despalaying the report???
 
Is it possible to change a report's recordsource before open it?
when I click on buton located on my form, I have the following code:

Report_myReport.RcordSource = "Select * from my Table"
DoCmd.OpenReport "myReport, acPreview

The code runs untill the line where the recordsource is changed and an
Visual C++ Runtime error appears and tells that Access asks to close the
application. and then MS Access closes!!

Is there an easy way to change the RecordSource of a report before
despalaying the report???

You need to change the recordsource *after" the report opens, from within the
report's own "OnOpen" event procedure - you cannot do it from outside the
report. If you declare a public variable to hold your Recordsource value, you
can set that value before opening the report, then in the report's "OnOpen"
event procedure, assign the value of that variable to the report's Recordsource
property:

'***In standard module (a global module, not behind a form or report)
Public strRecordSource As String
'***

'***In code that opens the report
strRecordSource = "Select * from my Table"
DoCmd.OpenReport "myReport, acPreview
'***

'***In the report's "OnOpen" event procedure
If Len(strRecordSource) > 0 Then
Me.Recordsource = strRecordSource
strRecordSource = ""
End If
'***
 
One way that I have done it in the past is the following:

DoCmd.OpenReport "myReport", acViewDesign
Reports!myReport.RecordSource = "Select * from my Table"
DoCmd.OpenReport "myReport", acViewPreview

techically you are not changing it before your are opening the report since
you are opening the report in design view first, but it does what you are
looking for and at least on the PC's I use it on it goes so fast that the
user never sees the report in design mode.

Sue
 
It works great! thanks


Bruce M. Thompson said:
You need to change the recordsource *after" the report opens, from within the
report's own "OnOpen" event procedure - you cannot do it from outside the
report. If you declare a public variable to hold your Recordsource value, you
can set that value before opening the report, then in the report's "OnOpen"
event procedure, assign the value of that variable to the report's Recordsource
property:

'***In standard module (a global module, not behind a form or report)
Public strRecordSource As String
'***

'***In code that opens the report
strRecordSource = "Select * from my Table"
DoCmd.OpenReport "myReport, acPreview
'***

'***In the report's "OnOpen" event procedure
If Len(strRecordSource) > 0 Then
Me.Recordsource = strRecordSource
strRecordSource = ""
End If
'***
 
Back
Top