Recordset BOF / EOF无法处理Null

时间:2016-01-17 17:10:19

标签: ms-access access-vba recordset

我有一个表单,想要在没有记录时显示消息。以下代码中的SQL不显示任何记录(Null)(目前应该如此)。该功能无法正常工作。它既不返回数字也不显示消息。如果我将函数放在有记录的表单中,它会准确计算它们。

Public Function NumRecs() As Integer

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("SELECT tblClient.ClientName, tblInvoices.SentToPayer, [Adjustment]+[MyFee]+[DBSFee] AS TotFees, tblClient.ClientID, tblDisclosure.ApplicantForenames, tblDisclosure.AppEmail " & _
                             "FROM ((tblInvoiceDetails INNER JOIN tblDisclosure ON tblInvoiceDetails.DiscLookup = tblDisclosure.ID) INNER JOIN tblInvoices ON tblInvoiceDetails.InvoiceLookup = tblInvoices.ID) INNER JOIN ((tblOfficerDetails INNER JOIN tblOfficers ON tblOfficerDetails.OfficerLookup = tblOfficers.ID) INNER JOIN tblClient ON tblOfficerDetails.ClientLookup = tblClient.ClientID) ON tblInvoices.AppLookup = tblClient.ClientID " & _
                             "WHERE (((tblInvoices.DatePaid) Is Null)) ")

If Not rs.BOF And Not rs.EOF Then
    NumRecs = Me.Recordset.RecordCount
Else
    DisplayMessage ("No records.")
    NumRecs = 0
End If

rs.Close
Set rs = Nothing

End Function

4 个答案:

答案 0 :(得分:0)

为了使用DAO获取记录计数,您需要MoveLast。另外,请尝试更改“EOF”。检查(见下文):

Public Function NumRecs() As Integer
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL as String

strSQL = "SELECT tblClient.ClientName, tblInvoices.SentToPayer, [Adjustment]+[MyFee]+[DBSFee] AS TotFees, tblClient.ClientID, tblDisclosure.ApplicantForenames, tblDisclosure.AppEmail " & _ 
"FROM ((tblInvoiceDetails INNER JOIN tblDisclosure ON tblInvoiceDetails.DiscLookup = tblDisclosure.ID) INNER JOIN tblInvoices ON tblInvoiceDetails.InvoiceLookup = tblInvoices.ID) INNER JOIN ((tblOfficerDetails INNER JOIN tblOfficers ON tblOfficerDetails.OfficerLookup = tblOfficers.ID) INNER JOIN tblClient ON tblOfficerDetails.ClientLookup = tblClient.ClientID) ON tblInvoices.AppLookup = tblClient.ClientID " & _ 
"WHERE (((tblInvoices.DatePaid) Is Null))"

Set dbs = CurrentDB
Set rs = dbs.OpenRecordset(strSQL)

If Not rs.EOF Then 
    rs.MoveLast         ' ADD THIS LINE
    NumRecs = rs.RecordCount 
Else 
    DisplayMessage ("No records.") 
    NumRecs = 0 
End If

rs.Close 
Set rs = Nothing
dbs.Close
Set dbs = Nothing

End Function`

答案 1 :(得分:0)

每当我需要DAO中的记录计数时,我总是MoveLast然后是MoveFirst

Dim db as DAO.Database
Dim rst as DAO.Recordset
Dim strSQL as string

strSQL = ""  ' your query here

Set db=CurrentDB()
   Set rst=db.OpenRecordset(stSQL,dbOpenDynaSet)
      With rst
         If NOT (.EOF and .BOF) Then   ' There are records to be had
            Dim iRecCount as Integer
            .MoveLast: .MoveFirst       
              ' DAO typically requires the all records before the count
              ' count is correct
            iRecCount = .RecordCount

         Else    ' There are NOT records to be had
            ' ADD YOUR MESSAGE HERE FOR NO RECORDS.
         End If
         .Close
      End with
   Set rst=nothing
Set db=nothing

我可以选择在VBA外部构建查询并添加参数。这就是为什么我知道我的查询产生了我期望的结果。然后,您可以将查询作为CurrentDB()对象的QueryDefs的对象引用。然后将参数作为QueryDef的属性进行处理。

以下是Allen Browne对Recordsets的精彩解读。 http://allenbrowne.com/ser-29.html

答案 2 :(得分:0)

  

“我有一个表单,想要在没有记录时显示一条消息。”

您可以在不打开其他DAO.Recordset的情况下完成该任务。只需使用已存在的RecordsetClone

Private Sub Form_Load()
    Dim lngRowCount As Long

    lngRowCount = 0
    With Me.RecordsetClone
        If Not (.BOF And .EOF) Then
            .MoveLast
            lngRowCount = .RecordCount
        End If
    End With
    MsgBox lngRowCount & " records"
End Sub

答案 3 :(得分:0)

你真正需要的是:

Public Function NumRecs() As Integer

    Dim rs As DAO.Recordset

    Set rs = Me.RecordsetClone
    If rs.RecordCount = 0 Then
        DisplayMessage ("No records.")
    Else
       rs.MoveLast
       NumRecs = rs.RecordCount
    End If
    rs.Close    
    Set rs = Nothing

End Function