Connecting Tech Pros Worldwide Forums | Help | Site Map

Printing Report with VB

Newbie
 
Join Date: Sep 2008
Posts: 12
#1: Jul 1 '09
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?

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#2: Jul 2 '09

re: Printing Report with VB


Quote:

Originally Posted by damonrulz View Post

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:
  1. Populate a Combo or List Box with all Available Printers.
  2. Select the specific Printer from the Combo/List Box that will Print the Report.
  3. Open the Report in Preview Mode and Hidden.
  4. Set the Report's Printer Object to the chosen Printer.
  5. Print the Report.
  6. Close the Report.
Newbie
 
Join Date: Sep 2008
Posts: 12
#3: Jul 3 '09

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?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#4: Jul 3 '09

re: Printing Report with VB


Quote:

Originally Posted by damonrulz View Post

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
#5: Jul 4 '09

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?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#6: Jul 4 '09

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
#7: Jul 5 '09

re: Printing Report with VB


Quote:
Do you need to see the code?
Yes, I am not very good at writing VB
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#8: Jul 5 '09

re: Printing Report with VB


Quote:

Originally Posted by damonrulz View Post

Yes, I am not very good at writing VB

  1. 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.
  2. 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.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Open(Cancel As Integer)
    2. Dim prn As Printer
    3. Dim cbo As ComboBox
    4.  
    5. Set cbo = Me![cboPrinters]
    6.  
    7. cbo.RowSourceType = "Value List"
    8.  
    9. For Each prn In Application.Printers
    10.   cbo.AddItem prn.DeviceName
    11. Next
    12. End Sub
  3. 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.
    Expand|Select|Wrap|Line Numbers
    1. 'Substitute your Report Name here
    2. Const conREPORT_TO_PRINT As String = "rptTest"
    3.  
    4. 'Must select a Printer, correct?
    5. If IsNull(Me![cboPrinters]) Then
    6.   MsgBox "No Printer selected"
    7.     Exit Sub
    8. End If
    9.  
    10. 'Imperative, Row Source Type must = "Value List"
    11. Me![cboPrinters].RowSourceType = "Value List"
    12.  
    13. DoCmd.OpenReport conREPORT_TO_PRINT, acViewDesign, , , acHidden
    14.  
    15. 'Have your Report sent to a specific, selected, Printer
    16. Set Reports(conREPORT_TO_PRINT).Printer = Application.Printers.Item(Me![cboPrinters].Value)
    17.  
    18. 'Fire away!
    19. DoCmd.OpenReport conREPORT_TO_PRINT, acViewNormal
    20.  
    21. DoCmd.Close acReport, conREPORT_TO_PRINT
  4. 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
#9: Jul 7 '09

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?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#10: Jul 8 '09

re: Printing Report with VB


Quote:

Originally Posted by damonrulz View Post

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:
  1. In Report ==> Design View
  2. File ==> Print
  3. Set the Printer Name = the Default Printer
  4. Click Open
  5. Now, execute the code
Reply