|
-
Mar 3rd, 2001, 09:55 AM
#1
Thread Starter
Addicted Member
Hi,
How can I get the number of records in a recordset?
I'm using ADO.
This is my code:
Rst.Open "Select PREmp.*, PRFil.EmpCod, PRFil.FilCod, PRFil.FilIde FROM PREmp INNER JOIN PRFil ON PREmp.EmpCod = PRFil.EmpCod", Connec
If Rst.RecordCount = 0 Then
vString = "No records available"
Combo1.AddItem vString
End If
Combo1.Text = Combo1.List(0) ' Show the first item
Note: the query above also can return an empty result.
When no records are returned, the message "No records avalilabe" should be inserted in the Combo, but it's not.
Is the RecordCount property ok? What's wrong in my code above?
Thanks,
Michel Jr.
-
Mar 3rd, 2001, 11:43 AM
#2
Lively Member
The problem is you did not set the cursor type of the recordset.
There are 4 cursor types: adOpenDynamic, adOpenForwardOnly, adOpenKeyset, and adOpenStatic. The default is adOpenForwardOnly.
For dynamic and forward only cursors, the RecordCount will always be -1.
-
Mar 5th, 2001, 06:15 AM
#3
Member
A.Srikanth
Application Engineer
-
Mar 5th, 2001, 07:21 AM
#4
_______
<?>
Code:
If mRS.BOF Then
vString = "No records available"
Combo1.AddItem vString
End If
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Mar 6th, 2001, 04:58 AM
#5
Well ...
Originally posted by HeSaidJoe
Code:
If mRS.BOF Then
vString = "No records available"
Combo1.AddItem vString
End If
Perhaps checking both .BOF and .EOF ?
Code:
If mRS.BOF AND mRS.EOF Then
MsgBox "No Records Found"
End If
.
-
Mar 6th, 2001, 05:02 AM
#6
New Member
You realy need to do a rec.MoveLast here
-
Mar 6th, 2001, 05:36 AM
#7
PowerPoster
yap, I agree with Desmond, whereby you need to move the recordset to the last position and then move it back to the first record location. This will ensure you've a correct recordcount.
-
Mar 6th, 2001, 07:19 AM
#8
Well ...
Agreed that by moving to the end and then returning to the beginning of the recordset, you get an accurate number of records in the recordset. But if you have a blank recordset, .MoveLast will fail. So you need a method to ascertain that there is at least one record in the recordset so you can safely use .MoveLast and that's when you can check for the .BOF and .EOF properties. When you make sure that the recordset is not empty, you can safely use the .MoveLast and .MoveFirst methods.
The following code would fail:
Code:
'There are no records in mRS
'
mRS.MoveLast
mRS.MoveFirst
MsgBox "Records : " & CStr(mRS.RecordCount)
But if you write it like this, it would always work:
Code:
If mRS.BOF And mRS.EOF Then
MsgBox "There are no records"
Else
mRS.MoveLast
mRS.MoveFirst
MsgBox "Records : " & CStr(mRS.RecordCount)
End If
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|