views:

3487

answers:

2

Hi there guys.

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.

A: 

What version of crystal are you using?

In the .net world, I generally pass the dataset to the report as emoreau says here.

That way, your connection is set from code rather than crystal and can be stored in a global connection property. However, that's .net. I'm thinking that either the version of crystal you have should have similar functionality OR Emoreau may have an example of how to do it in VB6 in the version you're using.

Hope that get's you started.

asp316
I'm using Crystal Reports 11 (XI) and VB6.For designing the report I needed to add the database in order to layout the fields etc and am now wanting to set the servername and database name via code like you suggest. Reading up on that link now thanks.
+1  A: 

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
MicSim
Thanks for the link it seems exactly what is needed. Do you know if there is a way to leave out the username and password? Our client uses Windows Authentication to connect to the DB. Thanks again
Also when I try the example code I receive a 'User-defined type is not defined' error on 'Dim logInfo As New TableLogOnInfo', any idea what reference I'm missing?