VB6 - change Crystal Report at Runtime

I am programming in VB6 and using Crystal Reports.  When i make the Report i'm looking at a Access database ( \Testing\123.mdb ).  But at runtime i may look for that database in ( \Real\123.mdb ) or ( \Another\123.mdb ) and so on.  When i run my vb application, it will only look for the database in the \Testing folder.  I want to be able to run the report and point it to the correct folder and database.  The table that i'm looking at will be the same no matter which folder it is in.
Is there a way at runtime to point the crystal report to a different folder and database, then where it was created it from?  I need it formatted at design time because there are logo's and other formatting in this report.
sborchersAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

morganahlstromCommented:
Is this what you're looking for? http://www.tek-tips.com/faqs.cfm?fid=5374

BR
FicusCommented:
Hi, the above link has some good examples of what your asking...

I have pasted in a snippet of code I wrote for a report viewer with VB and CR.  Maybe it will give you some ideas on syntax - then you can google and follow up.  CR documentation can drive a person insane.  Good luck.

-------------------------------------------------------------------------

Private Sub L_SpawnCRView()
'/ Create a new instance of our report viewer and assign the report to use as well as
'/ store some data about this report viewers attribs. to our public type 'CRV_FormState()'

  Dim i As Integer, j As Integer
  Dim LInt_RepCount As Integer
  Dim CRXReport As CRAXDRT.Report
  Dim ff
 
  'On Error GoTo errhandle
   
  If hfg_reports.Rows = 0 Then Exit Sub
 
  Screen.MousePointer = vbHourglass
 
  'Create a new child viewer form - Find free index or increase array
  LInt_RepCount = GF_CRV_Form_New()
 
  'tag and caption for new viewer child form
  CRV_Form(LInt_RepCount).Caption = "Reporte #" & LInt_RepCount & " - " & hfg_reports.TextMatrix(hfg_reports.Row, 1)
 
  'Open our report template (*.rpt) and assign it to our Local report Object (file name based off label.caption)
  Set CRXReport = CRXApplication.OpenReport(GStr_ReportsPath & lbl_reportfilename.Caption)
       
   
  'Bind to current data
  Dim DBTable
  For Each DBTable In CRXReport.Database.Tables
    DBTable.Location = GStr_RootDataPath & GCStr_DBName_MergedPOS
  Next
  CRXReport.ReadRecords
 
  'assign report to Type CRV_FormState (we manipulate this to change scope in viewing)
  Set CRV_FormState(LInt_RepCount).ReportObject = CRXReport
 
  'Set attributes and default scope for this report (eg. tpye = 'otro' (needs to be there regardless of date scope we add later...)
  CRV_FormState(LInt_RepCount).ReportStyle = Val(hfg_reports.TextMatrix(hfg_reports.Row, 3))
  CRV_FormState(LInt_RepCount).ReportPeriod = hfg_reports.TextMatrix(hfg_reports.Row, 7)
  CRV_FormState(LInt_RepCount).DefaultFormula = CRV_FormState(LInt_RepCount).ReportObject.RecordSelectionFormula
       
  'Assign report source to our report viewer
  CRV_Form(LInt_RepCount).CRViewer.ReportSource = CRV_FormState(LInt_RepCount).ReportObject
 
 
  'place report 'sub-titles'
  For Each ff In CRV_FormState(LInt_RepCount).ReportObject.FormulaFields
    If ff.Name = "{@Criteria}" Then
      ff.Text = Chr$(34) & CRV_FormState(LInt_RepCount).DefaultFormula & Chr$(34)
    End If
    If ff.Name = "{@FirstDate}" Then
        ff.Text = Chr$(34) & "*" & Chr$(34)
    End If
    If ff.Name = "{@LastDate}" Then
        ff.Text = Chr$(34) & "*" & Chr$(34)
    End If
    If ff.Name = "{@Period}" Then
      ff.Text = Chr$(34) & "NA" & Chr$(34)
    End If
  Next
   
  'View the report
  CRV_Form(LInt_RepCount).CRViewer.ViewReport
 
  Do While CRV_Form(LInt_RepCount).CRViewer.IsBusy = True
    DoEvents
  Loop
 
 
  '/ Depending on the attributes of this report display the apropriate controls on the form
  If CRV_FormState(LInt_RepCount).ReportPeriod = True Then
    CRV_Form(LInt_RepCount).cmb_GroupPeriodType.Visible = True
    CRV_Form(LInt_RepCount).cmb_GroupPeriodType.ListIndex = 2
    CRV_Form(LInt_RepCount).lbl_period.Visible = True
   Else
    CRV_Form(LInt_RepCount).cmb_GroupPeriodType.Visible = False
    CRV_Form(LInt_RepCount).lbl_period.Visible = False
  End If
 
  If CRV_FormState(LInt_RepCount).ReportStyle > 0 Then
    CRV_Form(LInt_RepCount).chk_alldates.Visible = True
    'TO DO: Set DTPs to High and Low Ranges of availabe Records
    '       (not the stupid default value of the date when control was placed on form
   Else
    CRV_Form(LInt_RepCount).chk_alldates.Visible = False
  End If

 
  Screen.MousePointer = vbDefault
     
  Set CRXReport = Nothing
mlmccCommented:
What version of Crystal?  What report presentation method?

The information provided above is great if you are using the RDC.

mlmcc
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

sborchersAuthor Commented:
Crystal 8.5
I got the report to work, but when i try to send a parameter it won't work.  Any suggestions on how to pass a parameter to the report?  I have included the code i'm using in VB6.

Public crxChart As New crChart
    crxChart.Database.Tables(1).Location = strDir
    crxChart.DiscardSavedData
    crxChart.ParameterFields.GetItemByName("ReqNumber").AddCurrentValue strChartReqNumber
    frmCrystal.CRViewer2.ReportSource = crxChart
    frmCrystal.CRViewer2.ViewReport
    frmCrystal.CRViewer2.Zoom 65
FicusCommented:
In the code I pasted above I set criteria by setting the SelectionFormula

CRV_FormState(LInt_RepCount).DefaultFormula = CRV_FormState(LInt_RepCount).ReportObject.RecordSelectionFormula

'where recordSelectionFormula is something like "active = true".  If i recall its some SQL like CR syntax.  There are 'record selection' and 'report selection', or something like that.  ReportPeriod is another useful one.

If you are using the Selection formula already in the creation of your static report (.rpt), you just concot it with whatever the user selected parameter you need.
FicusCommented:
Sorry, you know the code above really does not show what im talking about...

'set the record selection critera for report and refresh
LStr_DateScope = CRV_FormState(Me.Tag).DefaultFormula

 LStr_DateScope = LStr_DateScope " AND " & "{cajas.madestamp} >= DATE(" & Format$(DTP_Start.Value, "yyyy,mm,dd") & ") AND {cajas.madestamp} <= DATE(" & Format$(DTP_stop.Value, "yyyy,mm,dd") & ")"

CRV_FormState(Me.Tag).ReportObject.RecordSelectionFormula = LStr_DateScope

 
  '//Adjust the date period of our groups summary
  LInt_NewGC = Me.cmb_GroupPeriodType.ListIndex
  For i = 1 To CRV_FormState(Me.Tag).ReportObject.Areas.Count
    If CRV_FormState(Me.Tag).ReportObject.Areas(i).Kind = crGroupFooter Or CRV_FormState(Me.Tag).ReportObject.Areas(i).Kind = crGroupHeader Then
      'We are in the Group Header or Group Footer Area
      If CRV_FormState(Me.Tag).ReportObject.Areas(i).GroupCondition <> crGCAnyValue Then
        'The current group condition is of a date type (it is not 'AnyValue' which in our case is anything besides a date type)
        CRV_FormState(Me.Tag).ReportObject.Areas(i).GroupCondition = LInt_NewGC
      End If
    End If
  Next i

  '//refresh
  Me.CRViewer.Refresh


I do a loop thru all the report sections, resetting conditions for each.
mlmccCommented:
What Crystal references have you added to your application?

mlmcc
sborchersAuthor Commented:
I'm not using a RDC connection, would Ficus information still work?  

I've added, Crystal Report Viewer Control, Crystal Reports 8.5 ActiveX Designer Run Time Library, and Crystal Reports 8.5 Designer Design and Runtime Library are the references.

I'm confused on why the report will work without a parameter but won't with the parameter??
mlmccCommented:
Not sure why but try this first.

Remove the Crystal Reports 8.5 Designer Design and Runtime Library from the references.  Unless you are using the royalty-required calls you don't need it.  It is actually Crystal Reports 8.5 ActiveX Designer Run Time Library with added capability thus only one is needed and generally only Crystal Reports 8.5 ActiveX Designer Run Time Library.

By the way, that is the RDC.

Are you running with the RPT files oor did you embed the reports in the application as DSR files?

mlmcc
sborchersAuthor Commented:
Public crxChart As New crChart
    crxChart.Database.Tables(1).Location = strDir
    crxChart.DiscardSavedData
    crxChart.ParameterFields.GetItemByName("ReqNumber").AddCurrentValue strChartReqNumber
    frmCrystal.CRViewer2.ReportSource = crxChart
    frmCrystal.CRViewer2.ViewReport
    frmCrystal.CRViewer2.Zoom 65


That code does work, i had to change the way i was connecting to the database.  Then it worked fine, i'm not sure if ficus's code would work.  But the code i have worked very easily.  Thanks for all the help.

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
sborchersAuthor Commented:
close the question for me....thanks for the help!!!
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
Visual Basic Classic

From novice to tech pro — start learning today.