Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Why am I getting a recordcount of -1

I've been trying to count the number of records in a query that I'm
pulling back to Excel from Access. The query returns about 13K
records, but my recordcount keeps coming back as -1.

What am I doing wrong here?

Thanks

Public Sub SelectFromAccess()

Dim rsData As ADODB.Recordset
Dim sPath As String
Dim sConnect As String
Dim sSQL As String

' Clear the destination worksheet.
Sheet1.UsedRange.Clear

' Get the database path (same as this workbook).
sPath = ThisWorkbook.Path
If Right$(sPath, 1) < "\" Then sPath = sPath & "\"

' Create the connection string.
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sPath & "TestExcelData.mdb;"

' Build the SQL query.
sSQL = "SELECT [CM Data Store].* FROM [CM Data Store];"

' Retrieve the data using ADO.
Set rsData = New ADODB.Recordset
rsData.Open sSQL, sConnect, _
adOpenForwardOnly, adLockReadOnly, adCmdText

If Not rsData.EOF Then
Sheet1.Range("A2").CopyFromRecordset rsData
Else
MsgBox "No data located.", vbCritical, "Error!"
End If

'Get the record count
MsgBox rsData.RecordCount

rsData.Close
Set rsData = Nothing

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Why am I getting a recordcount of -1

Try doing a rsdata.movelast before getting the record count.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 131
Default Why am I getting a recordcount of -1

" wrote:

I've been trying to count the number of records in a query that I'm
pulling back to Excel from Access. The query returns about 13K
records, but my recordcount keeps coming back as -1.

What am I doing wrong here?

Thanks

Public Sub SelectFromAccess()

Dim rsData As ADODB.Recordset
Dim sPath As String
Dim sConnect As String
Dim sSQL As String

' Clear the destination worksheet.
Sheet1.UsedRange.Clear

' Get the database path (same as this workbook).
sPath = ThisWorkbook.Path
If Right$(sPath, 1) < "\" Then sPath = sPath & "\"

' Create the connection string.
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sPath & "TestExcelData.mdb;"

' Build the SQL query.
sSQL = "SELECT [CM Data Store].* FROM [CM Data Store];"

' Retrieve the data using ADO.
Set rsData = New ADODB.Recordset
rsData.Open sSQL, sConnect, _
adOpenForwardOnly, adLockReadOnly, adCmdText

If Not rsData.EOF Then
Sheet1.Range("A2").CopyFromRecordset rsData
Else
MsgBox "No data located.", vbCritical, "Error!"
End If

'Get the record count
MsgBox rsData.RecordCount

rsData.Close
Set rsData = Nothing

End Sub



Use static instead of forward-only cursor:

' Retrieve the data using ADO.
Set rsData = New ADODB.Recordset
rsData.Open sSQL, sConnect, _
adOpenStatic, adLockReadOnly, adCmdText

--
urkec
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
RecordCount not working T. S. Excel Programming 1 February 27th 07 11:34 PM
Reg: Incorrect RecordCount when opened a Excel sheet in ADO Recordset dev[_5_] Excel Programming 4 June 23rd 05 09:48 AM
Recordset.Recordcount Returns -1! Sauron[_24_] Excel Programming 1 June 28th 04 09:11 AM
RecordCount problem Terence Excel Programming 1 August 11th 03 09:10 AM


All times are GMT +1. The time now is 11:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"