Visual Basic - Order By from Database

By martinbullock15 ·

I have created a list box which is populated from a table in a database.

I was wondering if there was a way to order the list by the primary number in the same table?

This is the code which lists the values:

varSQLCmd = "SELECT * FROM Dpt"
varDptRecSet.Open varSQLCmd, varDptConn, adOpenStatic

DptCount = 0
If varDptRecSet.RecordCount > 0 Then
Do While Not varDptRecSet.EOF
lstDpt.AddItem varDptRecSet.Fields("Code") & " - " & varDptRecSet.Fields("Dpt")
DptCount = DptCount + 1
End If

The field in the table which contains the primary ID is named "ID" this is the field i would like the list to be ordered by.

Thanks in advance


This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

What to do

by jck In reply to Visual Basic - Order By f ...

Assuming you are doing this in VB6


a) add it to your query


b) add the column ID as the first column in the list box, and use the Sorted attribute to sort them

Collapse -

Spot on

by martinbullock15 In reply to What to do

Hey Mate

I looked down the properties section of the lst box but must of skipped over it, plus i was looking for Order By and missed the Sorted attribute.

But now it's sorted and coming out in the order it is in the tables



Collapse -


by Tony Hopkinson In reply to Visual Basic - Order By f ...

Select * From DPT Order By ID

MoveFirst is unnecessary, Openn always goes to the first record

Be very wary of RecordCount, some databases will return -1, until you do something with the dataset.

Given you are using it, what the heck is dtpCount for?

It will be recordcount, or in this case lstdpt.Items.Count???

Related Discussions

Related Forums