Solved

Report Printing

Posted on 2004-09-20
10
439 Views
Last Modified: 2008-02-01
I have a button on a form which should print a form.  But, I want to select my printer.  Using the wizard to create my button, it gives me VB code that prints the report using the default printer.

Please Help!
0
Comment
Question by:sbreen
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Author Comment

by:sbreen
ID: 12105342
It should have read... I have a button on a form which should print a REPORT.
0
 
LVL 5

Expert Comment

by:broesi
ID: 12105367
Changing the code from

docmd.OpenReport "rptYourReportName"

to

docmd.OpenReport "rptYourReportName",acViewPreview

will open the report in print preview. There you can select the printer.

HTH,

broesi
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12105437
The code here will allow you to programmatically set a printer:

Option Compare Database   'Use database order for string comparisons
Option Explicit

' Code from:
' Microsoft Access 95 How-To
' (c) 1998 Ken Getz and Paul Litwin
' All rights reserved.

' You may only use this code as part of an application
' that requires its use. You must including this
' notice intact. You may not distribute the code
' as your own work, nor can you distribute the
' code on its own.

Function ahtGetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean

   ' Retrieve the default printer information. Though
   ' the function dutifully returns True if the
   ' values were available, and False otherwise, Windows
   ' really isn't happy without a default printer, and
   ' this situation rarely comes up.

   ' In:
   '     dr: a aht_tagDeviceRec structure to fill in
   ' Out:
   '     Return Value: True if info available, False otherwise.
   '     dr: filled in with default printer information,
   '        if it was available (check the function's return
   '        value).
   '
   ' Comments:
   '     Requires the ahtGetToken() function from basGetToken
   '     Requires the ahtGetINIString() function from basINIFile
   '     Requires type definitions from basPrintTypes
   
   Dim strBuffer As String

   strBuffer = ahtGetINIString("Windows", "Device")
   If Len(strBuffer) > 0 Then
      With dr
         .drDeviceName = ahtGetToken(strBuffer, ",", 1)
         .drDriverName = ahtGetToken(strBuffer, ",", 2)
         .drPort = ahtGetToken(strBuffer, ",", 3)
      End With
      ahtGetDefaultPrinter = True
   Else
      ahtGetDefaultPrinter = False
   End If
End Function

Function ahtSetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean

   ' Set the default printer device in Win.INI

   ' In:
   '     dr: a aht_tagDeviceRec structure to use as
   '        the source of information.
   ' Out:
   '     Return Value: True if set correctly, False otherwise.
   '     If successful, writes a string in the form:
   '        device=HP LaserJet 4,HPPCL5E,LPT1:
   '     to your Win.INI file.
   '
   ' Comments:
   '     Requires the aht_apiWriteProfileString() declaration from basINIFile
   '     Requires type definitions from basPrintTypes

   Dim strBuffer As String

   ' Build up the appropriate string.
   strBuffer = dr.drDeviceName & ","
   strBuffer = strBuffer & dr.drDriverName & ","
   strBuffer = strBuffer & dr.drPort

   ' Now write that string out to WIN.INI.
   ahtSetDefaultPrinter = (aht_apiWriteProfileString("Windows", _
    "Device", strBuffer) <> 0)
End Function
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 12105471
Sorry - you'll need the complete code, available here:
http://www.mvps.org/access/reports/rpt0009.htm

(on the link that says download the code DefaultPrt.zip)
0
 
LVL 1

Expert Comment

by:sonsofperez
ID: 12105521
Another option is that you can go to page setup in the report and tell it to print with a specific printer rather then the default printer.  However, this will only work if you always want the report to go to the same printer.  If you actually need to choose the printer each time then try shanes code.  
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:sbreen
ID: 12105615
I want a dialog box to appear that allows me to choose which printer that I want.  I do not want to "set" my default printer as something else, and then print using the default printer to acheive the result of printing the report.  (The 2 solutions given would work, but are work-arounds)

A bit more:  I actually have two buttons on my form.  The first one prints using the default printer.  (Works Great!) The second is the problem.  It should  give me a dialog box when pressed and ask me which printer I want to use. The reason I am doing this is because I want to create a PDF file from the report, and to do so, I need to select the PDF printer.  (If there is a better way to create the PDF that I am unaware of, please do share. :-)

Thanks -
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 250 total points
ID: 12105666
You could use the command:

DoCmd.RunCommand acCmdPrint

However, you might be better off using the printer code to change to the Acrobat printer, print the report and then change it back.
0
 
LVL 1

Expert Comment

by:sonsofperez
ID: 12105672
Why not just open the report in acViewPreview and then chose the print option?  You will have one minor extra step, but nothing that is very time consuming.

If you chose to change the default printer in code then all you have to do is change it back to your original default printer before closing the code.  I do this with some of my reports and it works fine.
0
 

Author Comment

by:sbreen
ID: 12105684
Thanks for your help everyone!
0
 

Author Comment

by:sbreen
ID: 12105740
If this DB is to be used on multiple machines, and those machines don't have adobe, then it could be a problem.  Because of that, I will use the DoCmd.RunCommand acCmdPrint


It is good to know the other option as well.  Again Thanks.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now