You will need to add a Where Condition to the OpenReport method so that the
report shows only the information for the record displayed on your form. The
record will need to have a unique identifier, presumably WorkOrderNumber.
Then, in the Click event of your command button, you can insert the
following:
' This works when your WorkOrderNumber field is text
Dim strCriteria As String
strCriteria = "[WorkOrderNumber] = " & Chr(34) & Me!WorkOrderNumber &
Chr(34)
DoCmd.OpenReport "MyReport", acViewNormal, , strCriteria
' This works when your WorkOrderNumber field is a number
Dim strCriteria As String
strCriteria = "[WorkOrderNumber] = " & Me!WorkOrderNumber
DoCmd.OpenReport "MyReport", acViewNormal, , strCriteria
In the event that you have not done much VBA coding of events, here are the
steps needed to put the code "behind a button":
1. Right click on the desired Command Button (let's call it:
CommandRunReport) control and select Properties.
2. When the Properties sheet opens, click the tab labeled Event.
3. In the grid below, locate the row labeled, On Click. It should be blank.
Click anywhere in the white space to the right of the label and you will see
a downward-pointing arrow appear, indicating that this is also a ComboBox.
Click the arrow and select "Event Procedure".
4. Then, notice that there is an ellipsis or three little dots (...) to the
right of the ComboBox. Click the ellipsis and you will open a code window.
You will see that Access has given you a space for entering some code in
this event - it will look something like the following:
Private Sub Private Sub CommandRunReport_Click()
End Sub
5. After the "Private Sub Private Sub CommandRunReport_Click()" line,
insert the code provided earlier.
6. Click the Save icon and close the Microsoft Visual Basic code window.
--
Cheryl Fischer, MVP Microsoft Access
kdc said:
I am very new to Access and have been working forever to try to get a
simple form together for my boss. It's a form which tracks incoming work
orders on vehicles complete with a work order number, date incoming, date
expected back, date sent out for service, where and a few other items. At
the bottom of the form I want a command button that reads print form which
would print only one form. I have read tons of information concerning not
printing forms but rather to set up a report and print that instead. I have
also read alot about VBA's (very confusing information-to me) Printing a
report is fine with me - I have set one up that looks beautiful, now my
biggest dilema. I need step-by-step instructions (remember I'm really dumb)
on how to link the command button on the form to the report. Also, I'm sure
you've figured out I need to print one form or report at time. You have no
idea how much I would appreciate any help given.