Link to home
Start Free TrialLog in
Avatar of THeine
THeine

asked on

Using a command button in MS Access to open Seagate Crystal Reports

I am using Crystal as a query tool to access client  information from our mainframe.  I would like to launch a Crystal report from Access so the user would not have to use both applications (Access and Crystal) in a disjuncted manner.  The effort here is to use a command button to open a pre-established Crystal report that would look up all of the personal information on one of our clients based on policy number.  The query results from Crystal would then populate in a table in the Access database.  The database used here will track complaints for our company.  Instead of having to type all of the client information manually (and avaoiding data integrity errors) we would like to use the protocol described above.  Do you know if this can be done?  Please let me know if you need more information.  Thanks!
Avatar of CareyMBilyeu
CareyMBilyeu

You need to place a command button on your form that references the Crystal OCX that you are going to install on the form as well.

You will set the properties of the OCX to the report that you want to open.

Private Sub MyCommand_Click()
Dim mres As String

CrystalReport0.Reset

With CrystalReport0
.ReportFileName = "E:\My Documents\Test.rpt"
.Destination = crptToWindow
mres = .PrintReport
If mres <> 0 Then
MsgBox "Error", vbOKOnly

End If

End With

End Sub


Now you can do plenty of interesting things here, you can pass parameter values, create a query in the call, set group options, etc.

another example:

Sub CrystalReportConnect1 (mCrystalFormName as Form)

Dim SelectionFormula as String
Dim mres as String

CrystalReport0.Reset    'refers to the OCX

CrystalReport0.WindowTitle "Some Title Here"

With CrystalReport0
.Connect = driver={SQL Server};server="MyServer;uid="Me";pwd="sa";database="database";

.ReportFileName= "C:\My Docs\MyReport1"
.ReplaceSelectionFormula {sp_My StoredProcedure}
.Destination = crptToWindow

mres = .PrintReport

If mres <> 0 Then
Do Something Here

End If

End With


If you're using stored procedures, (much faster), you can also Dim the sp name and then do a With statement to update your record set in Access.

Cheers
THeine:

I have been playing around with the Crystal Reports OCX and can find only several references to the informations that you need;

Start with a command button on your form and place the Crystal Reports Active X control onto the form. Name it CrystalReports1.

We are going to set the code & properties behind the command button, therefore you do not need to set any properties on the OCX other than the name.

Sub MyCommand

Dim myCity As String
myCity = Me!Combo3

CrystalReport1.Reset

With CrystalReport1
.ReportFileName = "E:\My Documents\Test.rpt"
.ParameterFields(0) = "City; " & myCity & ";True"
.Destination = crptToWindow

mres = .PrintReport

If mres <> 0 Then
MsgBox "Error or do something else", vbOKOnly
End If

End Sub

OK, here's what's going on.

I have a combo box on my form to select a city from my Customers table.

I refresh CrystalReports1 by Resetting it.

I tell CrystalReports1 the location and name of the file.

I then pass the parameter "MyCity" to the report by "TRUE".

Then I tell it where to publish the report, in this case, my monitor.

You can tell Crystal Reports to Output the file to and ODBC-MS Access table, including the file that you are working in, BUT, it's not pretty and you will have to perform another query on the data and place the new data into another table. You would be better off coding it to output to a comma seperated value file (Text File) and then perform an import by way of an import specification.

Hereis a sample to set the destination output to a CSV file:

CrystalReport0.Reset


With CrystalReport0
.ReportFileName = "E:\My Documents\Test.rpt"
'.ReplaceSelectionFormula = " & MyCity & "
.ParameterFields(0) = "City; " & myCity & ";True"

.Destination = 2  'crptToFile
.PrintFileCharSepQuote = ""
.PrintFileCharSepSeparator = ","
.PrintFileName = "E:\My Documents\test.csv"
.PrintFileType = 6 - crptCharSep
.ReportFileName = "E:\\My Documents\Test.rpt"


mres = .PrintReport
If mres <> 0 Then
MsgBox "Error", vbOKOnly


Good luck,

Carey

Hey THeine:

Any Luck on this???
for THeine

No comment has been added for the last two months.
So it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial