visual basic code to open crystal report


Well i have a Visual Basic 6.0 and Crystal report 8.5 installed.
Now the other user has made verious crystal report for one database.I mean like sales report annual,sales report monthly.

So i want to create a form where i can choose his crystal report and then it will show in other window may be a viewer or something.

SO it may be like .. File -> Open -> and then i browse to select a crystal report file.

and when i press ok it will show me the report or if in the report it is designed like range of month or account number then it will ask the required  field.

I think the main thing is to open different rpt files by just file-open and browse.



Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


The File Open requirement you have can be done using the Common Dialog (you need to add it to your project's Components)

This has various capabilities for displaying Open/Save dialogs, Print dialog, Color, dialog, etc.

You can set the file type to just expect .rpt files so the users will only be browsing for those.

If the report needs variables completed (date range for example) then this is best done using Crystal's parameter functionality which can prompt the user when the report is opened.

harry_cyprusAuthor Commented:
hi rogue,

Well thanks for your reply.

now my point is how do i connect vb form to crystal report viewer.

i have added menu's like file -> open etc but the whole point is opening the crystal report

and showing the database.

well just tell me how can i connect the VB form to crystalreport .

Actually i made a button now if i click that button its opening the default crystal report which is c:\harry\statement.rpt

below is my code.

Form1.CrystalReport1.WindowTitle = "REPORT"
                Form1.CrystalReport1.ReportFileName = "c:\harry\statement.rpt"
        Form1.CrystalReport1.ReplaceSelectionFormula ("")
        Form1.CrystalReport1.WindowState = crptMaximized
        Form1.CrystalReport1.Destination = crptToWindow
        Form1.CrystalReport1.Action = 1

now good thing is as i am clicking that button its opening the crystal report with the parameter and its asking me account number and date etc. which is correct.

but as soon as it finishes taking all input values , and try to show the output i see an error :
run time error 20599
Cannot open sql server"

pls help where do i specify server name



Unfortunately I have Crystal 10 and they have changed things fairly dramatically.  I no longer have the simple CrystalReport1 viewer.

If I remember correctly you have a .Database property?

That said, you shouldn't need to change the database settings if the report is already configured to access the SQL server

Might be best to look at the report in the Crystal Report Designer, do a verify of the databases and make sure it works outside of VB

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

This is how Im going it.

add references to

1) Crystal Reports 8 ActiveX Desighner Run Time Library

Add the following components
1) Crystal Report Viewer Control

Private Sub ShowReport()
Dim sVal As String
Dim sVal1 As String

On Error GoTo Local_Err:

        Set Report = crxApplication.OpenReport("C:\Report.rpt", 1)
        On Error GoTo Local_Err:
        Report.Database.LogOnServer "pdsodbc.dll", "respons", "respons", "sysadm", "sysadm"
        frmReportViewer.Caption = lstReport
Exit Sub

        MsgBox " Err.Number:         " & Err.Number & vbLf _
             & " Error Description:  " & Err.Description & vbLf _
             & " Error Source:       " & Err.Source & vbLf _
             , vbCritical, "Error in Reporting." & Me.Name & ".ShowReport"
End Sub


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry .. forgot some declarations

Dim Report As CRAXDRT.Report
Dim crxApplication As New CRAXDRT.Application
harry_cyprusAuthor Commented:
well well,

i am really confused .

Can somebody just give me a simple example

1.View crystal report from VB.

and open different crystal reports.

my email is

Well i know its not good to give email on this site but i am really in need of clues to finish this VB crystal report story.

the above solution didnt work.
I am still getting the sql server error



It may sound obvious but you have to change the database names, and login info given in VX70's code (the LogOnServer bit) to whatever your database and login is.  Do NOT publish that stuff here.


If you open the report in the normal Crystal Reports Designer (nothing to do with VB) - does the report work?

harry_cyprusAuthor Commented:
BTW the report is working fine in the asp pages and other applications also.

same error

Create an ODBC pointing to your database and use it here...

Report.Database.LogOnServer "pdsodbc.dll", "YourDB", "YourDB", "UserName", "Password"
harry_cyprusAuthor Commented:
ok i s there any software to view crystall reports directly.
i mean like there is powerpoint  and power point viewer
is there  a way i will just double click the .rpt crystal report file and it will show me the report


Here is the code I am using

Here frmCaller is the form from which I am calling the report form. I am having a single report form to display multiple reports (named frmReport).

On the basis of the form that is calling this frmReport, I am assigning a value to the variable iReport (a public variable)

Here are the declarations
Dim crystal As CRAXDDRT.Application  'LOADS REPORT FROM FILE
Dim report As        'HOLDS REPORT
Dim crpParamDefs As CRAXDDRT.ParameterFieldDefinitions
Dim crpParamDef As CRAXDDRT.ParameterFieldDefinition
Public rs As Recordset
Dim rptName  As String
Public frmCaller As Form

This is the load of the form frmReport
Private Sub Form_Load()
    Me.WindowState = 2
End Sub

This is the resize event of the frmReport
Private Sub Form_Resize()
    crvReport.Top = 0
    crvReport.left = 0
    crvReport.Height = ScaleHeight
    crvReport.Width = ScaleWidth
    Call InitializeForm
End Sub

Public Sub InitializeForm()
    Set crystal = New CRAXDDRT.Application
    Set report = New
    Set crpParamDefs = report.ParameterFields
    crvReport.DisplayBorder = False
    crvReport.DisplayTabs = False
    crvReport.EnableDrillDown = False
    crvReport.EnableRefreshButton = False
    crvReport.EnableExportButton = True
'    Set CRXDatabase = CRXReport.Database
    Call CheckReport ' Here I am finding the report to be displayed
    Set report = crystal.OpenReport(Globals.sReportPath & rptName)
    Call AssignParameter 'here I am passing any formula to the report
    report.Database.SetDataSource rs

    crvReport.ReportSource = report
    Do While crvReport.IsBusy            'ZOOM METHOD DOES NOT WORK WHILE
       DoEvents                         'REPORT IS LOADING, SO WE MUST PAUSE
    Loop                                'WHILE REPORT LOADS.
    crvReport.Zoom 94
    'Once the report is displayed, we can then cleanup out objects:
    Set rs = Nothing
    Set crystal = Nothing
    Set report = Nothing

End Sub

Here I am checking the report to be displayed
Public Sub CheckReport()
    Select Case iReport
        Case 1
            rptName = "ItemSold.rpt"
            Exit Sub
        Case 2
            rptName = "HBOItemCons.rpt"
            Exit Sub
        Case 3
            rptName = "HBOWiseInv-Acc.rpt"
            Exit Sub
        Case 4
            rptName = "HBOWiseInv-cons.rpt"
            Exit Sub
        Case 5
            rptName = "HBOCommission.rpt"
            Exit Sub
        Case 6
            rptName = "Achievers.rpt"
            Exit Sub
        Case 7
            rptName = "StateCityWise.rpt"
            Exit Sub
        Case 8
            rptName = "Abr.rpt"
            Exit Sub
    End Select
End Sub

Here I am passing value to the formula field of the report.
Public Sub AssignParameter()
    Select Case iReport
        Case 1
            report.FormulaFields(1).Text = "'" & CStr(frmCaller.txtfrmInvNo.Text) & "'"
            report.FormulaFields(2).Text = "'" & CStr(frmCaller.txtToInvNo.Text) & "'"
        Case 6
            Call MonthYear(CStr(frmCaller.cmbYrMth.Text))
            Month = Month + "-" + CStr(Mid(frmCaller.cmbYrMth.Text, 1, 4))
            report.FormulaFields(1).Text = "'" & Month & "'"
        Case 7
            report.FormulaFields(1).Text = "'" & CStr(frmCaller.txtFmDate.Text) & "'"
            report.FormulaFields(2).Text = "'" & CStr(frmCaller.txtToDate.Text) & "'"
        Case 8
            Call MonthYear(CStr(frmCaller.cmbMonth.Text))
            report.FormulaFields(7).Text = "'" & Month & " - " & CStr(frmCaller.cmbYear.Text) & "'"
'            report.FormulaFields(2).Text = "'" & CStr(frmCaller.txtToDate.Text) & "'"
    End Select
End Sub

Hoping that this will help U.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.