如何测试 Recordset 是否为 null 或为空?

问题描述 投票:0回答:6

如何测试 Recordset 是否为 null 或为空?

Dim temp_rst1 As Recordset
Dim temp_rst2 As Recordset
            
Set temp_rst1 = db.OpenRecordset("SELECT * FROM ORDER_DATA WHERE SKUS_ORDERED = '" & curSKU1 & "' AND [ORDER] = " & curOrder)
Set temp_rst2 = db.OpenRecordset("SELECT * FROM ORDER_DATA WHERE SKUS_ORDERED = '" & curSKU2 & "' AND [ORDER] = " & curOrder)
            
If IsNull(temp_rst1) Or IsNull(temp_rst2) Then MsgBox "null"

我正在根据 select 语句打开几个记录集。如果没有记录,IsNull会返回true吗?

vba ms-access recordset
6个回答
47
投票

我会检查“文件结束”标志:

If temp_rst1.EOF Or temp_rst2.EOF Then MsgBox "null"

17
投票

RecordCount 是您想要使用的。

If Not temp_rst1.RecordCount > 0 ...

10
投票

如果

temp_rst1.BOF
temp_rst1.EOF
则记录集为空。对于空记录集、链接记录集或本地记录集始终如此。


3
投票

一个简单的方法就是这样写:

Dim rs As Object
Set rs = Me.Recordset.Clone
If Me.Recordset.RecordCount = 0 then 'checks for number of records
   msgbox "There is no records" 
End if

1
投票

如果不是 temp_rst1 则什么都不是...


0
投票

在这里,我使用 MS Access 2016,并使用以下命令检查记录集字段是否为空:

If (RecordSt.Fields("field_name").Value) Then
' do what you want if there is returned data
Else 
' do what you want if there isn't any data returned by the select
© www.soinside.com 2019 - 2024. All rights reserved.