run report on current record

K

Karen53

Hi,

I need to run a report from a form based on the current record I am on in
the form. I only want it to print that one record, not the entire database.
How would I do this?

Thanks,
 
R

Rick B

Asked and answered very often. Please search previous posts before starting
a new thread.


Private Sub cmdPrint_Click()

Dim strWhere As String

If Me.Dirty Then 'Save any edits.

Me.Dirty = False

End If

If Me.NewRecord Then 'Check there is a record to print

MsgBox "Select a record to print"

Else

strWhere = "[ID] = " & Me.[ID]

DoCmd.OpenReport "MyReport", acViewPreview, , strWhere

End If

End Sub



Notes: If your primary key is a Text type field (not a Number type field),
you need extra quotes: strWhere = "[ID] = """ & Me.[ID] & """"

If you want the report to print without preview, replace acViewPreview with
acViewNormal.
 
T

tina

use the Where condition of the OpenReport method to specify the primary key
value of the current record on the form, as

DoCmd.OpenReport "MyReportName", , , "PrimaryKeyField = " &
Me!PrimaryKeyField

substitute the correct report and field names, of course. if your primary
key field is a text datatype, rather than numeric, be sure to enclose the
value in quotes, as

DoCmd.OpenReport "MyReportName", , , "PrimaryKeyField = '" &
Me!PrimaryKeyField & "'"

hth
 
Top