2014-11-24 119 views
2

我第一次使用.Sort,但從谷歌搜索以下應該工作,但調試打印輸出不排序。Recordset.Sort方法不排序

Function SortByYear(ByVal z As DAO.Recordset) As String 
Dim mySortedRS As DAO.Recordset 
z.Sort = "Year" 
Set mySortedRS = z 
    Do 
     Debug.Print mySortedRS!Year 
     mySortedRS.MoveNext 
    Loop Until mySortedRS.EOF 
Set mySortedRS = Nothing 
End Function 

回答

2

當你

Set mySortedRS = z 

沒有創建一個新的Recordset對象,你只是創建指向現有Recordset對象的新變量。要創建一個新的(排序)Recordset你需要使用

Set mySortedRS = z.OpenRecordset 

欲瞭解更多信息,請參閱

Recordset.Sort Property (DAO)

+0

啊哈!我看到我的錯誤。謝謝...順便說一句,我看到彈出氣球在這裏說,以避免感謝評論...似乎很粗魯,做到這一點。 – Kirk 2014-11-24 13:31:26

+0

@Kirk這只是努力減少評論中的「噪音」。你總是可以通過upvoting和/或[accepting]來說「謝謝」(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)。 – 2014-11-24 13:39:29