Network Printer (1 Viewer)

FahadTiger

Member
Local time
Tomorrow, 01:57
Joined
Jun 20, 2021
Messages
115
Hello Sir...
When I use the following code..it gives me an error if the printer is not on the network..
how do I solve this problem
Code:
Private Sub cmdPrint_Click()
On Error GoTo Err_cmdPrint_Click
 Dim stDocName As String
 stDocName = "Report1"
 DoCmd.OpenReport stDocName, acViewNormal
 Reports(stDocName).Printer = "printerName "

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:57
Joined
Oct 29, 2018
Messages
21,473
Complete your error handler.
 

FahadTiger

Member
Local time
Tomorrow, 01:57
Joined
Jun 20, 2021
Messages
115
this is what I do..
and give a message when printer offline
Code:
Private Sub cmdPrint_Click()
On Error GoTo Err_cmdPrint_Click
 Dim stDocName As String
 stDocName = "rpt1"
 DoCmd.OpenReport stDocName, acViewNormal
 Reports(stDocName).Printer = "HP DJ 2103"
Err_cmdPrint_Click:
 Resume Next

End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:57
Joined
Feb 28, 2001
Messages
27,184
First, put the "Reports(stDocname)" line BEFORE you open the report. Afterwards is too late, because Access executes those lines sequentially and synchronously. As you wrote it, the report is being opened BEFORE you set the printer. Therefore, the previously asserted printer is the one being selected.

Second, although it might appear to work correctly, there is a terrible crash potential in what you wrote. This is what I think it SHOULD look like.

Code:
Private Sub cmdPrint_Click()
    On Error GoTo Err_cmdPrint_Click
    Dim stDocName As String
    stDocName = "rpt1"
    Reports(stDocName).Printer = "HP DJ 2103" 
    DoCmd.OpenReport stDocName, acViewNormal
    Exit Sub

Err_cmdPrint_Click:
    Resume Next

End Sub

This is nit-picking - but technically your data flow was wrong. There are those who might say that it would still work, but it does so only because VBA is being forgiving - and because the "NEXT" in "Resume Next" is just an End Sub.

Your previous flow had the code exit by "falling through" your event routine, which included a Resume Next statement. When an error trap occurs, it is NOT in the same context as other event code - because this error trap is one of the few things that can interrupt the flow of an event. (Another is the DoEvents call.) Events do not interrupt each other except in very specific circumstances - but errors can interrupt events because they are not in event context. They are in ERROR context. That's why you use RESUME rather than EXIT to get out of that context. Sometimes it doesn't seem to make a difference, But we have had cases where a non-trivial event routine caused an Access crash.

That little "Exit Sub" line I added covers both cases: (1) An error occurs, so you trap and continue to the Exit Sub (2) No error occurs so your natural flow reachs the Exit Sub. In either case, it just recognizes and keeps separate the two contexts that appear in the routine.
 

FahadTiger

Member
Local time
Tomorrow, 01:57
Joined
Jun 20, 2021
Messages
115
Thank you sir for this nice explanation.
In fact, I took this code from another site..but it gives me the error message when the printer is off the network..
Is there a suggestion when the printer is out of network it gives me a message with that..
not that error!!
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:57
Joined
Sep 21, 2011
Messages
14,299
Yes, you test for that error number, and if true you display your own error message?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:57
Joined
Feb 28, 2001
Messages
27,184
The point of the error routine is that if you are in it, you have triggered an error. Which means the Error object now exists in the context of the error routine. You can test "Err.Number" to see what error number has been triggered. If you get an error number for printer offline, then you can test for it in the error routine and issue an appropriate message via a message box. You can also do other things in the error routine, with the warning that an error routine should not tie up a system for a long time. You should set some program flag or use a "special" target for Resume NoPrinter where you write code in that _prtCmd_Click routine to hand the case for "NoPrinter" in a "normal" event context.

However, this might not be very helpful after the fact. The problem, you see, is probably outside of Access control since "Printer offline" is really a Windows driver-level error, not primarily a VBA or Access error. Therefore, the REAL way to do this is to test the printer first.

I found this link that might be helpful.


If you can try something like this, you can test the printer status before you try to print anything with it.

Here is another thread. NOTE that the first element of the thread is reported as NOT a solution, but a couple of posts down, someone else comes up with a workable solution.

 

FahadTiger

Member
Local time
Tomorrow, 01:57
Joined
Jun 20, 2021
Messages
115
The point of the error routine is that if you are in it, you have triggered an error. Which means the Error object now exists in the context of the error routine. You can test "Err.Number" to see what error number has been triggered. If you get an error number for printer offline, then you can test for it in the error routine and issue an appropriate message via a message box. You can also do other things in the error routine, with the warning that an error routine should not tie up a system for a long time. You should set some program flag or use a "special" target for Resume NoPrinter where you write code in that _prtCmd_Click routine to hand the case for "NoPrinter" in a "normal" event context.

However, this might not be very helpful after the fact. The problem, you see, is probably outside of Access control since "Printer offline" is really a Windows driver-level error, not primarily a VBA or Access error. Therefore, the REAL way to do this is to test the printer first.

I found this link that might be helpful.


If you can try something like this, you can test the printer status before you try to print anything with it.

Here is another thread. NOTE that the first element of the thread is reported as NOT a solution, but a couple of posts down, someone else comes up with a workable solution.

I tried the code in the link..it was giving me messages about the printers on my computer and I was supposed to enter the names of the printers in the combobox on the form..but it gives me this error
1626177242934.png

1626177283115.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:57
Joined
May 7, 2009
Messages
19,242
you have an Enumeration there (PrinterStatus), which is the same as your local variable PrinterStatus.
change the Local PrinterStatus variable to something else, example:

Private Sub Form_Timer()
Dim prtStatus As String
Dim JobStatus As String
Me.txtCheckPrinter = CheckPrinterStatus(prtStatus, JobStatus)
 

FahadTiger

Member
Local time
Tomorrow, 01:57
Joined
Jun 20, 2021
Messages
115
you have an Enumeration there (PrinterStatus), which is the same as your local variable PrinterStatus.
change the Local PrinterStatus variable to something else, example:

Private Sub Form_Timer()
Dim prtStatus As String
Dim JobStatus As String
Me.txtCheckPrinter = CheckPrinterStatus(prtStatus, JobStatus)
I changed before..but give me same Error
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:57
Joined
May 7, 2009
Messages
19,242
ok, go to CheckPrinterStatus function.
then add Byval on each parameter, example:

Public Function CheckPrinter(Byval p1 As String, Byval p2 As String)

note: p1 and p2 are just placeholder, leave as-is whatever is the parameter name in the
function.
 

FahadTiger

Member
Local time
Tomorrow, 01:57
Joined
Jun 20, 2021
Messages
115
plz look to the code ..I dont know how do it
ok, go to CheckPrinterStatus function.
then add Byval on each parameter, example:

Public Function CheckPrinter(Byval p1 As String, Byval p2 As String)

note: p1 and p2 are just placeholder, leave as-is whatever is the parameter name in the
function.
 

Users who are viewing this thread

Top Bottom