Changing printers in reports at run time

Is it possible to change printer drivers on a specific report at run time?

I need to be able to write an Operator friendly interface to change the printer for a number of reports during the setup of a program.

I am using Access 97.

Any ideas?

Who is Participating?
Alright, if you want the wrappers to prtDevMode and prtDevNames, here they are:

Public Const DN_DEFAULTPRN = &H1

  wDriverOffset As Integer
  wDeviceOffset As Integer
  wOutputOffset As Integer
  wDefault As Integer
End Type

Public Const DM_ORIENTATION = &H1&
Public Const DM_PAPERLENGTH = &H4&
Public Const DM_PAPERSIZE = &H2&
Public Const DM_PAPERWIDTH = &H8&
Public Const DM_SCALE = &H10&
Public Const DM_COPIES = &H100&
Public Const DM_DEFAULTSOURCE = &H200&
Public Const DM_PRINTQUALITY = &H400&
Public Const DM_COLOR = &H800&
Public Const DM_DUPLEX = &H1000&
Public Const DM_YRESOLUTION = &H2000&
Public Const DM_TTOPTION = &H4000&
Public Const DM_COLLATE As Long = &H8000
Public Const DM_FORMNAME As Long = &H10000


Public Const DMPAPER_10X11 = 45                
Public Const DMPAPER_10X14 = 16                
Public Const DMPAPER_11X17 = 17                
Public Const DMPAPER_15X11 = 46                
Public Const DMPAPER_9X11 = 44                  
Public Const DMPAPER_A_PLUS = 57                
Public Const DMPAPER_A2 = 66                    
Public Const DMPAPER_A3 = 8                    
Public Const DMPAPER_A3_EXTRA = 63              
Public Const DMPAPER_A3_TRANSVERSE = 67        
Public Const DMPAPER_A4 = 9                    
Public Const DMPAPER_A4_EXTRA = 53                  
Public Const DMPAPER_A4_PLUS = 60              
Public Const DMPAPER_A4_TRANSVERSE = 55        
Public Const DMPAPER_A4SMALL = 10              
Public Const DMPAPER_A5 = 11                    
Public Const DMPAPER_A5_EXTRA = 64              
Public Const DMPAPER_A5_TRANSVERSE = 61        
Public Const DMPAPER_B_PLUS = 58                
Public Const DMPAPER_B4 = 12                    
Public Const DMPAPER_B5 = 13                    
Public Const DMPAPER_B5_EXTRA = 65              
Public Const DMPAPER_B5_TRANSVERSE = 62        
Public Const DMPAPER_CSHEET = 24                
Public Const DMPAPER_DSHEET = 25                
Public Const DMPAPER_ENV_10 = 20                
Public Const DMPAPER_ENV_11 = 21                
Public Const DMPAPER_ENV_12 = 22                
Public Const DMPAPER_ENV_14 = 23                
Public Const DMPAPER_ENV_9 = 19                
Public Const DMPAPER_ENV_B4 = 33                
Public Const DMPAPER_ENV_B5 = 34                
Public Const DMPAPER_ENV_B6 = 35                
Public Const DMPAPER_ENV_C3 = 29                
Public Const DMPAPER_ENV_C4 = 30                
Public Const DMPAPER_ENV_C5 = 28                
Public Const DMPAPER_ENV_C6 = 31                
Public Const DMPAPER_ENV_C65 = 32              
Public Const DMPAPER_ENV_DL = 27                
Public Const DMPAPER_ENV_INVITE = 47            
Public Const DMPAPER_ENV_ITALY = 36            
Public Const DMPAPER_ENV_MONARCH = 37          
Public Const DMPAPER_ENV_PERSONAL = 38          
Public Const DMPAPER_ESHEET = 26                
Public Const DMPAPER_EXECUTIVE = 7              
Public Const DMPAPER_FANFOLD_US = 39            
Public Const DMPAPER_FOLIO = 14                
Public Const DMPAPER_ISO_B4 = 42                
Public Const DMPAPER_LEDGER = 4                
Public Const DMPAPER_LEGAL = 5                  
Public Const DMPAPER_LEGAL_EXTRA = 51              
Public Const DMPAPER_LETTER = 1
Public Const DMPAPER_LETTER_EXTRA = 50              
Public Const DMPAPER_LETTER_PLUS = 59          
Public Const DMPAPER_LETTERSMALL = 2            
Public Const DMPAPER_NOTE = 18                  
Public Const DMPAPER_QUARTO = 15                
Public Const DMPAPER_RESERVED_48 = 48          
Public Const DMPAPER_RESERVED_49 = 49          
Public Const DMPAPER_STATEMENT = 6              
Public Const DMPAPER_TABLOID = 3                
Public Const DMPAPER_TABLOID_EXTRA = 52              
Public Const DMPAPER_USER = 256

Public Const DMBIN_AUTO = 7
Public Const DMBIN_CASSETTE = 14
Public Const DMBIN_ENVELOPE = 5
Public Const DMBIN_ENVMANUAL = 6
Public Const DMBIN_LARGEFMT = 10
Public Const DMBIN_LOWER = 2
Public Const DMBIN_MANUAL = 4
Public Const DMBIN_MIDDLE = 3
Public Const DMBIN_ONLYONE = 1
Public Const DMBIN_SMALLFMT = 9
Public Const DMBIN_TRACTOR = 8
Public Const DMBIN_UPPER = 1
Public Const DMBIN_USER = 256

Public Const DMRES_DRAFT = (-1)
Public Const DMRES_HIGH = (-4)
Public Const DMRES_LOW = (-2)
Public Const DMRES_MEDIUM = (-3)

Public Const DMCOLOR_COLOR = 2

Public Const DMDUP_SIMPLEX = 1
Public Const DMDUP_VERTICAL = 2

Public Const DMTT_BITMAP = 1
Public Const DMTT_DOWNLOAD = 2          
Public Const DMTT_SUBDEV = 3

Public Const DMCOLLATE_FALSE = 0
Public Const DMCOLLATE_TRUE = 1

Public Const DM_GRAYSCALE = &H1
Public Const DM_INTERLACED = &H2

Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32
        dmDeviceName As String * CCHDEVICENAME
        dmSpecVersion As Integer
        dmDriverVersion As Integer
        dmSize As Integer
        dmDriverExtra As Integer
        dmFields As Long
        dmOrientation As Integer
        dmPaperSize As Integer
        dmPaperLength As Integer
        dmPaperWidth As Integer
        dmScale As Integer
        dmCopies As Integer
        dmDefaultSource As Integer
        dmPrintQuality As Integer
        dmColor As Integer
        dmDuplex As Integer
        dmYResolution As Integer
        dmTTOption As Integer
        dmCollate As Integer
        dmFormName As String * CCHFORMNAME
        dmUnusedPadding As Integer
        dmBitsPerPel As Integer
        dmPelsWidth As Long
        dmPelsHeight As Long
        dmDisplayFlags As Long
        dmDisplayFrequency As Long
End Type
And here are the excerpts of prtDevMode from Win32 API that Access Help lacks:


A set of bit flags that specify whether certain members of the DEVMODE structure have been initialized. If a field is initialized, its corresponding bit flag is set, otherwise the bit flag is clear.  A printer driver supports only those DEVMODE structure members that are appropriate for the printer technology.

The following bit flags are defined, and are listed here with the corresponding structure members:

DM_ORIENTATION          dmOrientation
DM_PAPERSIZE             dmPaperSize
DM_PAPERLENGTH        dmPaperLength
DM_PAPERWIDTH           dmPaperWidth
DM_SCALE                      dmScale
DM_COPIES                    dmCopies
DM_DEFAULTSOURCE    dmDefaultSource
DM_PRINTQUALITY          dmPrintQuality
DM_COLOR                     dmColor
DM_DUPLEX                    dmDuplex
DM_YRESOLUTION          dmYResolution
DM_TTOPTION                 dmTTOption
DM_COLLATE                  dmCollate
DM_FORMNAME              dmFormName
DM_LOGPIXELS               dmLogPixels
DM_BITSPERPEL                dmBitsPerPel
DM_PELSWIDTH                  dmPelsWidth
DM_PELSHEIGHT                 dmPelsHeight
DM_DISPLAYFLAGS             dmDisplayFlags
DM_DISPLAYFREQUENCY   dmDisplayFrequency


Selects the orientation of the paper. This member can be either DMORIENT_PORTRAIT (1) or DMORIENT_LANDSCAPE (2).


Selects the size of the paper to print on. This member can be set to zero if the length and width of the paper are both set by the dmPaperLength and dmPaperWidth members. Otherwise, the dmPaperSize member can be set to one of the following predefined values:

DMPAPER_LETTER            Letter, 8 1/2- by 11-inches
DMPAPER_LEGAL              Legal, 8 1/2- by 14-inches
DMPAPER_A4                    A4 Sheet, 210- by 297-millimeters
DMPAPER_CSHEET           C Sheet, 17- by 22-inches
DMPAPER_DSHEET           D Sheet, 22- by 34-inches
DMPAPER_ESHEET           E Sheet, 34- by 44-inches
DMPAPER_LETTERSMALL  Letter Small, 8 1/2- by 11-inches
DMPAPER_TABLOID           Tabloid, 11- by 17-inches
DMPAPER_LEDGER            Ledger, 17- by 11-inches
DMPAPER_STATEMENT      Statement, 5 1/2- by 8 1/2-inches
DMPAPER_EXECUTIVE        Executive, 7 1/4- by 10 1/2-inches
DMPAPER_A3                     A3 sheet, 297- by 420-millimeters
DMPAPER_A4SMALL          A4 small sheet, 210- by 297-millimeters
DMPAPER_A5                     A5 sheet, 148- by 210-millimeters
DMPAPER_B4                     B4 sheet, 250- by 354-millimeters
DMPAPER_B5                     B5 sheet, 182- by 257-millimeter paper
DMPAPER_FOLIO               Folio, 8 1/2- by 13-inch paper
DMPAPER_QUARTO           Quarto, 215- by 275-millimeter paper
DMPAPER_10X14               10- by 14-inch sheet
DMPAPER_11X17               11- by 17-inch sheet
DMPAPER_NOTE               Note, 8 1/2- by 11-inches
DMPAPER_ENV_9             #9 Envelope, 3 7/8- by 8 7/8-inches
DMPAPER_ENV_10           #10 Envelope, 4 1/8- by 9 1/2-inches
DMPAPER_ENV_11           #11 Envelope, 4 1/2- by 10 3/8-inches
DMPAPER_ENV_12           #12 Envelope, 4 3/4- by 11-inches
DMPAPER_ENV_14           #14 Envelope, 5- by 11 1/2-inches
DMPAPER_ENV_DL            DL Envelope, 110- by 220-millimeters
DMPAPER_ENV_C5            C5 Envelope, 162- by 229-millimeters
DMPAPER_ENV_C3            C3 Envelope,  324- by 458-millimeters
DMPAPER_ENV_C4            C4 Envelope,  229- by 324-millimeters
DMPAPER_ENV_C6            C6 Envelope,  114- by 162-millimeters
DMPAPER_ENV_C65          C65 Envelope, 114- by 229-millimeters
DMPAPER_ENV_B4            B4 Envelope,  250- by 353-millimeters
DMPAPER_ENV_B5            B5 Envelope,  176- by 250-millimeters
DMPAPER_ENV_B6            B6 Envelope,  176- by 125-millimeters
DMPAPER_ENV_ITALY            Italy Envelope, 110- by 230-millimeters
DMPAPER_ENV_MONARCH     Monarch Envelope, 3 7/8- by 7 1/2-inches
DMPAPER_ENV_PERSONAL    6 3/4 Envelope, 3 5/8- by 6 1/2-inches
DMPAPER_FANFOLD_US                        US Std Fanfold, 14 7/8- by 11-inches
DMPAPER_FANFOLD_STD_GERMAN      German Std Fanfold, 8 1/2- by 12-inches
DMPAPER_FANFOLD_LGL_GERMAN      German Legal Fanfold, 8 1/2- by 13-inches


Specifies whether collation should be used when printing multiple copies. (This member is ignored unless the printer driver indicates support for collation by setting the dmFields member to DM_COLLATE.) This member can be be one of the following values:

Value                                Meaning
DMCOLLATE_TRUE           Collate when printing multiple copies.
DMCOLLATE_FALSE         Do not collate when printing multiple copies.
Using DMCOLLATE_TRUE provides faster, more efficient output for collation, since the data is sent to the device driver just once, no matter how many copies are required. The printer is told to simply print the page again.


Windows NT: Specifies the name of the form to use; for example, "Letter" or "Legal". A complete set of names can be retrieved by using the EnumForms function.

Windows 95: Printer drivers do not use this member.


Specifies the number of pixels per logical inch. Printer drivers do not use this member.


Specifies the color resolution, in bits per pixel, of the display device (for example: 4 bits for 16 colors, 8 bits for 256 colors, or 16 bits for 65536 colors). Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.


Specifies the width, in pixels, of the visible device surface. Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.


Specifies the height, in pixels, of the visible device surface. Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.


Specifies the device's display mode. This member can be one of the following values:

Value                          Meaning
DM_GRAYSCALE        Specifies that the display is a noncolor device. If this flag is not set, color is assumed.
DM_INTERLACED        Specifies that the display mode is interlaced. If the flag is not set, noninterlaced is assumed.

Display drivers use this member, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.


Specifies the frequency, in hertz (cycles per second), of the display device in a particular mode. This value is also known as the display device's vertical refresh rate. Display drivers use this member. It is used, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.
When you call the EnumDisplaySettings function, the dmDisplayFrequency member may return with the value 0 or 1. These values represent the display hardware's default refresh rate. This default rate is typically set by switches on a display card or computer motherboard, or by a configuration program that does not use Win32 display functions such as ChangeDisplaySettings.


A device driver's private data follows the public portion of the DEVMODE structure. The size of the public data can vary for different versions of the structure. The dmSize member specifies the number of bytes of public data, and the dmDriverExtra member specifies the number of bytes of private data.
Whew, with all that out of the way, here I will attempt to explain what you need to do to change the printer drivers. Remember, you can change prtDevMode and prtDevNames only in Report design view only.

As you can see that prtDevMode and prtDevNames are Access Strings to the real DEVMODE and DEVNAMES structures in Win32. To use them, we have to pick apart the Strings by using LSet command and an intermediate String-structure type.

Type DEVMODEtype
  ' 512 is arbitrary, we need a number as large as possible
  strDevMode as String * 512
End Type

Dim dm as DEVMODE
Dim dmStr as DEVMODEtype
dmStr.strDevMode = Reports!YourReport.prtDevMode
LSet dm = dmStr

Now you can access and change any member of the DEVMODE structure like in:
    dm.dmOrientation = ....

Now after changes, to put back to prtDevMode, do the reverse:

LSet dmStr = dm
Reports!yourReport.prtDevMode = dmStr.strDevMode
For prtDevNames, it is harder because the first three fields are just pointer-offset to the three c-strings (strings that have Chr$(0) appended to the end): Driver Name, Device Name, and Output Name. We need to build them. And again using LSet and an intermediate String-structure:

  strDevNames as String * 8
End Type

Assuming that strDriver, strDevice, strOutput are the String variables containing the Driver Name, Device Name, and Output Name respectively.

Dim dvStr As DEVNAMEStype

'First offset is always 8
dv.wDriverOffset = 8
dv.wDeviceOffset = dv.wDriverOffset + Len(strDriver) + 1
dv.wOutputOffset = dv.wDeviceOffset + Len(strDevice) + 1
dv.wDefault = 0

LSet dvStr = dv
Reports!yourReport.prtDevNames = dvStr.strDevNames & strDriver & Chr$(0) & strDevice & Chr$(0) & strOutput & Chr$(0)

There you have it, you just change prtDevNames.
Ok, I hope I made all that clear.

Remember to change prtDevMode first before change prtDevNames. Also, the report has to be opened in design view (acViewDesign) mode before you can change and save.

Good luck.
ChrisBerryAuthor Commented:
Edited text of question
Maby this is not what you where looking for but ...
If it is one report and you want to change the printer for it just open the report in preview use ctrl+P and choose from any printer in the network.

The user must choose printer anyway so why not use the built in windows "standards" instead of using tricky API calls that are bound to end up in errors (depending on the users windows configuration)..
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

ChrisBerryAuthor Commented:

I do need to be able to setup the printers for the reports.

The application is to be installed at a number of sites that may have different reports. The majority will use the Default printer but other reports need to be setup for specific printers e.g. most will use the default printer, as LaserJet, but other need to use a dot matrix and others a fax.


ChrisBerryAuthor Commented:
Further to the above:-

I wish to use this in the setup of the application. The user of the software does not need to select the printer after that.


You might want to have a look at prtdevmode and prtdevnames in help.....then again, you might not. It's a nightmare
ChrisBerryAuthor Commented:
I have, it is and that was why I was trying to find out if someone either knew of a easier way or could explain prtdevmode and prtdevnames. Maybe even some routines or wrappers that would take care of it.



There is a 3'rd part software for this purpose. Check out :
 ATTAC Consulting Group. Internet:
The "On the Fly Printing" code module provides the following functionality in printing Access reports:

1.) Allows a user to select to print an Access report to any printer on the system, including fax and network printers through the standard print dialog, which can be called from a command button on a form, without having to compile the report before printing.
2.) The ability to spool multiple reports to a selected printer, despite different paper sizes, paper bins, and orientation.
3.) The ability to specify a filter for the report, when it is sent to print.
4.) For developers, "On the Fly Printing" can now be used with secure reports where the user does not have design permissions.  (See Using/Calling the Functions for more details.)
 How to get it:
 by contacting us by either e-mail (75323.2112 or regular slow mail.

Prices etc..I don't know

ChrisBerryAuthor Commented:
I guess I asked for that. Not quite the type of "wrapper" i was thinking about, slightly tighter would have been better, but I will live with it.



I have to distribute my databases in an .mde.  Is there a way to simply change the paper size or orientation without going into design mode?  It is ridiculous that the reports lose their settings when you move the file, but some sort of work around in .mde runtime would be extremely helpful if anyone knows...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.