Hello Fellow Loungers,
I have a database I developed for our Home Owners Association and it is severely locked down because one of the main users is computer challenged.
After completing the final lock down the graphic below is what the user sees. The printer Icon uses the Windows Default printer. However, there are a couple of reports which need to be sent to a PDF printer so they can be posted on our website. I didn’t realize this problem until the first time they tried to do it after the final lockdown. After some poking around the Access VBA help files I came up with a solution which y’all may find useful.
Add this code to a standard module:
Option Compare Database Option Explicit Public strDfltPrt As String '*** Save Default Printer Name for resetting *** ' +---------------------+ +----------+ '--------------------------| SwitchPrinters() |-----------------| 07/30/10 | ' +---------------------+ +----------+ 'Called by : Form_Open() - From any form! ' Form_Close() - From any form! 'Calls : N/A 'Function Calls: N/A 'Globals Used : N/A Sub SwitchPrinters(zSwitchToPtr As String) Dim prtName As Printer Dim iPrtNo As Integer iPrtNo = 0 For Each prtName In Application.Printers If prtName.DeviceName = zSwitchToPtr Then Exit For Else iPrtNo = iPrtNo + 1 End If Next prtName '*** Uncomment next 2 lines for testing or visual verification of switch *** ' MsgBox "Printer Selected: " & Format(iPrtNo, "#0") & _ ' " " & Application.Printers(iPrtNo).DeviceName Application.Printer = Application.Printers(iPrtNo) End Sub '*** SwitchPrinters ***
Add the following code to each Report that needs to go to a different printer:
Option Compare Database Option Explicit ' +---------------------+ +----------+ '--------------------------| Report_Open() |-----------------| 07/30/10 | ' +---------------------+ +----------+ 'Calls : SwitchPrinters 'Function Calls: N/A 'Globals Used : strDfltPrt Private Sub Report_Open(Cancel As Integer) strDfltPrt = Application.Printer.DeviceName SwitchPrinters "PrimoPDF" '*** Subistitute Desired Printer Name Here *** End Sub ' +---------------------+ +----------+ '--------------------------| Report_Close() |-----------------| 07/30/10 | ' +---------------------+ +----------+ 'Calls : SwitchPrinters 'Function Calls: N/A 'Globals Used : strDfltPrt Private Sub Report_Close() SwitchPrinters strDfltPrt End Sub
Or If you want to allow interactive selection of printers or just for testing purposes you can add this code to a standard module:
' +----------------------------+ +----------+ '-------------------|UserSelectPrinterByNumber() |-----------------| 07/30/10 | ' +----------------------------+ +----------+ 'Called by : Form_Open() - From any form! ' Form_Close() - From any form! 'Calls : N/A 'Function Calls: N/A 'Globals Used : N/A Sub UserSelectPrinterByNumber() Dim prtName As Printer Dim iPrtNo As Integer Dim zPrtMsg As String iPrtNo = 0 zPrtMsg = "No. Printer Name" & vbCrLf For Each prtName In Application.Printers zPrtMsg = zPrtMsg & Format(iPrtNo, "#0") & " " & prtName.DeviceName & vbCrLf iPrtNo = iPrtNo + 1 Next prtName iPrtNo = Val(InputBox(zPrtMsg, "Printer Selection Dialog")) MsgBox "Printer Selected: " & Format(iPrtNo, "#0") & _ " " & Application.Printers(iPrtNo).DeviceName Application.Printer = Application.Printers(iPrtNo) End Sub '*** UserSelectPrinterByNumber ***
If anyone has ideas to improve this code I’d love to hear from you.