[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

On save, run crystal report and auto print

I have a simple form where users enter data.  When the user presses save, i need to run a crystal report (passing in the current order number), and automatically send to printer.  

One of the design requirements is for the report to print, without any intervention from the user.  (ie. the user does not have to hit enter key, or anything).

I am very familiar with Crystal reports, and a ok with VB.net. However, I have never had to make the two work together.

If anyone could give me some ideas of where to begin, it would be greatly appreciated.
3 Solutions
Hi down0041,
Check out the Crystal Reports SDK, they have some good samples on their website (also included in CR):
It's really not hard, open, connect, print :)) (ok, just a bit more :))
This is what I do. Basically, make sure you had a reference in your project to all libaries begining with "Crystal," there are a lot. I'm assuming you don't want a CrystalReportViewer, but just print it.

All right, the first thing I usually do is create a DataSet (add to project, Dataset). In the schema designer, drag an "element" over, I call the very first one whatever I want the table to be called.  Then add entries, (make sure they are marked with "E") foir each Column you want in the query and make sure the datatype is right.

Once you've done that, go and add a CrystalReport document to project. In the Field Source, or whatever it is called, click on the one that either says Project Data or ADO.NET. Then from there drill down until you find your dataset and insert it as a table, click next. If you know crystal reports then you know how to finish the report.

Now, I don't know what your datasource is. I'm going to assume it is Access, if it is SQL then you will only need to do a Find&Replace between "OleDB" and "SQLClient." I'm assuming you haven't used ADO.NET, if I'm wrong then sorry.

First add an imports statement:
imports System.Data.OleDB

Now in whatever function or whatever place you create this report use code similar to this:

Dim oConn As New OleDBConnection(" .. This is your connection string, make sure it is right")
Dim oCmd As New OleDBCommand
oCmd.Connection = oConn

Dim ds As New MyDataSet   ''' This is actually whatever you named your Dataset's .xsd file earlier
Dim oAdapter As New OleDBDataAdapter(oCmd)

oAdapter.Fill(ds, "MyDataSetTable")  '' Whatever you named the table in the DataSet, probably same as MyDataSet

Dim rpt As New CrystalReport1 '' This is the same as whatever your report document's name is

Now, you can do whatever you want with it, print it or put it in a report or what have you. Hope this helps.
down0041Author Commented:
Thank you.  All the above comments were helpful.  

For the quick printing (without showing a dialog box), the real secret seems to lie in creating the reportDocument object.  From there, you can call the method "printToPrinter".  

For linking my orderID to the report, the secret seems to lie in
Me.CrystalReportViewer1.SelectionFormula = "{ORDERS.TKT_NO} = '" & fldOrderID.Text & "'"

Thanks again for all your input.
Glad i could help


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now