Links removed when connecting with the RDC (Automatic SetLocation)
We have a VB6 application that is using the Crystal Report 11 RDC to display reports. When we are connecting the report to the database, one of the SQL links is removed because a field contained in this link is removed from its table. This behaviour gives us problem because the data returned is not good anymore. We would have expected the report to leave the SQL intact and give an error instead.
Is there a way to workaround this behavior.
Ludek Uher replied
OK. So from your description, you have essentially the same issue in the designer and at runtime. The "match for the missing fields" is the field mapping dialog. Now, the designer is nice enough to prompt you with a request to tell it what you want to do with the missing field. Runtime is not. E.G.; you designed the report with a particular field and so the report expects that field to be there. if it's not, it has no idea what to do.
You can force the field mapping dialog at runtime, but you will have to code for it as follows:
' Purpose: Demonstrate how to manipulate Field Mapping
' Field mapping allows the user to change the name of
' a database field that a report field points to if
' that field no longer exists or has been renamed in
' the data source.
' In this sample, the Report_FieldMapping event in the
' code for CrystalReport1 is used to programmatically
' change the mapping of the report fields to a different
' database field.
Dim crApplication As New CRAXDRT.Application
Dim WithEvents m_Report As CRAXDRT.Report
Attribute m_Report.VB_VarHelpID = -1
Dim ReportDB As Database
Dim MappingData As CRAXDRT.FieldMappingData
Dim dbMappingData As CRAXDRT.FieldMappingData
Private Sub CRViewer11_CloseButtonClicked(UseDefault As Boolean)
' Load the Report in the viewer and enable fieldmapping
Private Sub Form_Load()
Set m_Report = crApplication.OpenReport(App.Path & "\FieldMapping.rpt";)
Set ReportDB = m_Report.Database
' Explanation of FieldMappingType:
' 0 - crAutoFieldMapping - Remove all report fields which do not have the same
' name in the new database
' 1 - crPromptFieldMapping - Display a dialog box to get the user to manually
' map the fields
' 2 - crEventFieldMapping - Cause the Report_FieldMapping event to be triggered
m_Report.FieldMappingType = crEventFieldMapping
' Verifying the database or using SetLocation will trigger the Field Mapping event
' Display our report in the viewer
CRViewer11.ReportSource = m_Report
Private Sub cmdAbout_Click()
Private Sub cmdExit_Click()
Private Sub m_Report_FieldMapping(reportFieldArray As Variant, ByVal databaseFieldArray As Variant, UseDefault As Boolean)
' Setting useDefault to True will cancel all fieldmapping
UseDefault = False
' Get the first field in the report ("LastName")
Set MappingData = reportFieldArray(0)
' Setting MappingToFieldIndex to -1 will remove this field from the report
' Set the db field to point to the second database field in the report,
' which happens to be "Last Name"
MappingData.MappingToFieldIndex = 2
MsgBox "Mapped '" & MappingData.FieldName & "' ", vbInformation
' Get the first field in the report ("Total Salary")
Set MappingData = reportFieldArray(3)
Set dbMappingData = databaseFieldArray(12)
' Set the db field to point to the ninth database field in the report,
' which corresponds to "Salary"
MappingData.MappingToFieldIndex = 12
MsgBox "Mapped '" & dbMappingData.FieldName & "' ", vbInformation