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

SQL Select Distinct Text Fields and Order By - in Excel VBA

Status
Not open for further replies.

Mav3000

Technical User
Jun 20, 2005
113
GB
Hi All,

I've an Excel application which queries a database table, and uses the results to populate a combo box.

However, the table has lots of duplicate values in it - I'd like one of each to appear only, hence a SELECT DISTINCT requirement. I'd also like to sort them in ascending order, so an ORDER BY clause is also needed.

However, the fields are text fields, and when I try to do this, I get the error that I can't do DISTINCT or ORDER BY on text fields.

How can I get around this in order to remove the duplicates and order them alphabetically? This is in VBA so as I understand it, it all needs to be done in a single SELECT statement to return the data.

My query is created dynamically, but essentially looks like this (in duplicated, unordered form):

Code:
SELECT 
  tblMobPageDictionary.Table_name,
  tblMobPageDictionary.Field_name,
  tblMobPageDictionary.Label
FROM 
  tblMobPageDictionary
 


Hi,

I have never seen this before, as I often query using Distinct on text fields.

Try returning your resultset to a sheet, where you could then point your combo to that source to query as desired.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks Skip, That is the approach I am trying at the moment.

I'm putting the recordset onto a worksheet and using an advanced filter to filter the cells specifying unique records only.

I'll then sort it, and set it as the row source on the combo box.
 



This query to an Oracle table executes...
Code:
SELECT DISTINCT COST_CENTERS_MM.ORGANIZATION, COST_CENTERS_MM.MANAGER, COST_CENTERS_MM.RESOURCE_ANALYST
FROM FPRPTSAR.COST_CENTERS_MM
ORDER BY 1, 2, 3
and this from an Access db
Code:
SELECT DISTINCT `Item Info`.Lot, `Item Info`.Cat, `Item Info`.Brand
FROM `Item Info`
ORDER BY 3, 2, 1


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
It would help to know what the data source is. If it is SQL Server then "Text" suggests a memo type field. I would try something like:
Code:
SELECT DISTINCT
  Left(Table_name,100) as TableName,
  Left(Field_name,100) as FieldName,
  Left(Label, 100) as Label
FROM 
  tblMobPageDictionary
ORDER BY 1,2,3

Duane
Hook'D on Access
MS Access MVP
 
Sorry guys, it's an SQL database, connecting via ADO. I'm unsure of the exact field types as I can't access the DB directly - only via ADO and Excel.

At the moment I have my function pulling the fields into a worksheet, then removing duplicates and sorting the remainder, then loading them into a combo box.

Not ideal, but it works well enough.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top