Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if a recordSet is empty? isNull?

Tags:

vba

ms-access

How can you test if a record set is empty?

        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"

I'm opening up a couple of record sets based on a select statement. If there are no records, will IsNull return true?

like image 323
Shubham Avatar asked Jul 22 '11 16:07

Shubham


People also ask

How can you tell if a recordset is empty?

Check the recordset's RecordCount property. If it is zero, you know there aren't any records. Check the recordset's RecordCount property. If it is zero, you know there aren't any records.

Do you need to close recordset?

You can set Recordset to Nothing without needing to call Close, according to official documentation: An alternative to the Close method is to set the value of an object variable to Nothing (Set dbsTemp = Nothing).


5 Answers

I would check the "End of File" flag:

If temp_rst1.EOF Or temp_rst2.EOF Then MsgBox "null"
like image 116
mwolfe02 Avatar answered Oct 02 '22 14:10

mwolfe02


RecordCount is what you want to use.

If Not temp_rst1.RecordCount > 0 ...
like image 21
Tom Studee Avatar answered Oct 02 '22 15:10

Tom Studee


If temp_rst1.BOF and temp_rst1.EOF then the recordset is empty. This will always be true for an empty recordset, linked or local.

like image 21
BillyBob Avatar answered Oct 02 '22 15:10

BillyBob


A simple way is to write it:

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
like image 38
user3959733 Avatar answered Oct 02 '22 15:10

user3959733


If Not temp_rst1 Is Nothing Then ...

like image 29
NDizzle Avatar answered Oct 02 '22 14:10

NDizzle