Question Locked

Visual Basic - Order By from Database

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi

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
varDptRecSet.MoveFirst
Do While Not varDptRecSet.EOF
lstDpt.AddItem varDptRecSet.Fields("Code") & " - " & varDptRecSet.Fields("Dpt")
DptCount = DptCount + 1
varDptRecSet.MoveNext
Loop
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

Martin

4 replies

by jck

Assuming you are doing this in VB6

Either:

a) add it to your query

or

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

by martinbullock15

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

Thanks

Martin

by tony hopkinson

Select * From DPT Order By ID

PS
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???

This discussion is closed to new replies.