ladybug via AccessMonster.com wrote:
I have a form called "EmpRpt". It has three fields. First field is
"chrUserID," this is a combo box with a row source from another table. The
Second Field is "BeginDate." The Third field is "EndDate."
I then have a command button. I want to put a code in this buttons on click
event. The code should open up a report called "EmpRpt."
The report will have the three fields just like the form. I want those three
fields to appear with the data the user entered in the form. I would also
like this report to open in print preview.
Can someone please help me with a code? Thank you!
You could create a query called EmpRpt. Add the 2 tables (since the
combo is linked) you use and drag down the fields you want from the 2
tables. Save the query.
Go to Reports, click New, and use the ReportWizard. Select the query
EmpRpt. Follow the steps to create the report. Save it as EmpRpt.
Now in your command button you open the property sheet to the event tab
and select the OnCLick event. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview
You can filter it as well. Lets say you have a field called EmpID and
it is numeric. You could enter
Docmd.OpenReport "EmpRpt",acViewPreview,,"EmpID = " & Me.chrUserID
and this will display the report for the current empid.
Now it's possible you might open a report where all of the data has not
been saved. If this were a new record, the report might even say no
data exists! So sometimes you might want to save the record first
before displaying a report. Your code might look like this
If Me.Dirty then Me.Dirty = False
Docmd.OpenReport...
or
If Me.Dirty then DoCmd.RunCommand acCmdSaveRecord
Docmd.OpenReport...