The printer object is part of the overall Access Objects and is contained within the printers collection. The Printers collection of the Application object contains a collection of Printer objects that represent all of the printers installed on a system. Microsoft Access 2002 makes it possible to programmatically manipulate printer settings of forms and reports through the printers collection.
In the past versions of access (prior to Microsoft Access 2002) the only way of programmatically manipulating printer settings was by using the following properties:
PrtDevMode – Works with a copy of the DevMode data
structure
PrtDevNames - Works with a copy of the DevName data
structure
PrtMip - Works with the data structure that Access
stores with a form or report when printer settings are saved with
those database objects.
As mentioned above the PrtDevMode and PrtDevNames properties both work with copies of the devname/mode data structures that is used by Windows internally to make available information about printer settings and the like. Below is an outline of these structures:
DevMode Data Structure Syntax:
typedef struct _devicemode {
BCHAR dmDeviceName[CCHDEVICENAME];
WORD dmSpecVersion;
WORD dmDriverVersion;
WORD dmSize;
WORD dmDriverExtra;
DWORD dmFields;
union {
struct {
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
};
POINTL dmPosition;
DWORD dmDisplayOrientation;
DWORD dmDisplayFixedOutput;
};
short dmColor;
short dmDuplex;
short dmYResolution;
short dmTTOption;
short dmCollate;
BYTE dmFormName[CCHFORMNAME];
WORD dmLogPixels;
DWORD dmBitsPerPel;
DWORD dmPelsWidth;
DWORD dmPelsHeight;
union {
DWORD dmDisplayFlags;
DWORD dmNup;
}
DWORD dmDisplayFrequency;
#if(WINVER >= 0x0400)
DWORD dmICMMethod;
DWORD dmICMIntent;
DWORD dmMediaType;
DWORD dmDitherType;
DWORD dmReserved1;
DWORD dmReserved2;
#if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
DWORD dmPanningWidth;
DWORD dmPanningHeight;
#endif
#endif /* WINVER >= 0x0400 */
} DEVMODE;
DevNames Data Structure Syntax:
tagDEVNAMES {
WORD wDriverOffset; // file name of driver (without extension)
WORD wDeviceOffset; // device name
WORD wOutputOffset; // device name of physical output medium
WORD wDefault; // DN_DEFAULTPRN if default printer chosen
} DEVNAMES;
The three properties as outline above provide incredible detail about
printer settings not to mention the near complete control you get
when using them, but they are very difficult to control. This difficulty
can be solely attributed to the fact that each property returns all
of its members as a single array of bytes that your code must then
parse. This of course will not do, when the data structures (I mean
just look at them!) are so large. Needles to say, access programmers
complained about the slow and cumbersome task of parsing this data
and wanted a new object model that will make it easier and faster
to work with printer objects. Microsoft responded by introducing the
Printers Collection and Printer object from Microsoft Access 2002.
Note that all versions prior to Access 2002 will not have the Printers Collection available.
The second problem with the PrtDevNames, PrtDevMode and PrtMip properties is that they cannot be used in Microsoft Access applications that is saved in .ade or .mde files, neither can they be used from runtime applications. This is because they are only available when a form or report is opened in Design View. The new Printers Collection and Printer Object has no such problems, they can be used in all views and applications.
Here are some of the names of the members of the Printer Object:
Application – This property is used to access the
application object and related properties.
Item - Returns a specific member of the collection
by position or index.
Count – Returns the number of printers in the collection.
Parent - Returns the parent of the Printers collection,
which is the Application object.
To get a list of all the printers on a particular system, you need to return the printers collection. Because all information about printers is contained in the printers collection, you need to use the printers property of the application object to return the printers collection. Once you have the printers collection, you simply loop through it and list all the printers that is contained therein. The code snippet below list all the currently installed printer names in a combo box:
Dim prt As Access.Printer Dim i As Integer Dim cboPrinter As Access.ComboBox DoCmd.RunCommand acCmdSizeToFitForm Set cboPrinter = Me![cboSelectprinter] cboPrinter.RowSource = "" i = 0 For Each myprinter In Application.Printers cboPrinter.AddItem i & ";" & myprinter.DeviceName If myprinter.DeviceName = Application.Printer.DeviceName Then intDefaultPrinter = i End If i = i + 1 Next cboPrinter = intDefaultPrinter 'Me![cboPaperSize] = 1 End Sub
You can do almost anything once you have the printer collection returned, except that you cannot delete or add printers to the printers collection through code. Adding or removing printers to the collection is done by the user either by installing a new printer or removing one. Another useful feature of the printer collection is that you can set the paper size and orientation through code. Below is another code snippet that shows how this is done. The code assumes that you have combo boxes that enable you to select printers (as per the code above), select a predetermined report (listed in cboSelectReport):
Dim myprinter As Printer Dim strReport As String Set myprinter = Application.Printers(CLng(Me![cboSelectPrinter])) strReport = Me![cboSelectReport] myprinter.PaperSize = Me![cboPaperSize] myprinter.Orientation = Me![fraOrientation] DoCmd.OpenReport strReport, acViewPreview Reports(strReport).Printer = myprinter Exit Sub
Conclusion
There are numerous other features that can accessed through the printer collection, but for now you have the basics and can start developing applications and make use of the powerful features offered by the printers collection.