?
Solved

VB6 - change Crystal Report at Runtime

Posted on 2006-04-13
12
Medium Priority
?
1,145 Views
Last Modified: 2008-01-09
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
Comment
Question by:sborchers
  • 4
  • 3
  • 3
  • +1
11 Comments
 

Expert Comment

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

BR
0
 
LVL 3

Expert Comment

by:Ficus
ID: 16455819
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 16462501
What version of Crystal?  What report presentation method?

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

mlmcc
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:sborchers
ID: 16480605
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
 
LVL 3

Expert Comment

by:Ficus
ID: 16482408
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
 
LVL 3

Expert Comment

by:Ficus
ID: 16482436
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 16483498
What Crystal references have you added to your application?

mlmcc
0
 

Author Comment

by:sborchers
ID: 16487532
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 16494436
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
 

Accepted Solution

by:
sborchers earned 0 total points
ID: 16544252
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
 

Author Comment

by:sborchers
ID: 16566994
close the question for me....thanks for the help!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question