Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

programically selecting a printer

Status
Not open for further replies.

humvie

Technical User
Aug 14, 2002
61
CA
I have searched several forums (and read and re-read all the books I have) for an example on how to select a printer during runtime. There are several examples in this forum alone but none do what I think would be the obvious thing.

I have two reports that need to be printed on seperate printers. One is a simple report that currently prints on the default printer. The other is the tricky one. It must print on a thermal printer to print shipping labels. The printer driver is setup and I know it works.

What I idealy would like to have a is to be able to open a form or dialogue, select the report in question and set the printer for that report only. When setting is complete, close and SAVE the settings.

I am using Access XP and have the Office Developer with runtime. Another important fact is that the reports are designed to print the moment the print button is clicked. No preview therefore the option to allow the user to click on the setup button is NOT an option. (hence why I need this done programically).

I have the following code supplied by ........
' From Access 2002 Developer's Handbook, Volume I
' by Litwin, Getz, and Gunderloy (Sybex)
Code:
Private Sub cmdChosen_Click()
    On Error Resume Next
    
    Dim strRptName As String
    strRptName = cboObjects.Value
    
    If chkChangeDefaultPrinter.Value Then
        ' The report must be set to use the default printer
        ' in order for this to work. Otherwise, you must open
        ' the report first, then set its UseDefaultPrinter
        ' property to True, then open it again to print it.
        Set Application.Printer = Application.Printers(cboDestination.ListIndex)
        DoCmd.OpenReport strRptName, View:=acViewNormal
        Set Application.Printer = Nothing
    Else
        DoCmd.OpenReport strRptName, View:=acPreview, WindowMode:=acHidden
        With Reports(strRptName)
            Set .Printer = Application.Printers(cboDestination.ListIndex)
        End With
        DoCmd.OpenReport strRptName, View:=acViewNormal
    End If
End Sub

Private Sub cmdDefault_Click()
    ' Print to the current location.
    ' You must have selected an object (obj) before you end up here!
    On Error Resume Next
    DoCmd.OpenReport cboObjects
    Call DoCleanup
End Sub

Private Sub DoCleanup()
    cboObjects.Value = vbNullString
    lblCurrentDestination.Caption = vbNullString
    chkUseDefault.Value = False
    
    cboObjects.SetFocus
    cmdChosen.Enabled = False
    cmdDefault.Enabled = False
End Sub

Private Sub Form_Load()
    ' Initialize the list type, and
    ' fill in the list of objects.
    Dim prt As Printer
    
    cboObjects.RowSourceType = "Value List"
    Call FillObjectList
    
    For Each prt In Printers
        Me.cboDestination.AddItem prt.DeviceName
    Next prt
    Me.cboDestination = Application.Printer.DeviceName
End Sub

Private Sub GetReportList()
    Dim strOut As String
    Dim objItem As AccessObject
    Dim col As Object
    
    ' Clear the list before adding new items.
    cboObjects.RowSource = vbNullString
    For Each objItem In CurrentProject.AllReports
        cboObjects.AddItem objItem.Name
    Next objItem
End Sub

Private Sub cboObjects_AfterUpdate()
    Dim strRptName As String
    Dim strPrtName As String
    Dim rpt As Report
    
    strRptName = cboObjects.Value
    DoCmd.OpenReport strRptName, View:=acPreview, WindowMode:=acHidden
    With Reports(strRptName)
        strPrtName = .Printer.DeviceName
        chkUseDefault.Value = .UseDefaultPrinter
        chkChangeDefaultPrinter.Enabled = .UseDefaultPrinter
        If Not .UseDefaultPrinter Then
            chkChangeDefaultPrinter.Value = False
        End If
    End With
    
    lblCurrentDestination.Caption = strPrtName
    DoCmd.Close acReport, strRptName
    
    ' Enable the printing buttons.
    cboObjects.SetFocus
    cmdChosen.Enabled = True
    cmdDefault.Enabled = True
End Sub

Private Sub FillObjectList()
    Call GetReportList
    Call DoCleanup
End Sub

For some reason the above code does not do what it says it's suppose do to. Any help would be appreciated.

Regards


 
You might want to look at the code that deals with printer handling that I presented in the following thread:

thread181-680482

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks cajuncenturion,

Had a look at your code and I don;t think it's what I'm looking for.

I don't want to change the default printer as the first report DOES require it to print on default.

The second report must print to the alternative printer. The code I indicated does allow me to do this but for some reason does not SAVE the settings. It needs to be reset everytime the app is opened. Very inefficient.

If you code does save (which I don;t see) please let me know and I'll give a crack at it.

Thanks for your response.

 
You can set the specific printer in the design of your report.

Open your report in design view. Select File - Page Setup. Select the Page Tab at the top and click the Specific Printer radio button at the bottom. Click on the Printer... button and select the Thermal Printer you want to use.

When you distribute this, you need to make sure the printer is setup on the users machine.

I hope this helps.
 
I don't think that's possible. The design app is compiled and installed as an MDE with a back end. The runtime is also included. I don;t have the thermal printer setup on my machine. (please note that this app is not installed on my company network as most cases are in this forum). I designed an application which I am trying to have the user select whatever printer he has (whithout any special setup on my part).

This way when I distribute the app, the user has full control of the printer selections.

Thnaks
 
humvie, have you tried defining the reports always print to the default printer, then in the Report_Open event, set the default printer to the appropriate for that report?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
no but that's one I never thought of. Will give that a shot and let you know. Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top