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
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.