user3750023
user3750023

Reputation: 21

List all database fields in use within my Crystal Reports programmatically

If I open up a Crystal Report (v. XI) manually, I can see within the Field Explorer which Database fields are in use (they're checked off). How can I find this out programmatically? I tried cycling through the fields found in the Table object (see code below) using the API, but I then get a list of all database fields within the tables in question, even if they are not used within my report!

For Each t As Table In rptDoc.Database.Tables
            For Each fld As DatabaseFieldDefinition In t.Fields
                debug.print(fld.name)
            Next
        Next

I also tried the following code, but still didn't get all the fields I was expecting to see:

For Each RO As ReportObject In rptDoc.ReportDefinition.ReportObjects
        If RO.Kind = ReportObjectKind.FieldObject Then
            fo = DirectCast(RO, FieldObject)
            Debug.Print(fo.DataSource.Name.ToString)
        End If
    Next

HELP!!!

Upvotes: 0

Views: 2904

Answers (1)

David -
David -

Reputation: 2031

You are almost there :)

Use the UseCount property which represent the number of times the field is in use on the report.

For Each t As Table In rptDoc.Database.Tables
    For Each fld As DatabaseFieldDefinition In t.Fields
        If fld.UseCount > 0 Then
            Debug.Print(fld.Name)
        End If
    Next
Next

or

For Each RO As ReportObject In rptDoc.ReportDefinition.ReportObjects
    If RO.Kind = ReportObjectKind.FieldObject Then
        Dim fo As FieldObject = DirectCast(RO, FieldObject)

        If fo.DataSource.UseCount > 0 Then
            Debug.Print(fo.DataSource.Name.ToString)
        End If
    End If
Next

Upvotes: 1

Related Questions