如何用ado recordset的sort 属性对字段按降序排列
编号:QA004320
建立日期: 2001年7月23日 最后修改日期:2001年7月23日
所属类别:
tian:
操作系统: windows
编程工具: vb
问题: 如何用ado recordset的sort 属性对记录集的某个字段按降序排列?(最好有代码实例)
水平: 中级
回答:
看到Sort属性的格式就是:
字段1 DESC 字段2 ASC ...
其中ASC表示升序,DESC表示降序。排在前面的字段优先。
下面是来自MSDN的一个例子:
Public Sub SortX()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
rst.CursorLocation = adUseClient
cnn.Open "DSN=Pubs;Provider=MSDASQL;uid=sa;pwd=;"
rst.Open "SELECT * FROM Authors", cnn, _
adOpenStatic, adLockReadOnly, adCmdText
SortXprint "Initial Order", rst
rst.Sort = "au_lname ASC, au_fname ASC"
SortXprint "Last Name Ascending", rst
rst.Sort = "au_lname DESC, au_fname ASC"
SortXprint "Last Name Descending", rst
rst.Close
cnn.Close
End Sub
Public Sub SortXprint ( title As String, rstp As ADODB.Recordset )
Debug.Print "---------------" & title & "---------------"
Debug.Print "First Name Last Name" & vbCr & _
"---------------------------------------------------"
rstp.MoveFirst
While Not rstp.EOF
Debug.Print rstp!au_fname & " " & rstp!au_lname
rstp.MoveNext
Wend
Debug.Print
End Sub
此问题由李海回答。
| |
|
|
| |
|
|