jpl458
jpl458

Reputation: 615

MS ACCESS: Change default printer via VBA

I have a small app that changes the default printer to an Ithica ticket printer, then prints some info and resets to the default printer. The program will be used at three different work stations each with its own ticket printer.

The following is my current code attempt, which bombs. What is wrong?

"application.Printer = application.Printers(tempTicket)".

Using printer names from "Devices and Printers"

   Private Sub Printfoundcardbtn_Click()
   Dim stEnviron As String
   Dim tempTicket As String
   '----- get the Computer Name
       stEnviron = Environ$("COMPUTERNAME")
'-------------Set ticket printer associated with "COMPUTERNAME"
        Select Case [stEnviron]
    Case "MyComputer"
        tempTicket = "Ithaca USB Printer"
    Case "SSP-REG1"
        tempTicket = "Ithaca USB Printer"
    Case "SSPREGA2"
        tempTicket = "ITherm610"

 End Select


    '-----------------------------------------------------------------
    '-------Set to ticket printer then back to Default---------------------------
    '------------------------------------------------------------------

     Dim prt As Printer
     ' Get current default printer
     Set prt = Application.Printer
     ' Set default printer
     application.Printer = Application.Printers(tempTicket)
     ' Print something, e.g.
     DoCmd.OpenForm "couponPrintFForm"
     '
     DoCmd.PrintOut acPages = 1
          ' Restore original printer
     Set Application.Printer = prt
     DoCmd.Close acForm, "couponPrintFForm", acSave = yes


End Sub

Upvotes: 1

Views: 6363

Answers (1)

andshrew
andshrew

Reputation: 418

Dim prn As Printer
Dim printerFound As Boolean: printerFound = False

For Each prn In Application.Printers
    If prn.DeviceName = tempTicket Then
        Application.Printer = prn
        printerFound = True
        Exit For
    End If
Next

If Not printerFound Then
    ' Handle Printer Not Found Error
End If

You're not assigning the Application.Printer correctly, if you use the above it will look for the printer named in tempTicket and if found will set Access to use that as the default printer. If the printer isn't found then you can handle the error as you require.

Upvotes: 1

Related Questions