Printing the current record from a sub-form

  • Thread starter Thread starter RandyH
  • Start date Start date
R

RandyH

TIA....

I have a working form with sub-form. I would like to print a selected record
from the sub form. I have made the report, and added a button to the sub
form. What command(s) should I use to get just the selected record to print?
As it's set up now, I get a report with all records. Here is the code I have
behind the button.


Private Sub Print_workorder_Record_Click()
On Error GoTo Err_Print_workorder_Record_Click


stDocName = "Workorder Details Report"
DoCmd.OpenReport stDocName, acPreview

Exit_Print_workorder_Record_Click:
Exit Sub

Err_Print_workorder_Record_Click:
MsgBox Err.Description
Resume Exit_Print_workorder_Record_Click

End Sub
 
Use the WhereCondition of the OpenForm action.

This example assumes the subform's table has a primary key (AutoNumber)
named "ID" that uniquely identifies the record to be printed.

Private Sub Print_workorder_Record_Click()
On Error GoTo Err_Print_workorder_Record_Click
Dim stDocName As String
Dim strWhere As String

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If

If Me.NewRecord Then
MsgBox "Select a record to print."
Else
strWhere = "[ID] = " & Me.[ID]
stDocName = "Workorder Details Report"
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
End If

Exit_Print_workorder_Record_Click:
Exit Sub

Err_Print_workorder_Record_Click:
MsgBox Err.Description
Resume Exit_Print_workorder_Record_Click
End Sub
 
Back
Top