Solved

check for empty recordset

Posted on 2002-06-04
10
707 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now