Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

check for empty recordset

Posted on 2002-06-04
10
Medium Priority
?
729 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

688 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