Solved

How to print to a specific printer using VBA code in MS Access

Posted on 2006-06-21
10
430 Views
Last Modified: 2012-06-27
I have a report that prints Bills of Lading and must be printed on a specific Bill Of Lading Form. We have several dedicated printers only for these forms.  I look up the user name in a table and get the name of the printer each user should use.  I need to be able to direct the report to this printer.  I DO NOT WANT TO CHANGE THE DEFAULT PRINTER, only for this one form in this one application.
0
Comment
Question by:MGothelf
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 16952850
I think in Access you can specify a printer at report level
(When in design view of the report go to page setup, under the page tab you will see an option to specify a printer for the report,  Access will remember the printer and always print that report on that printer).
0
 
LVL 26

Accepted Solution

by:
dannywareham earned 500 total points
ID: 16952884
0
 

Author Comment

by:MGothelf
ID: 16954374
This is not a DESIGN question.  Every time the report is run, the program must look up the user to get the right printer.  There are over a dozen different users running the application, and 5 different printers.  The printer is different each time, depending on which user is running the program.
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 16957851
When you open the print dialog, there's a list of available printers in a dropdown list at the top.
Is this what you mean?
0
 

Author Comment

by:MGothelf
ID: 16958925
Thats not what I mean.  

I don't want the user to have to select the printer at all.  I have a table of users and each user is in a department and each department has a specific printer assigned.  I want my application to change the printer.  

For example, users 'SMITH' and 'JONES' are in department 'ACCOUNTING', and ACCOUNTING is assigned printer PRT1, but user 'KELLY' is in department 'HR', and HR is assigned printer PRT2.  So when SMITH runs the report it prints on PRT1 and when KELLY runs the report it prints on PRT2, without the users having to do anything.  

I am looking for a way to change the printer assignment in VBA code within the application.

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 17052180
MGothelf,

What you are asking can be accomplished.

Based on you Question History here, my feeling is that you just need to be pointed in the right direction.

In Access 2002 and beyond, the printer object is more "Exposed" in VBA.
For example, the code:
   
    MsgBox Application.Printers.DeviceName

…Will display the name of the current Printer

In other words, with VBA Code, you can see a list of all installed printers, change printers, Manipulate report settings like Margins. (And yes, even: “Choose an Output Device at Runtime) In Access 2000 an earlier you really cant.

Check out the book "Access Cookbook" by Ken Getz ... et al, put out by O'Reilly (ISBN: 0-596-00084-7)

It has an excellent chapter on Access Printer control; the book also has a CD with the examples. You can modify the code and drop it right into your app!

Good luck!

Hope this helps
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17053569
ok, I just answered a question recently on setting printers as I just found a way to do it

Now if u say u need to lookup printers based on a user

what if u created  a table with user's windows logon id in a table
and alongside that, u have their default printer

simple way to get user id (without API)



    Dim tmpPrinter as Printer
    Dim sUser as String
    Dim sPrinterName as String

    sUser = Environ("Username")
    sPrinterName = DLOOKUP("PrinterName","tblUserPrinters","UserName='" & sUser & "'")
   
    Set tmpPrinter = Application.Printer
    Application.Printer = Application.Printers(sPrinterName)

'Do your stuff, then reset back to original printer
    Application.Printer = tmpPrinter


Now I know u said u dont want to change default printer, but as access prints to default printer, a temporary change is what u might have to do



0
 

Author Comment

by:MGothelf
ID: 17053694
Thanks for your help.  The problem was not in the VBA code to get the default, it was how to set it and problem was that many of my users have Access 2000, so many of the simple functions in Access 2002 and 2003 are not available,so I had to do it brute force.  The link provided by dannywareham actually led me pretty close to the solution.
0
 
LVL 26

Expert Comment

by:dannywareham
ID: 17056561
I'm glad you got it sorted, MGothelf

Good luck with the project
:-)
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 17056657
MGothelf,

BTW:

The Access 2000 Devlopers Handbook (desktop) has a great chapter on printer control in Access 2000

Jeff
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

863 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

20 Experts available now in Live!

Get 1:1 Help Now