Printing Report with VB | Newbie | | Join Date: Sep 2008
Posts: 12
| | |
Im trying to print a report through visual basic. I have used many different codes to accomplish what I want to do, but none of them are quite what I am looking for.
When the report prints, I want the Print dialogue to popup, all the code I have tried just prints to the default printer.
Can anyone help?
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Printing Report with VB Quote:
Originally Posted by damonrulz Im trying to print a report through visual basic. I have used many different codes to accomplish what I want to do, but none of them are quite what I am looking for.
When the report prints, I want the Print dialogue to popup, all the code I have tried just prints to the default printer.
Can anyone help? One Method that you can use is to: - Populate a Combo or List Box with all Available Printers.
- Select the specific Printer from the Combo/List Box that will Print the Report.
- Open the Report in Preview Mode and Hidden.
- Set the Report's Printer Object to the chosen Printer.
- Print the Report.
- Close the Report.
| | Newbie | | Join Date: Sep 2008
Posts: 12
| | | re: Printing Report with VB
Ok, but how do I make the system know that the values in the list are linked to the printers?
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Printing Report with VB Quote:
Originally Posted by damonrulz Ok, but how do I make the system know that the values in the list are linked to the printers? Quote:
but how do I make the system know that the values in the list are linked to the printers?
Not exactly sure what you mean, please explain.
| | Newbie | | Join Date: Sep 2008
Posts: 12
| | | re: Printing Report with VB
Well you said make a combo box with the names of the printers in it. How does the system know what that combo box means?
In the code to print, is there a part that looks up the printer form that combo box?
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Printing Report with VB Quote:
is there a part that looks up the printer form that combo box?
Yes, Items 2 thru 4 in Post #2. Do you need to see the code?
| | Newbie | | Join Date: Sep 2008
Posts: 12
| | | re: Printing Report with VB Quote:
Do you need to see the code?
Yes, I am not very good at writing VB
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Printing Report with VB Quote:
Originally Posted by damonrulz Yes, I am not very good at writing VB - Create a Combo Box on your Form and Name it cboPrinters. This Combo Box will contain a listing of all available Printers on your System.
- Copy and Paste the following code to the Open() Event of your Form. It will populate cboPrinters with the List of available Printers previously mentioned.
- Private Sub Form_Open(Cancel As Integer)
-
Dim prn As Printer
-
Dim cbo As ComboBox
-
-
Set cbo = Me![cboPrinters]
-
-
cbo.RowSourceType = "Value List"
-
-
For Each prn In Application.Printers
-
cbo.AddItem prn.DeviceName
-
Next
-
End Sub
- Copy and Paste the following code wherever you deem appropriate, the Click() Event of a Command Button will probably be a good choice. This code will print a Report (rptTest) with the specific Printer you selected from the Combo Box. Set the Value of the Constant conREPORT_TO_PRINT to your Report Name.
- 'Substitute your Report Name here
-
Const conREPORT_TO_PRINT As String = "rptTest"
-
-
'Must select a Printer, correct?
-
If IsNull(Me![cboPrinters]) Then
-
MsgBox "No Printer selected"
-
Exit Sub
-
End If
-
-
'Imperative, Row Source Type must = "Value List"
-
Me![cboPrinters].RowSourceType = "Value List"
-
-
DoCmd.OpenReport conREPORT_TO_PRINT, acViewDesign, , , acHidden
-
-
'Have your Report sent to a specific, selected, Printer
-
Set Reports(conREPORT_TO_PRINT).Printer = Application.Printers.Item(Me![cboPrinters].Value)
-
-
'Fire away!
-
DoCmd.OpenReport conREPORT_TO_PRINT, acViewNormal
-
-
DoCmd.Close acReport, conREPORT_TO_PRINT
- I'm making the assumption that your Access Version is at least 2002, if not, the above code will not work.
| | Newbie | | Join Date: Sep 2008
Posts: 12
| | | re: Printing Report with VB
When I try to print it comes up with an error:
"This document was previously formatted for the printer on 'Printer Name', but that printer isn't available. Do you want to use the default printer 'Default Printer Name'?"
Then I can click 'OK', 'Cancel', 'Setup', or 'Help'. If i click OK, then the report prints to the deafult printer.
This happens even if the deafult printer is selected in the combo box.
Any ideas?
|  | Expert | | Join Date: Apr 2006 Location: Philadelphia
Posts: 5,218
| | | re: Printing Report with VB Quote:
Originally Posted by damonrulz When I try to print it comes up with an error:
"This document was previously formatted for the printer on 'Printer Name', but that printer isn't available. Do you want to use the default printer 'Default Printer Name'?"
Then I can click 'OK', 'Cancel', 'Setup', or 'Help'. If i click OK, then the report prints to the deafult printer.
This happens even if the deafult printer is selected in the combo box.
Any ideas? This is probably because your Report was previously assigned to a Printer which does not currently exist or is unavailable, try: - In Report ==> Design View
- File ==> Print
- Set the Printer Name = the Default Printer
- Click Open
- Now, execute the code
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|