Detecting no records in query

Boo

Registered User.
Local time
Yesterday, 16:33
Joined
Aug 30, 2004
Messages
32
Hi all you helpful folks,
Can anybody tell my why the code below does not work?
When I click the cmd button, the only msgBox that shows is the one that starts Didn't work. I want to be able to show the MsgBox stating there are no records. Help??????

Private Sub Command30_Click()

Dim db As Database
Dim rs As Recordset
Dim n As Integer
Dim strSQL As String

Set db = CurrentDb
strSQL = "Select * from [No records Query]"
Set rs = db.OpenRecordset(strSQL)
n = rs.RecordCount

If n < 0 Then
MsgBox "Your query has no records"
Else
MsgBox "Didn't work - Else fired this"
End If
rs.Close
db.Close
Set db = Nothing

End Sub
 
I would try something like this

Dim db As dao.Database
Dim rs As dao.Recordset
Dim n As Integer
Dim strSQL As String

Set db = CurrentDb
strSQL = "Select * from [No records Query]"
Set rs = db.OpenRecordset(strSQL)

If rs.recordcount = 0 Then
MsgBox "Your query has no records"
Else
rs.movelast
MsgBox "Query contains " & rs.recordcount & " records"
End If
rs.Close
db.Close
Set db = Nothing
 
Roy-Vidar It Works!!

I have been struggleing with this for some time.
I am sooooo happy I finally got it. Thank you so much for your time.
Erin
 

Users who are viewing this thread

Back
Top Bottom