|
-
March 4th, 2004, 04:10 AM
#1
VBA Recordcount
Hi Guru's,
I did a search on this subject but no one has encountered the error I am having. This is the code
------------------------------------------------------------------
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Set conn = New ADODB.Connection
Set rst = New ADODB.Recordset
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & "G:\NAWKLANT\FONTYS\FONTYS.MDB" & ";Persist Security Info=False"
conn.Open
rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic
rst.LockType = adLockBatchOptimistic
rst.Open "SELECT * FROM Studenten"
Number = rst.RecordCount
rst.MoveLast
rst.MoveFirst
txtRecCount.Text = Number
--------------------------------------------------------------------
I get an error at the rst.open part saying that this connection can not be used for this action, or this action is not valid in this context.
Hope someone can tell me what I am doing wrong and excuse my newbieness.
Thanks in advance,
Jasper
Last edited by Jasper XAFAX; March 4th, 2004 at 04:39 AM.
Flying is to throw yourself on the ground and miss
-
March 4th, 2004, 06:58 AM
#2
I think your problem is that you haven't assigned a connection to RST.
Will you try RST.ACTIVECONNECTION = CONN before your RST.OPEN.
HTH
JP
Please remember to rate all postings. 
-
March 4th, 2004, 08:18 AM
#3
Whaaaaaaaa, it worked. Thank you very much!
Jasper
Flying is to throw yourself on the ground and miss
-
March 4th, 2004, 10:48 PM
#4
You would be a lot better off doing
Select count(*) from tablename. This will return the total number of records. I don't know what ado is doing behind the covers and I am sure I don't want to know.
Regards,
David Ritchie
CodeLab Technology Group
-
March 5th, 2004, 02:31 AM
#5
Hi Cambece,
Why would I change something that's already working properly?
Jasper
Flying is to throw yourself on the ground and miss
-
March 5th, 2004, 08:01 AM
#6
The Select Count(*) will save you having to open the recordset, and then going to the last record, and trying to get the number of records twice.
I have found that depending on the cursor I use (adUseClient or adUseServer), that sometimes the .RECORDCOUNT returned is -1, where this property may not be supported I guess.
I think in Access it is faster to use COUNT(*) instead of COUNT(fld name). Don't know about SQL Server though.
HTH
JP
Please remember to rate all postings. 
-
March 8th, 2004, 06:20 PM
#7
I hope I did not offend you. That was not my intention, but rather to educate you in the proper way of developing a piece of code that counts the number of records in a table.
The select count method will make your code better for the following reasons:
Performance (You will not open a cursor)
Portability (It will work even if you switch versions of ADO)
Reliablity (You can get unpredicatable results from you method)
The reason you would change something that already works is to make it better.
Regards,
David Ritchie
CodeLab Technology Group
-
March 9th, 2004, 02:37 AM
#8
Hi Cambece and jp140768,
Thank you both for clearing this up for me, and Cambece I am not offended at all. I am going to try your piece of code. So I can keep the rest of the code the same and just ad Count in the rst.open part? Oh, and I don't think I mentioned I work within VBA .
Many thanks,
Jasper
Last edited by Jasper XAFAX; March 10th, 2004 at 07:33 AM.
Flying is to throw yourself on the ground and miss
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
|