Setting Crystal Report data source at runtime

crystal-reportsdatabasevb6

When creating my Crystal Report I obviously set up a database and server connection that I use for development.

What I want to do now in my VB application is to dynamically set the database and server name to use with the reports. I have these values as the strings varServer and varDatabase.

Anyone know how to go about doing this?

Thanks in advance.

P.S I have tried several online solutions but am having trouble with VB6.

Best Answer

This link has all information you want to know.

UPDATE: Here is a minimum working sample for integrated authentication with SQL Server. You should use the ConnectionProperties of the table object to set connection parameters.

Dim app As New CRAXDDRT.Application
Dim rpt As CRAXDDRT.Report
Dim tbl As CRAXDDRT.DatabaseTable
Dim tbls As CRAXDDRT.DatabaseTables

Set rpt = app.OpenReport("C:\report\repotest.rpt")

For Each tbl In rpt.Database.Tables
    tbl.ConnectionProperties.DeleteAll
    tbl.ConnectionProperties.Add "Provider", "SQLOLEDB"
    tbl.ConnectionProperties.Add "Data Source", "localhost"
    tbl.ConnectionProperties.Add "Initial Catalog", "testdb"
    tbl.ConnectionProperties.Add "Integrated Security", "True"   ' cut for sql authentication
    'tbl.ConnectionProperties.Add "User Id", "myuser"   ' add for sql authentication
    'tbl.ConnectionProperties.Add "Password", "mypass"  ' add for sql authentication
Next tbl

'This removes the schema from the Database Table's Location property.
Set tbls = rpt.Database.Tables
For Each tbl In tbls
    With tbl
        .Location = .Name
    End With
Next

'View the report
Viewer.ReportSource = rpt
Viewer.ViewReport
Related Topic