• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1183
  • Last Modified:

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.
0
sborchers
Asked:
sborchers
  • 4
  • 3
  • 3
  • +1
1 Solution
 
morganahlstromCommented:
Is this what you're looking for? http://www.tek-tips.com/faqs.cfm?fid=5374

BR
0
 
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
0
 
mlmccCommented:
What version of Crystal?  What report presentation method?

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

mlmcc
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
0
 
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.
0
 
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.
0
 
mlmccCommented:
What Crystal references have you added to your application?

mlmcc
0
 
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??
0
 
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
0
 
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.
0
 
sborchersAuthor Commented:
close the question for me....thanks for the help!!!
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now