亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

代碼遍歷MS Access中的所有記錄

代碼遍歷MS Access中的所有記錄

FFIVE 2019-12-15 12:12:03
我需要一個代碼來遍歷表中的所有記錄,以便提取一些數據。除此之外,是否還可以遍歷過濾的記錄并再次提取數據?謝謝!
查看完整描述

3 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

找到了一個很好的代碼,并帶有解釋每個語句的注釋。在-accessallinone找到代碼


Sub DAOLooping()

On Error GoTo ErrorHandler


Dim strSQL As String

Dim rs As DAO.Recordset


strSQL = "tblTeachers"

'For the purposes of this post, we are simply going to make 

'strSQL equal to tblTeachers.

'You could use a full SELECT statement such as:

'SELECT * FROM tblTeachers (this would produce the same result in fact).

'You could also add a Where clause to filter which records are returned:

'SELECT * FROM tblTeachers Where ZIPPostal = '98052'

' (this would return 5 records)


Set rs = CurrentDb.OpenRecordset(strSQL)

'This line of code instantiates the recordset object!!! 

'In English, this means that we have opened up a recordset 

'and can access its values using the rs variable.


With rs



    If Not .BOF And Not .EOF Then

    'We don’t know if the recordset has any records, 

    'so we use this line of code to check. If there are no records 

    'we won’t execute any code in the if..end if statement.    


        .MoveLast

        .MoveFirst

        'It is not necessary to move to the last record and then back 

        'to the first one but it is good practice to do so.


        While (Not .EOF)

        'With this code, we are using a while loop to loop 

        'through the records. If we reach the end of the recordset, .EOF 

        'will return true and we will exit the while loop.


            Debug.Print rs.Fields("teacherID") & " " & rs.Fields("FirstName")

            'prints info from fields to the immediate window


            .MoveNext

            'We need to ensure that we use .MoveNext, 

            'otherwise we will be stuck in a loop forever… 

            '(or at least until you press CTRL+Break)

        Wend


    End If


    .close

    'Make sure you close the recordset...

End With


ExitSub:

    Set rs = Nothing

    '..and set it to nothing

    Exit Sub

ErrorHandler:

    Resume ExitSub

End Sub

記錄集在遍歷數據時具有兩個重要的屬性,EOF(文件末尾)和BOF(文件開始)。記錄集就像表一樣,當您遍歷一個表時,實際上是按順序從一個記錄移到另一個記錄。當您在記錄中移動時,EOF屬性設置為false,但在嘗試通過最后一條記錄后,EOF屬性變為true。反之,BOF屬性的工作原理與此相反。


這些屬性使我們知道何時達到記錄集的限制。



查看完整回答
反對 回復 2019-12-16
  • 3 回答
  • 0 關注
  • 735 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號