Solved

check for empty recordset

Posted on 2002-06-04
10
708 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vba find the last empty column 10 88
Access 2013 combo box not working 3 38
Prevent user closing word document opened with VB6 6 67
Macro Excel - Multiple If conditions 2 62
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

930 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