How to know emplty recordset (1 Viewer)

rickyfong

Registered User.
Local time
Yesterday, 19:04
Joined
Nov 25, 2010
Messages
199
I have the following VBA code to select certain records from table to recordset, however, sometimes no records existed and movefirst should give error message. Just want to know how to know there got no records in the TRANS2 in case no criteria is matched?? Thanks

Set TRANS2 = db.OpenRecordset("SELECT * FROM TRANSACTION WHERE tcode = 't12345'", dbOpenDynaset)

TRANS2.MoveFirst
 

namliam

The Mailman - AWF VIP
Local time
Today, 04:04
Joined
Aug 11, 2003
Messages
11,695
if Trans2.Recordcount = 0 then msgbox "No records found"
 

Frothingslosh

Premier Pale Stale Ale
Local time
Yesterday, 22:04
Joined
Oct 17, 2012
Messages
3,276
I've always been partial to 'If TRANS2.BOF And TRANS2.EOF Then MsgBox ErrMsg', myself, but I'll admit namliam's method is probably around twice as fast. :)
 

Keith Tedbury

Registered User.
Local time
Today, 03:04
Joined
Mar 18, 2013
Messages
26
Its normally a good idea to check the record count before you do anything with the record set to avoid errors.

My self I do

Set TRANS2 = db.OpenRecordset("SELECT * FROM TRANSACTION WHERE tcode = 't12345'", dbOpenDynaset)

If TRANS2.RecordCount<> 0 then

TRANS2.MoveFirst

End If
 
Last edited:

Users who are viewing this thread

Top Bottom