Solved

check for empty recordset

Posted on 2002-06-04
10
718 Views
Last Modified: 2013-11-25
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
Comment
Question by:philvw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 5

Expert Comment

by:rpai
ID: 7054836
If objrs Is Nothing Or (objrs.EOF And objrs.BOF)
'-- The recordset is Empty
Else
'-- There is data
End If
0
 
LVL 3

Expert Comment

by:jjmartin
ID: 7054838
The count function isn't reliable.  Check for EOF instead.  If it's true, then you have returned no records.
0
 
LVL 5

Expert Comment

by:rpai
ID: 7054856
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 5

Expert Comment

by:kcm76
ID: 7054927
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
 
LVL 5

Expert Comment

by:kcm76
ID: 7054941
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
 
LVL 1

Expert Comment

by:killer5
ID: 7055576
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
 

Author Comment

by:philvw
ID: 7056873
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
 
LVL 5

Accepted Solution

by:
rpai earned 50 total points
ID: 7057005
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7851485
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
 

Author Comment

by:philvw
ID: 7891685
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question