Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > RecordCount Function Problem.w


Reply
 
Thread Tools Display Modes
  #1  
Old 01-19-2003, 10:51 PM
dpdsouza dpdsouza is offline
Regular
 
Join Date: Oct 2002
Location: India
Posts: 80
Default RecordCount Function Problem.w


I am using the RecordCount function to catch the EOF BOF / NO record exist error
using the following statement.

if rs.RecordCount=0 then Exit Sub

But Sometimes I find that the program still gives the EOF BOF/no record error. When I debug the statement I found that VB is counting the records at -1 through there were no records exist.

I had to modified my statement as

if rs.RecordCount <=0 then Exit Sub


I am confused how can VB count the records as -1

PLS HELP.

rgds,
Dennis.
Reply With Quote
  #2  
Old 01-19-2003, 11:49 PM
Rezner's Avatar
Rezner Rezner is offline
C# Lover

* Expert *
 
Join Date: Jan 2002
Location: 00-80-C8-C3-2E-52
Posts: 1,899
Default

You need to specify the cursor position and locktype. Here's an example:
Code:
RS.Open strSQL, adpCon, adOpenStatic, adLockOptimistic, 1
If you're using DAO, then you need to move to the end of the recordset before accessing the RecordCount property.
__________________
"Man is still the best computer we can put aboard a spacecraft...and the only one that can be mass produced with unskilled labor." - Wernher von Braun
Reply With Quote
  #3  
Old 01-20-2003, 01:07 AM
joe77s joe77s is offline
Centurion
 
Join Date: Feb 2002
Location: Indonesia
Posts: 170
Default

or u can use this, when you open your recordset

RS.Open strSQL, adpCon, adOpenKeyset, adLockOptimistic, AdCmdText

if u use adOpenforward, you need to MoveFirst and the MoveLast to know RecordCount
__________________
every day is a Gift, how great Thou art

www.corpussoft.com
Reply With Quote
  #4  
Old 01-20-2003, 07:22 AM
lebb's Avatar
lebb lebb is offline
Disillusioned Code Poet

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Tennessee, USA
Posts: 12,808
Default

Sorry, joe77s, that is incorrect. If you MoveLast on an adOpenForwardOnly recordset, you then can no longer access any of the other fields. The RecordCount property of an ADO recordset (unlike DAO) will be accurate for a bookmarkable recordset, without needing to traverse it fully first. Note that server-side cursors and forward-only and dynamic (hope I got that list right) recordsets are not bookmarkable and will always return a RecordCount of -1.
__________________
Laura

Ita erat quando hic adveni.
Reply With Quote
  #5  
Old 01-20-2003, 10:10 PM
dpdsouza dpdsouza is offline
Regular
 
Join Date: Oct 2002
Location: India
Posts: 80
Default

Hi Rezner, Joe & Icebb

Thanks a lot for taking part in the discussion. I am opening the recordset as follows.

Private Sub Form_Load()
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset
Set rs3 = New ADODB.Recordset
cn.Open "DSN=payroll;uid=;pwd=;"
rs1.Open "select * from acctyear where yr = '" & frmEnterSal.cmbYear.Text & "'", cn, adOpenDynamic, adLockOptimistic
rs.Open "select * from salary where yr = '" & frmEnterSal.cmbYear.Text & "'order by empno", cn, adOpenDynamic, adLockOptimistic
rs2.Open "select * from emp where status ='Active' order by empno", cn, adOpenKeyset, adLockOptimistic
rs3.Open "emp", cn, adOpenKeyset, adLockOptimistic, adCmdTable
While Not rs2.EOF
cmbEmpNo.AddItem rs2!empno
rs2.MoveNext
Wend
DisableButtons "D"
rs1.MoveLast
txtYear.Text = rs1!yr

If rs1.RecordCount = 0 Then Exit Sub
If rs2.RecordCount = 0 Then Exit Sub
If rs.RecordCount = 0 Then Exit Sub
ShowData
End Sub

Please tell me how I should have opened the recordset in the above code.

Regards,
Dennis.
Reply With Quote
  #6  
Old 01-21-2003, 06:05 AM
lebb's Avatar
lebb lebb is offline
Disillusioned Code Poet

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Tennessee, USA
Posts: 12,808
Default

Before your cn.Open statement, add the line
Code:
cn.CursorLocation = adUseClient
and change all your adOpenDynamic constants to adOpenStatic.

If you want to check to see if a given recordset is bookmarkable, and thus would return a valid recordcount, you can use
Code:
YourRS.Supports(adBookmark)
If this returns True, then you will get a valid RecordCount.
__________________
Laura

Ita erat quando hic adveni.
Reply With Quote
  #7  
Old 01-21-2003, 08:08 AM
reboot's Avatar
reboot reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,612
Default

And all of this will be moot if you check for an empty recordset correctly with

If rs.BOF And rs.EOF

instead of

If rs1.RecordCount = 0
__________________
~ Quod non mortiferum, fortiorem me facit ~

Avatar by lebb
Reply With Quote
  #8  
Old 01-21-2003, 09:45 PM
dpdsouza dpdsouza is offline
Regular
 
Join Date: Oct 2002
Location: India
Posts: 80
Default

Thanks friends for you valuable suggestions,

rgds,
Dennis
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->