按降序排序数据库记录

时间:2022-06-11 22:12:55

I'm relatively New To programming and VB and Would like some help. I am trying to list runners in the order of their speed (Fastest First), the data is from a database, i am able to sort the data in ascending order but cannot get them in descending order, is there a way to do that simply, if not what is the simplest way to reverse the Listboxes.

我是编程和VB的新手,想要一些帮助。我试图按照速度(最快速度)的顺序列出跑步者,数据来自数据库,我能够按升序对数据进行排序,但不能按降序排列,有没有办法简单地做到这一点,如果不是最简单的方法来反转列表框。

My Code At the Moment:

我的代码:

Private Sub RunningRanking()
    Dim DBconn As New ADODB.Connection 'This is a connection string   declaration'
    Dim Record As New ADODB.Recordset ' This is a connection to the record
    DBconn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & _
               "Data source = '" & Application.StartupPath & "\Users Database.mdb'") 'this is opening the connection between the 
    'system and database
    With Record
        .CursorLocation = ADODB.CursorLocationEnum.adUseClient
        .Open("Trainee", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        .Sort = "RunningSpeed" 'sorting the database record in ascending order
        Do Until Record.EOF
            LstBoxName.Items.Add(Record.Fields("First Name").Value)
            LstBoxSpeed.Items.Add(Record.Fields("RunningSpeed").Value)
            .MoveNext()
        Loop
    End With
    Record.Close()
    DBconn.Close()
End Sub

1 个解决方案

#1


2  

You can use the desc keyword to sort the data descending:

您可以使用desc关键字对数据降序进行排序:

.Sort = "RunningSpeed desc"

Consider using an SQL query to fetch the data instead of opening the table. The database is generally much faster at sorting the data than the Recordset object.

考虑使用SQL查询来获取数据而不是打开表。数据库排序数据通常比Recordset对象快得多。

#1


2  

You can use the desc keyword to sort the data descending:

您可以使用desc关键字对数据降序进行排序:

.Sort = "RunningSpeed desc"

Consider using an SQL query to fetch the data instead of opening the table. The database is generally much faster at sorting the data than the Recordset object.

考虑使用SQL查询来获取数据而不是打开表。数据库排序数据通常比Recordset对象快得多。