Access Report: How to capture where condition in a string

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Access Report: How to capture where condition in a string

Post by Carol W. »

Access 2003:

I have a report that is opened with a where condition (4th parameter). How can I capture that condition into a string once the report is opened?

Explanation: I have a situation where I may need to cancel a report (A) and open another report (B) using the same where condition that was used in opening A. I know it would be best to use logic in the calling module to decide whether to open A or B but it would involve a lot of coding changes to do it that way. Am hoping to be able to do it this way,

Hope this is clear. Thanks.
Carol W.

User avatar
HansV
Administrator
Posts: 78455
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Access Report: How to capture where condition in a string

Post by HansV »

A report doesn't "know" whether it was opened with or without a where-condition.

You could store the where-condition in a public variable, and inspect that in the code behind the report.
Or you could pass the same string both as WhereCondition (the 4th argument to DoCmd.OpenReport) and as OpenArgs (the 6th argument):

Dim strWhere As String
strWhere = ...
DoCmd.OpenReport "rptSomething", acViewPreview, , strWhere, , strWhere

You can then inspect the OpenArgs property in the On Open event of the report:

If Me.OpenArgs = "..." Then
Best wishes,
Hans

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Access Report: How to capture where condition in a string

Post by Carol W. »

Hans,

I ended up inserting code in the calling module to decide whether to open report A or report B.

I had initially put the code in A's detail format event. When A cancelled out as I intended for it to do, lo and behold, it's heading had already printed. Not a good thing! :sad: So, I "bit the bullet" and did it the right way.

Thanks again.
Carol W.

User avatar
HansV
Administrator
Posts: 78455
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Access Report: How to capture where condition in a string

Post by HansV »

Modifying the code that opens the report is the best way to do it. :thumbup:

If you want to cancel a report, you need to use the On Open event or the On No Data event (the latter occurs if the record source of the report is empty) and set its Cancel argument to True. As you have found, the On Format or On Print event of a section occurs too late.
Best wishes,
Hans

User avatar
Carol W.
3StarLounger
Posts: 390
Joined: 26 Jan 2010, 16:02
Location: Las Vegas, NV

Re: Access Report: How to capture where condition in a string

Post by Carol W. »

Hans,

I was trying to avoid changing a key program but, alas, I had no choice.

Thanks, as always. :thankyou:
Carol W.