Print Query from form

User avatar
Michael Abrams
4StarLounger
Posts: 574
Joined: 10 Feb 2010, 17:32

Print Query from form

Post by Michael Abrams »

(Access 2003)
I am probably just doubting myself, but this code does what i want it to, but just doesn't seem 'elegant'.

I want to print the results of "Query1" using a button on a form.

Code: Select all

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click

    Dim stDocName As String

    stDocName = "Query1"
    DoCmd.OpenQuery stDocName, acNormal, acEdit
    DoCmd.PrintOut
    DoCmd.Close

Exit_Command0_Click:
    Exit Sub

Err_Command0_Click:
    MsgBox Err.Description
    Resume Exit_Command0_Click

End Sub
What are your thoughts of the code?

Michael

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Query from form

Post by HansV »

It can be done this way, but a more "usual" approach would be to create a report based on the query, and to print that:

Code: Select all

Private Sub Command0_Click()
    On Error GoTo Err_Command0_Click
    DoCmd.OpenReport "rptBasedOnQuery1"
    Exit Sub

Err_Command0_Click:
    If Err <> 2501 Then
        MsgBox Err.Description, vbExclamation
    End If
End Sub
Substitute the name of your report for rptReportBasedOnQuery1.
Best wishes,
Hans

User avatar
Michael Abrams
4StarLounger
Posts: 574
Joined: 10 Feb 2010, 17:32

Re: Print Query from form

Post by Michael Abrams »

Hans,

Same old story here - the suits want to print the query - I explained a report is more appropriate, but.....................

Thanks for the validation !!

Michael