Results 1 to 5 of 5
  1. #1
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659

    Changing Printers Code produces an error


    The code below switches printers and then prints a document to the printer. The Print succeeds but I get a error when trying to reset to the printer that was previously set.

    Code:
        Dim stDocName As String
        Dim strDeviceName As String
        Dim x$
        ' Get Current Printer Device and put it in temp
        x$ = Application.Printer.DeviceName
        ' Select the alternate printer for this form
        strDeviceName = "shop foreman printer"
        Set Application.Printer = Application.Printers("shop foreman printer")
        DoCmd.OpenReport "rptSalesOrderShopCopy", acNormal, , "[SalesOrderID]='" & Me.txtSalesOrderID & "'"
        'Reset to default printer
        strDeviceName = x$
        Set Application.Printer = Application.Printers(strDeviceName)
    Thoughts?

  2. #2
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Perceptus -

    Personally, I might try something like...

    'Dim stDocName As String
    Dim strDeviceName As String
    Dim x As Printer
    Dim y As String
    ' Get Current Printer Device and put it in temp
    With x
    y = Application.Printer.DeviceName
    End With
    MsgBox y 'See if it returns the original device name. Comment out MsgBox after test
    ' Select the alternate printer for this form
    strDeviceName = "shop foreman printer"
    Set Application.Printer = Application.Printers("shop foreman printer")
    DoCmd.OpenReport "rptSalesOrderShopCopy", acNormal, , "[SalesOrderID]='" & Me.txtSalesOrderID & "'"
    'Reset to default printer
    strDeviceName = y
    Set Application.Printer = Application.Printers(strDeviceName)

    Hope this helps,

    Jim

  3. #3
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Ive been working on this time permitting. Per your suggestion I was wondering if there is a benefit to using optimized code for the value of Y.

  4. #4
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Perceptus -

    I don't know if there would be a benefit or, not? Do you have a suggestion in mind? Lastly, did you get the code working?

    Jim

  5. #5
    stmoong is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    You can set the printer to print to in the report design, then you don't have to switch in VBA.

    If you need to use VBA, then you should be able to do something like this:
    Code:
        Dim prt as Printer
    
        'Get the current default printer
        Set prt = Application.Printer
    
        'Set the default printer
        Application.Printer = Application.Printer("shop foreman printer")
    
        'Print your report
        DoCmd.OpenReport "rptSalesOrderShopCopy", acNormal, , "[SalesOrderID]='" & Me.txtSalesOrderID & "'"
    
        'Restore original default printer
        Set Application.Printer = prt

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 04-03-2012, 12:00 PM
  2. Replies: 1
    Last Post: 10-18-2011, 07:10 PM
  3. Replies: 4
    Last Post: 05-18-2011, 03:24 PM
  4. Replies: 5
    Last Post: 07-26-2009, 12:13 PM
  5. exporting text produces a number
    By greend in forum Import/Export Data
    Replies: 0
    Last Post: 07-12-2006, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums