I have attempted to follow your instructions, but Access (2003) tends to
prevent me from entering the text precisely as you have suggested. This is
what I have:
Private Sub PreviewPackingSlipButton_Click()
On Error GoTo Err_PreviewPackingSlipButton_Click
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "Packing Slip", acViewPreview, ,
"DonatedStoreItemID"
Exit_PreviewPackingSlipButton_Click:
Exit Sub
Err_PreviewPackingSlipButton_Click:
MsgBox Err.Description
Resume Exit_PreviewPackingSlipButton_Click
End Sub
Though my command button asks for the record ID, it still opens all Reports
instead of the one associated with the open Record. Perhaps you can spot my
error and recommend a correction.
Thanks . . . Fred
fredg said:
On Sat, 23 Jun 2007 08:09:49 -0500, Fred Worthington wrote:
Greetings:
I have a Report that is linked to a Query. The Report (for example, a
packing slip or form letter) will contain a range of information relevant to
a single account. I want to place a command button in a Form that
will
open
the Report and Print only the current (open) record. How can I do this?
Thanks . . . Fred
Your table should have a unique prime key field.
In my example it is named [RecordID].
On the command button's property sheet write
[Event Procedure]
on the Click event line.
Then click on the little button with 3 dots that will appear on that
line.
When the code window opens, the cursor will be flashing between 2
already existing lines of code.
Between those 2 lines write:
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]
The above assumes a [RecordID] field that is a Number Datatype.
If, however, [RecordID] is Text Datatype, then use:
DoCmd.OpenReport "ReportName", acViewPreview, ,"[RecordID] = '" &
[RecordID] & "'"
as the Where clause.
For clarity, the single and double quotes are..
"[RecordID] = ' " & [RecordID] & " ' "
Change [RecordID] to whatever the actual field name is that you are
using.
See VBA Help files for:
Where Clause + Restrict data to a subset of records'
This is what I suggested:
DoCmd.OpenReport "ReportName", acViewPreview, , "[RecordID] = " &
[RecordID]
This is what you wrote:
DoCmd.OpenReport "Packing Slip", acViewPreview, ,
"DonatedStoreItemID"
What does "DonatedStoreItemID" (all by itself) mean?
You need to tell Access that the value of a certain field in the table
equals the value of certain control on the form. Take a look at my
suggested Where argument again.
"[RecordID = " & [RecordID]
I have no idea what your actual field names are, nor what the field
datatype is, nor what the actual name of the unique control on the
form is. So here is a guess:
The name of the unique field in your table is [DonatedStoreItemID] and
it is a Number datatype; also you have a control on your form named
[DonatedStoreItemID]
Therefore....
DoCmd.OpenReport "Packing Slip", acViewPreview, ,
"[DonatedStoreItemID] = " & [DonatedStoreItemID]
Should open a report showing whatever record is displayed on your
form.