Avatar of Ken Milam
Ken Milam
Flag for United States of America asked on

Print rport button w "Where =" conditions.

I have a form [f-BagLabel] on to which I would like to place a "Print Report" Button.  A similar report [r-BagLabel] already exists, and its based on a query [q-r-BagLabel].  In this case, however, I want the PrintReport button to print a report specific to the current record at [f-BagLabel]

Ordinarily, I would copy the report and copy the query, associate some query criteria from [f-BagLabel] into the new query and associate the new copied report with the new query w/ criteria.  Then, I would add a button that would open this newly created report.  This, as I am sure you are aware, results in multiple instances of the similar queries and reports.  I need to learn how to use conditions in VBA to accomplish this task w/o resorting to my current technique.

How would I incorporate some VBA into this Button to print a report whereby a field in q-r-BagLabel = some value on the form where the button is positioned?

Thanks,
Ken
Microsoft AccessVBA

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)

8/22/2022 - Mon
Jim Dettman (EE MVE)

You can use the 'Where' argument of the Open Report.   This would be the syntax if it was a numeric value:


DoCmd.OpenReport "myReport", acViewNormal, , "[q-r-BagLabel] = " & Forms![myFormNameHere]![myControlNameHere]


This would be for a string:


DoCmd.OpenReport "myReport", acViewNormal, , "[q-r-BagLabel] = " & chr$(34) & Forms![myFormNameHere]![myControlNameHere] & chr$(34)


Chr$(34) gives you a " (quote) character. There are other ways to do that:


 DoCmd.OpenReport "myReport", acViewNormal, , "[q-r-BagLabel] = '" & Forms![myFormNameHere]![myControlNameHere] & "'"


 is one such.  But they can be tough to read.


Docs for the OpenReport action is here by the way:


 https://docs.microsoft.com/en-us/office/vba/api/access.docmd.openreport


Jim.

ASKER CERTIFIED SOLUTION
Jim Dettman (EE MVE)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck