How do I print ONE chosen report from a form?

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

Guest

I am trying to create a database where I can look up a certain assembly part
in the form and then upon clicking on a Command Button, I can print out the
corresponding report. I have created the form and report and everything
works well, however when I click on the Print Report Command Button (in the
form) it prints out every single report...and I just want the one I selected
in my form. How do I do this???
 
You would need to modify your code something like the following (use your
own control names)...


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.



See also: http://allenbrowne.com/casu-15.html
 
Sorry I was not able to really look at this until this morning. I looked
into my visual basic code. (looked at Properties for the command button,
then "on Click", and then on "Event Procedure") and I was not really sure
where to start because the code it showed was completely different from
yours. Below is what I saw:


On Error GoTo Err_Print_Report_Click

Dim stDocName As String

stDocName = "BAE JSF PACKAGING INSTRUCTIONS"
DoCmd.OpenReport stDocName, acNormal

Exit_Print_Report_Click:
Exit Sub

Err_Print_Report_Click:
MsgBox Err.Description
Resume Exit_Print_Report_Click

End Sub


Am I missing something? Again thank you for your help.
 
I was able to follow the directions that you gave me from the website (sorry,
it was early this morning and I did not notice it), and I seem to be having
some sort of problem. I am getting an error message that reads:

Run-time error '3075':
Syntax error (missing operator) in query expression '([PART NUMBER]
= 2ABC1234-1234)'.

Following is what my code looked like.
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 = "[PART NUMBER] = " & Me.[PART NUMBER]
DoCmd.OpenReport "PACKAGING INSTRUCTIONS", acViewPreview, , strWhere
End If
End Sub
 
Back
Top