Connecting Tech Pros Worldwide Help | Site Map

Printing Report with VB

  #1  
Old July 1st, 2009, 07:10 PM
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?
  #2  
Old July 2nd, 2009, 12:39 AM
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,162
Provided Answers: 18

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.
  #3  
Old July 3rd, 2009, 05:18 PM
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?
  #4  
Old July 3rd, 2009, 06:43 PM
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,162
Provided Answers: 18

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.
  #5  
Old July 4th, 2009, 08:23 PM
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?
  #6  
Old July 4th, 2009, 11:46 PM
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,162
Provided Answers: 18

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?
  #7  
Old July 5th, 2009, 10:07 AM
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
  #8  
Old July 5th, 2009, 01:31 PM
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,162
Provided Answers: 18

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.
  #9  
Old July 7th, 2009, 12:51 PM
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?
  #10  
Old July 8th, 2009, 12:01 PM
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,162
Provided Answers: 18

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed WIth Printing BN.NET 2005 Siv answers 6 April 2nd, 2006 07:05 PM
VB.NET and CR.NET printing a report with out report viewer Brian Henry answers 2 November 21st, 2005 12:05 AM
Generate PDF with VB code in Acrobat 6.0 Andrew answers 4 November 12th, 2005 08:50 PM
VB 6.0 Questions hmmm... answers 3 July 17th, 2005 09:38 PM