SOLVED

Print a single record report from a button on a form

New Contributor

Hello, 

I am converting a database from Paradox to Access. I have limited Access experience. I am trying to print a single record on a report launched from a button on the form. I duplicated some code I found on another forum changing the form name, report name and field. When I press the button the report launches but it is blank, no output. The code I used is below. UNIT is the data field for the data and each record has a unique number.  UNIT is not the primary key. 

 

Option Compare Database

 

Private Sub Command394_Click()

End Sub

 

 

Private Sub command399_Click()
DoCmd.OpenReport "FLEET-MACHINE DATA SHEET", acViewPreview, , _
"[UNIT]=Forms!frmFLEET_MASTER_DEVELOP"
End Sub

Private Sub Print_Machine_Detail_Click()

End Sub

 

Blank output below.

gregm57_0-1611943814874.png

 

 

I tried modifying the code:

 

Option Compare Database

 

Private Sub Command394_Click()

End Sub

 

 

Private Sub command399_Click()
DoCmd.OpenReport "FLEET-MACHINE DATA SHEET", acViewPreview, , _
"[UNIT]=Forms!frmFLEET_MASTER!UNIT" '""
End Sub

Private Sub Print_Machine_Detail_Click()

End Sub

 

This launches a box which allows me to input the unit # and output to the report. I would like to code it so the user does not have to input any variable to run the report. 

 

gregm57_2-1611944001461.png

 

gregm57_3-1611944036073.png

 

 

 

 

3 Replies
best response confirmed by gregm57 (New Contributor)
Solution
Try

DoCmd.OpenReport "FLEET-MACHINE DATA SHEET", acViewPreview, , "[UNIT]=" & Me.Unit
One other comment, I usually check if the record needs to be saved prior to opening a report to ensure it is up-to-date, so before using the DoCmd.OpenReport, you may like to add If Me.Dirty = True Then Me.Dirty = False

@Daniel_Pineault 

 

Thanks for the coding, it works better but still requires the user to input the unit number. Is there a way to print the record selected on the form without user input?

 

gregm57_0-1612302760352.png

 

 

I found another issue, I have a number of machines in inventory where the stock number has a dash "-" in it. this causes the button to fail. 

 

gregm57_1-1612302897763.png

 

gregm57_2-1612302938620.png

 

Thanks again for the assistance, it is very helpful for a neophyte such as myself.