Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty recordset

Status
Not open for further replies.

sf123

Programmer
Oct 9, 2002
30
US
I'm trying to loop through a recordset to display the data. Everytime there are no matches I get an error because the recordset is empty. How can I check if the recordset is empty?
I tried rs.recordcount and it seems to always equal -1 before rs.movefirst but it can't do rs.movefirst if there aren't any records.

Here is some of my code:

If (rs.RecordCount = -1) Then
MsgBox ("There were no matches during that time. Please select another timeframe")
frmChoiceWizard.Hide
frmChoiceWizard.Show
Else
rs.MoveFirst
Any ideas? Thanks!

 
You have to MoveLast before you can use the recordcount
but that will produce an error also so you can either handle an empty recordset with error handling or perhaps try a DO while Not rs.EOF

Just a couple quick ideas ?

Sub Sample1()
On Error Goto ErrorHandler

rs.movefirst
Exit Sub

ErrorHandler:
Select Case Err.Number
Case is = 3021
MsgBox ("There were no matches during that time.
Please select another timeframe")
Exit Sub
Case else
MsgBox Err.number & " " & Err.Description
End Select
End Sub

Sub Sample2()
Dim x as integer
x = 0

Do While Not rs.EOF
x = x + 1
msgbox rs.Fields(0).value
rs.movenext
Loop
if x = 0 then
MsgBox ("There were no matches during that time.
Please select another timeframe")
End if
End Sub
 
Or you can try...

rs.open
if rs.BOF and rs.EOF then
msgbox "No records present"
exit sub
end if

Hth
Borg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top