run report on current record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,
 
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.
 
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
 
Back
Top