Sort data in unbounded combo box

S

Silvio

This code will populate an unbounded combo box using the columns headings of
a query. The question I have is: how can I sort the data in the combo box in
ascending order? Right now, it appears that the order to follow the order on
the columns in the query from left to right. For example if the first column
in the query is called Column10, Columns3, Column5 and so on, my combo box
will display

Column10
Column3
Colums5

What I want to see is

Column3
Column5
Column10

Thank you folks.


Dim curDatabase As Object
Dim strColumnsNames As String
Dim qryUsers As Object
Dim fldColumn As Object

' Get a reference to the current database
Set curDatabase = CurrentDb
' Get a reference to a query named qryUsers
Set qryUsers = curDatabase.QueryDefs("qryUsers")
' Retrieve the name of each column of the quary and
' store each name in the strColumnsNames string
For Each fldColumn In qryUsers.Fields
strColumnsNames = strColumnsNames & fldColumn.Name & ";"
Next

' Set the strColumnsNames string as the data source of the combo box
cboColumnNames1.RowSource = strColumnsNames
 
K

Ken Snell \(MVP\)

You're using a value list, and it's not possible to sort a value list in any
way except by the order in which you add items into the string as you build
it. That means you must figure out the correct sort order after getting the
entire list of field names, and then rebuild the string in the right order.

Is this meant for a situation where you'll always have field names of
ColumnX, where X is a number of some type? Or do you need a solution for
situations where the field names may be different than ColumnX?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top