Changing RecordSource of "Report" from "Form"

L

laserized

Hi to all,
My question is:
I ve tried to change the RecordSoruce of a report from my form.
I ve Lot of If statements in my form and among those if statements, my
SQL code changes.
Now I want to assign my SQL code to the reports RecordSource, so that
report will be generated by my form.
well While im tryng to do that, VB sends a runtime error message that
saying, You cannot change the recordSource of the report before
openning it or printing it. or something like that.my code looks like
this

Reports_DenemeReport.RecordSource = strSQL
DoCmd.OpenReport "DenemeReport", acViewPreview

How can I do that from my form?

Any helps are appriciated.
Thanx.
metan.
 
S

SA

Metan:

Access' error is correct, you can't change a report's record source while it
is closed. You can do it two ways, either a.) by opening the report in
design mode, or b.) by getting the new recordsource string when the report
opens using its On Open event.

To do the latter, you can, in a db general module, declare a global variable
e.g.

Global strMyReportsSQLString

Then in the report's On Open event add code like this:

If Len(strMyReportsSQLString)>0 Then Me.RecordSource = strMyReportsSQLString

In the reports On Close event add code like this to clear the string out
until you set it again:

strMyReportsSQLString = ""

Last in your form your code would look like:

strMyReportsSQLString = strSQL
DoCmd.OpenReport "DenemeReport", acViewPreview
 

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