Error handler

R

RipperT

I have code behind a button that temporarily switches the printer from
default to another printer, prints a report, then switches back to the
default. The trouble is that, for the printer I am temporarily switching to,
some machines have one driver and others use another. I would like to use
error handling to first try one, and if absent, try the other, and if that
one is absent, display a custom message box and exit. I can account for the
first two possiblities, but how can I add the custom error message?

Private Sub cmdPrintRptHU1AWFormal_CC_Click()
On Error GoTo Err_cmdPrintRptHU1AWFormal_CC_Click

Dim stDocName As String
Dim strDefaultPrinter As String
'Get default printer
strDefaultPrinter = Application.Printer.DeviceName
'Change to secondary printer
Set Application.Printer = Application.Printers("HP LaserJet 9050 PCL6")
'Print document
stDocName = "rpt1FormalAW"
DoCmd.OpenReport stDocName, acNormal
'Switch back to default printer
Set Application.Printer = Application.Printers(strDefaultPrinter)

Exit_cmdPrintRptHU1AWFormal_CC_Click:
Exit Sub

Err_cmdPrintRptHU1AWFormal_CC_Click:

Set Application.Printer = Application.Printers("HP LaserJet 9050 PCL 6")
Resume Next

End Sub

Many thanks,

Rip
 
A

Allen Browne

Assuming Access 2002 or 2003, it is possible to examine the properties of
the item in the Printers collection, but a better solution might be to allow
the user to choose one of their printers as the default. Since they will
always have the printer driver for that printer (unless they uninstall it),
that seems like a safe bet.

This sample shows how to create a custom property for the report, and allow
the user to select the printer that the report should remember to use for
the future:
Printer Selection Utility
at:
http://allenbrowne.com/AppPrintMgt.html
 
R

RipperT

I appreciate your response, Allen. I've checked out the custom report you
suggest. However, I would like to stay with my idea of error handling for
several reasons. Mostly because my users neither need nor want to see the
report. They interact with the form and that's all they care to see. Click a
few check boxes, print, done, get on with the day. Therefore, if you could
assist me with the error handling, if in fact that's the best way to do
this. I've created two subs; the first errors out to the second. It's the
best I've been able to do. I just can seem to figure out how to create two
different error handlers within the same sub. This may be one area where
Java wins out with it's try/catch block. My two subs follow. What do you
think? Anyone can weigh in here, of course. Many thanx.

Private Sub cmdPrintRptHU1AWFormal_CC_Click()
On Error GoTo Err_cmdPrintRptHU1AWFormal_CC_Click

Dim stDocName As String
Dim strDefaultPrinter As String
'Get default printer
strDefaultPrinter = Application.Printer.DeviceName
'Change to secondary printer
Set Application.Printer = Application.Printers("HP LaserJet 9050
PCL6")
'Print document
stDocName = "rpt1FormalAW"
DoCmd.OpenReport stDocName, acNormal
'Switch back to default printer
Set Application.Printer = Application.Printers(strDefaultPrinter)

Exit_cmdPrintRptHU1AWFormal_CC_Click:
Exit Sub

Err_cmdPrintRptHU1AWFormal_CC_Click:
Call secondary_handler

End Sub

Private Sub secondary_handler()
On Error GoTo Err_secondary_handler

Set Application.Printer = Application.Printers("HP LaserJet 9050
PCL 6")
'Print document
stDocName = "rpt1FormalAW"
DoCmd.OpenReport stDocName, acNormal
'Switch back to default printer
Set Application.Printer = Application.Printers(strDefaultPrinter)

Exit_secondary_handler:
Exit Sub

Err_secondary_handler:
MsgBox "ITS cannot find the control center printer. " _
& "Ensure there is an appropriate control center printer " _
& "driver installed on this machine. Count was not submitted.", _
vbExclamation, "Printer Not Found"
Resume Exit_secondary_handler

End Sub

--
 
A

Allen Browne

The 2 things that require error handling are actually instance of the same
thing (i.e. attempting to set the printer.) The simplest approach would be
to have a separate function handle the thing that is likely to error.

Private Sub cmdPrintRptHU1AWFormal_CC_Click()
Dim bPrinterOk as Boolean 'Flag if printer successfully set.
Dim strMsg As String 'To append error messages to.

bPrinterOk = UsePrinter("HP LaserJet 9050 PCL6", strMsg)
If Not bPrinterOk Then
bPrinterOk = UsePrinter("HP LaserJet 9050 PCL 6", strMsg)
End If

If bPrinterOk Then
DoCmd.OpenReport "rpt1FormalAW"
Else
MsgBox strMsg, vbExclamation, "Cannot set printer."
End If

'Restore the default printer
Call UsePrinter(vbNullString)
End Sub

Public Function UsePrinter(strPrinter As String, _
Optional strErrMsg As String) As Boolean
On Error GoTo Err_Handler
'Purpose: Make the named printer the active one.
'Arguments: Name of printer to assign. If zero-length string, restore
default.
' Error message string to append to.
'Return: True if set (or already set).

'If no printer specified, restore the default (by unsetting).
If Len(strPrinter) = 0 Then
Set Application.Printer = Nothing
Else
'Do nothing if printer is already set.
If Application.Printer.DeviceName = strPrinter Then
'do nothing
Else
Set Application.Printer = Application.Printers(strPrinter)
End If
End If
UsePrinter = True

Exit_Handler:
Exit Function

Err_Handler:
Select Case Err.Number
Case 5 'Invalid printer.
strErrMsg = strErrMsg & "Invalid printer: " & strPrinter & vbCrLf
Case Else
Call LogError(Err.Number, Err.Description, conMod & ".UsePrinter")
End Select
Resume Exit_Handler
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

OS dependant code 1
PDF Toolbar Button 4
PRINTER LIST ON WINDOWS 2003 NETWORK 1
runtime error 2212 3
Setting Default Printer 2
Difficulties with Microsoft example 1
Code Is Slow 3
Setting Printer by Code?? 3

Top