【问题标题】:Recordset BOF/EOF cannot handle Nulls记录集 BOF/EOF 无法处理 Null
【发布时间】:2016-01-17 17:10:19
【问题描述】:

我有一个表单,想在没有记录时显示一条消息。以下代码中的 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

【问题讨论】:

    标签: ms-access vba recordset


    【解决方案1】:

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

    您无需打开另一个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
    

    【讨论】:

      【解决方案2】:

      每当我需要 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

      【讨论】:

        【解决方案3】:

        你真正需要的是:

        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
        

        【讨论】:

          【解决方案4】:

          为了使用 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`
          

          【讨论】:

          • 谢谢。我之前尝试过 MoveLast。尽管如此,我已经尝试了您的整个代码,但仍然得到相同(错误)的结果。
          • “错误”怎么办?如果没有返回记录,你会得到“没有记录”的显示吗?如果返回任何记录,则您没有指定显示。你有'Option Explicit'并且已经编译了吗?
          • 和我原来的问题一样:它既不返回数字也不显示消息。我有明确的选项并已编译。稍后我会尝试反编译整个前端。
          • 哎呀!虽然我忘记将您的“Set Rs = ...”更改为“Set Rs = dbs.Open...”,但您需要将您的行 'Me.Recordset.RecordCount' 更改为 'rs.RecordCount'
          • 谢谢。完成了,但仍然没有进展,和以前一样。
          猜你喜欢
          • 1970-01-01
          • 2013-06-10
          • 1970-01-01
          • 2014-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多