Avatar of Ken Milam
Ken MilamFlag 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?

Microsoft AccessVBA

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

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:



Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Blurred text
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
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo