Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field names instead of field positions 1

Status
Not open for further replies.

samson123

Programmer
Jun 17, 2005
79
US
I am using crystal Reports XI. Access database 2000

I have in my VB code
If text = "Customer Name"
report.RecordSortFields.Add report.Database.Tables(1).Fields(2), crAscendingOrder

endif

The above code works fine.

But I would like to use the following so that I do not have to remember the field names position in the report

report.RecordSortFields.Add report.Database.Tables("Customer").Fields("Customer_Name"), crAscendingOrder

When I write this code with field nanmes it gives error message"Type Mismatch".

I do not understand. What is wrong in the code?



 
Surprisingly , nobody has responded to my question..

My problem is how do I know What is report.database.Tables(1) ?

Is Tables(1) the first table that I added to the report when creating the report ?

The db Expert shows the tables sorted by name. I am assuming the Tables(1) does not pick the first one from the alphabetically sorted list.

Please advise
 
The table indexes should be in the order the tables were added to the report (according to the help files anyway). They can also be accessed by name, or index, so either of the follwing should be valid:

rpt.Database.Tables("Customer")
or
rpt.Database.Tables(0)

To locate a specific table, you might do something like this:
Code:
Dim crxDBTable As CRAXDRT.DatabaseTable
For Each crxDBTable In Report.Database.Tables
  If crxDBTable.Name = "Customer" Then
    'do what you need
  End If
Next
To carry this on further, to set your sort field:
Code:
Dim crxDBTable As CRAXDRT.DatabaseTable
For Each crxDBTable In Report.Database.Tables
  If crxDBTable.Name = "Customer" Then
    Report.RecordSortFields.Add Report.Database.Tables(1).Fields.GetItemByName("Customer_Name"), crAscendingOrder
  End If
Next

So, your original statement using the field name probably would have worked if you'd used the GetItemByName method:
[tt]
report.RecordSortFields.Add report.Database.Tables("Customer").Fields.GetItemByName("Customer_Name"), crAscendingOrder[/tt]

-dave
 
Dave,

Thanks for you response...

This works...
Report.RecordSortFields.Add Report.Database.Tables(1).Fields.GetItemByName("Customer_Name"), crAscendingOrder


This does not work..Gives error "Type Mismatch"
Report.RecordSortFields.Add Report.Database.Tables("Customer").Fields.GetItemByName("Customer_Name"), crAscendingOrder

What do you think could be wrong ? I would like to use Table Name instead of the index.
 
That's odd. In my initial testing, using the table name instead of its index was working for me, but I created a new test project, and I'm also getting a "Type Mismatch" error.

I think your best bet would be to loop through the DatabaseTables collection to try to find the index of the table in question, etc...

-dave
 
Dave,

Thanks for trying to help me.. Unfortunately I will have to use the index approach...

I will give you a star for showing me the direction..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top