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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting Distinct values form a recordset?

Status
Not open for further replies.

plcman

Programmer
Feb 17, 2001
92
GB
Hi

I have a VB6 program that is using data from an MS SQL database.

Just wondered if there was an easy way to retrieve or filter DISTINCT values from a recordset, ie.without using a seperate Select statement

I am creating a chart and need distinct values from the recordset to become the axis lables.
 
Not that I'm aware of. You could either make another recordset using distinct, and spend another round trip to the db, or you could iterate the recordset and pull distinct values.
 
I totally agree with BobRodes suggestions. Look into the dictionary object if you want to check for distinct values.

Swi
 
Well, in effect, if you use the dictionary object, you're still iterating through all the items in the source recordset and using the exists method to check for duplicates. My feeling is that this technique has less steps, though:

1. Sort the recordset on the values you want to pull.
2. On each iteration of the recordset, store the current value in a variable.
3. On each iteration of the recorset but the first one, compare the current value to that in the variable. If not the same, copy the value to the destination.

How does that look?

Bob
 
I would be very interested to see which one is faster. If I get some free time I will try it out.

Swi
 
Thankyou both BobRodes and Swi for your replies.

I have to admit, I have never come across the Dictionary object, i will look into it.

Thanks again.

Regards

Alan Edwards
 
The Dictionary object is certainly worth looking into. It's kind of like a collection object, but lighter and in many ways with more functionality.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top