Applies to response #17
NEW (warns about blank form and how everything will be printed otherwise prints selected record):
Create a report for each of your forms and name each one something like rptPrintNameInfo then copy all fields from your form that is associated with the report and paste into the report. While in Design View for the report, check the box in the upper left corner (where the horizontal and vertical rulers meet). On the Property Sheet under the Data tab set the Record Source to the table that holds the information for the form associated with this report.
"rptPrintNameInfo" is whatever report name you want the current data to print to its format.
"[IDNum] = """ & Me![txtIDNum] & """" is whatever field you want the print operation to evaluate in the form. Note there are extra quotation marks because it is a string field being evaluated in this example. The important information here is this form text box field should be unpopulated when no record is selected and populated when a record is selected because this text box field is being evaluated in order to determine what action to perform from the code listed below. The [IDNum] is the bound column name (i.e. record source) of that field. The Me![txtIDNum] is whatever the descriptive name is associated with the form's text box field being evaluated, which is located in Property Sheet > Other tab > Name field.
Private Sub btnPrint_Click()
On Error GoTo btnPrint_Click_Err
Dim Response As Integer
Dim Cancel As Integer
If Me.NewRecord Then
Beep
Response = MsgBox("No record selected for printing. " _
& vbCrLf & "" _
& vbCrLf & "All records for current view will print if you proceed - click OK. " _
& vbCrLf & "" _
& vbCrLf & "Stop the print operation if you do not desire all records - click Cancel.", vbOKCancel, "Warning")
If Response = vbOK Then
'print every record associated with the current form in accordance with report layout
DoCmd.OpenReport "rptPrintNameInfo", acViewNormal
Else
'cancel the print operation
Cancel = True
End If
Else
'print only the selected record of the current form in accordance with report layout
DoCmd.OpenReport "rptPrintNameInfo", acViewNormal, , "[IDNum] = """ & Me![txtIDNum] & """"
End If
btnPrint_Click_Exit:
Exit Sub
btnPrint_Click_Err:
Beep
MsgBox Err.Description, vbOKOnly, ""
Resume btnPrint_Click_Exit
End Sub