|
|
|
When working with a forward-only, read-only Recordset - that is, the default ADO Recordset - the RecordCount property always returns -1. (This can also happen with other types of server-side cursors, depending on the specific OLEDB provider.) In general, you can determine how many records were returned only after visiting the entire Recordset, but often you need this information before processing the Recordset.
Depending on the approximate number of expected records, you can use the GetRows method to retrieve the Recordset's entire contents, and then use the UBound() function to determine the number of returned rows: |
Click here to copy the following block | Dim rs As New ADODB.Recordset Dim arr() As Variant Dim reccount As Long
rs.Open "SELECT * FROM Publishers", "DSN=pubs", , , adCmdText
arr() = rs.GetRows()
reccount = UBound(arr, 2) + 1
|
In some cases, however, you can't use this approach. For example, the number of returned rows might be too high (and the arr() array would therefore take too much memory). Or you might be using an updateable cursor (e.g. a dynamic cursor), and you don't want to read and process all the values twice, once in the GetRows method and once using a MoveNext loop. In all these cases you should submit two distinct SQL queries to the database, one to determine the number of rows in the Recordset, and the next one to retreive the actual rows: |
Click here to copy the following block | Dim cn As New ADODB.Connection, rs As New ADODB.Recordset Dim reccount As Long
cn.Open "DSN=pubs"
rs.Open "SELECT COUNT(*) FROM publishers", cn, , , adCmdText
reccount = rs(0) rs.Close
rs.Open "SELECT * FROM Publishers", cn, , , adCmdText
If you Dim rs As New ADODB.Recordset Dim reccount As Long, sql As String
sql = "SELECT COUNT(*) FROM publishers;" & "SELECT * FROM publishers" rs.Open sql, "DNS=pubs", , , adCmdText
reccount = rs(0)
Set rs = rs.NextRecordset
|
This version is faster because it requires only one trip to the server.
|
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|