MS Access中的DAO方法

时间:2014-01-23 15:27:25

标签: ms-access access-vba

我无法通过打开Ms Access Query来获取记录数,我使用以下代码。

Private Sub CmdGetData_Click()
Dim WRK As Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim StrSql As String

Set WRK = DBEngine.Workspaces(0)
Set db = CurrentDb
StrSql = "select * from [QrySalePatti]"

Set rs = db.OpenRecordset(StrSql, dbOpenDynaset)

Do While (Not rs.EOF)
  rs.MoveFirst
  rs.MoveLast
  MsgBox rs.RecordCount
Loop

exitRoutine:
If Not (db Is Nothing) Then
     db.Close
     Set db = Nothing
End If

Set WRK = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

您不需要Do While循环来获取RecordCount

Set rs = db.OpenRecordset(StrSql, dbOpenDynaset)
With rs
    If Not (.BOF And .EOF) Then
        .MoveLast
    End If
    MsgBox .RecordCount
End With

但是,如果您的目标只是计算QrySalePatti中的行数,则可以使用SELECT Count(*)查询并读取从中返回的值。

StrSql = "SELECT Count(*) AS row_count FROM [QrySalePatti]"
Set rs = db.OpenRecordset(StrSql)
MsgBox rs!row_count

或者您可以使用DCount表达式。

MsgBox DCount("*", "QrySalePatti")