Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

check for empty recordset

I am trying to check an open recordset to see if it returned any rows or not, if it returns one or more, I want to do something, if it's empty I want to do something else. Here is my code.

    Set db = OpenDatabase(MydbName)
    mySQL = "select DocNum from tblReleasedWOLots where LotNum = '" & txtLotNum.Text & "'"
    Debug.Print mySQL
    rstWO = db.OpenRecordset(mySQL)

    Debug.Print rstWO.count

    If rstWO.count > 0 Then
        'found the LotNum record, now do some stuff here
    Else
        'lot number not found in database, do other stuff
           
    End If
    db.Close

The SQL is set up so that it will only return one record, but even if I put in criteria that will return no records, rstWO.count is always returning '1' (in the debug) so it never hits the 'else' of the if statement.
What am I doing wrong? It's VB6 I'm using and the database is Access97
...

Thanks...

0
philvw
Asked:
philvw
  • 3
  • 2
  • 2
  • +3
1 Solution
 
rpaiCommented:
If objrs Is Nothing Or (objrs.EOF And objrs.BOF)
'-- The recordset is Empty
Else
'-- There is data
End If
0
 
jjmartinCommented:
The count function isn't reliable.  Check for EOF instead.  If it's true, then you have returned no records.
0
 
rpaiCommented:
Also, the Count property returns the number of objects in a collection. What you need is the RecordCount property of the Recordset object to retrieve the number of records.
RecordCount property depends on the type of recordset, and may or may not return the exact number of records in the recordset. Please refer to the above lines of code, which in my opinion, is the best way to check for empty recordset.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kcm76Commented:
objcn.open strConnectString
objrs.open "Select * from Authors",Objcn, 0, 1

if not(objrs.bof and objrs.eof) then
  msgbox "Recordset is NOT EMPTY; IT DOES CONTAIN A RECORD"
else
  msgbox "NO RECORDS"
end if

objrs.close
set objrs = nothing

objcn.close
set objcn = nothing
0
 
kcm76Commented:
oops acc'97.

the below code is DO.



if not(objrs.bof and objrs.eof) then
 msgbox "Recordset is NOT EMPTY; IT DOES CONTAIN A RECORD"
 while not objrs.eof
   msgbox Objrs(0)
   objrs.MoveNext
 wend
else
 msgbox "NO RECORDS"
end if

objrs.close
set objrs = nothing

objDB.close
set objDB = nothing
0
 
killer5Commented:
CHANGE YOUR CODE LIKE THIS...


   Set db = OpenDatabase(MydbName)
   mySQL = "select DocNum from tblReleasedWOLots where    LotNum = '" & txtLotNum.Text & "'"

   Debug.Print mySQL
   rstWO = db.OpenRecordset(mySQL)
   
   if rstWO is nothing then
      msgbox "Recordset is nothing"
      exit sub
   end if

   Debug.Print rstWO.RecordCount


   If rstWO.RecordCount > 0 Then
       'found the LotNum record, now do some stuff here
   Else
       'lot number not found in database, do other stuff
         
   End If
   db.Close
0
 
philvwAuthor Commented:
If I try and use .RecordCount .BOF or .EOF it tells me this "object doesnt support this property or method" so I am obviously trying to open the recordset in the wrong way. I believe this is the root of my problem.

kcm76 show a different way of opening the recordset, but I am not sure of the connection string.
0
 
rpaiCommented:
Please try this:-

Set db = OpenDatabase(MydbName)
   mySQL = "select DocNum from tblReleasedWOLots where LotNum = '" & txtLotNum.Text & "'"
   Debug.Print mySQL
   SET rstWO = db.OpenRecordset(mySQL)

   'Debug.Print rstWO.count

   If rstWO Is Nothing Or (rstWO.EOF And rstWO.BOF)
      'lot number not found in database, do other stuff
   Else
      'found the LotNum record, now do some stuff hereta
   End If

   
   db.Close
0
 
DanRollinsCommented:
Hi philvw,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept rpai's comment(s) as an answer.

philvw, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
philvwAuthor Commented:
The code syntax was correct, but it still didnt solve the problem. What finally did it, was that I was opening the recordset in DOA, and the count being returned is not support, or flat out doesnt work. I had to open the recordset with ADO, and then it functioned. So I had the code right, just not the right connector.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now