ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Slim Jim (https://www.excelbanter.com/excel-programming/320611-slim-jim.html)

Counting Records \(correct version\)

Slim Jim
 
Sorry about preovious post (accidentally hit Send,
posting only half the message !)

Here goes again, hope someone can advise...



I am connecting to a SQL Server stored procedure in Excel
VBA.

However, I am having difficulty COUNTING the number of
records returned from that Stored Procedure.

I have this code...

Dim conDB As New ADODB.Connection
Dim rstSP As New ADODB.Recordset
Dim strConnect As String

strConnect = "driver={sql server};server=
(local);database=mydb;uid=;pwd=;"


With conDB
.ConnectionString = strConnect
.Open
End With

'open the SP into recordset
conDB.storedprocname, rstSP

Now, I need to work out how many records have been
returned from the stored procedure into the recordset...

RecordCount DOES NOT WORK (it always returns -1), and I
need to be able to loop through the recordset but I can't
because I don't know how long it is !!

Please help !!

Cheers



Sharad Naik

Slim Jim
 
With your code, you are accessing a Linked Table.
Linked Table will not give RecrodCount, it will be
always -1.


"Counting Records (correct version)"
wrote in message ...
Sorry about preovious post (accidentally hit Send,
posting only half the message !)

Here goes again, hope someone can advise...



I am connecting to a SQL Server stored procedure in Excel
VBA.

However, I am having difficulty COUNTING the number of
records returned from that Stored Procedure.

I have this code...

Dim conDB As New ADODB.Connection
Dim rstSP As New ADODB.Recordset
Dim strConnect As String

strConnect = "driver={sql server};server=
(local);database=mydb;uid=;pwd=;"


With conDB
.ConnectionString = strConnect
.Open
End With

'open the SP into recordset
conDB.storedprocname, rstSP

Now, I need to work out how many records have been
returned from the stored procedure into the recordset...

RecordCount DOES NOT WORK (it always returns -1), and I
need to be able to loop through the recordset but I can't
because I don't know how long it is !!

Please help !!

Cheers





Jamie Collins

Slim Jim
 

Counting Records (correct version) wrote:
I am connecting to a SQL Server stored procedure in Excel
VBA.

However, I am having difficulty COUNTING the number of
records returned from that Stored Procedure.

I have this code...

Dim conDB As New ADODB.Connection
Dim rstSP As New ADODB.Recordset
Dim strConnect As String

strConnect = "driver={sql server};server=
(local);database=mydb;uid=;pwd=;"


With conDB
.ConnectionString = strConnect
.Open
End With

'open the SP into recordset
conDB.storedprocname, rstSP

Now, I need to work out how many records have been
returned from the stored procedure into the recordset...

RecordCount DOES NOT WORK (it always returns -1), and I
need to be able to loop through the recordset but I can't
because I don't know how long it is !!


Take a look at ADO's CursorType property. Because you didn't specify
one for your recordset, you got the default according to your
Connection object's CursorLocation ... which you didn't specify either.
So you got a server side cursor by default which gives you a forward
only cursor by default which does not support the RecordCount property.

The simplest tweak would be to add the line:

..CursorLocation = adUseClient

just above your .Open line. With a client side cursor you get a static
cursor by default, which means the RecordCount property will be
available.

However, I'd encourage you to consider which properties you do and
don't need; you may have to create a recordset and explicitly set some
properties rather than use the defaults.

Note with a forward only cursor you can still loop through the
recordset, testing for .EOF as you .MoveNext on each iteration.
Jamie.

--


Gary Brown[_5_]

Slim Jim
 
Is there a .MoveLast method that can be used followed by a .RecordCount?


"Jamie Collins" wrote:


Counting Records (correct version) wrote:
I am connecting to a SQL Server stored procedure in Excel
VBA.

However, I am having difficulty COUNTING the number of
records returned from that Stored Procedure.

I have this code...

Dim conDB As New ADODB.Connection
Dim rstSP As New ADODB.Recordset
Dim strConnect As String

strConnect = "driver={sql server};server=
(local);database=mydb;uid=;pwd=;"


With conDB
.ConnectionString = strConnect
.Open
End With

'open the SP into recordset
conDB.storedprocname, rstSP

Now, I need to work out how many records have been
returned from the stored procedure into the recordset...

RecordCount DOES NOT WORK (it always returns -1), and I
need to be able to loop through the recordset but I can't
because I don't know how long it is !!


Take a look at ADO's CursorType property. Because you didn't specify
one for your recordset, you got the default according to your
Connection object's CursorLocation ... which you didn't specify either.
So you got a server side cursor by default which gives you a forward
only cursor by default which does not support the RecordCount property.

The simplest tweak would be to add the line:

..CursorLocation = adUseClient

just above your .Open line. With a client side cursor you get a static
cursor by default, which means the RecordCount property will be
available.

However, I'd encourage you to consider which properties you do and
don't need; you may have to create a recordset and explicitly set some
properties rather than use the defaults.

Note with a forward only cursor you can still loop through the
recordset, testing for .EOF as you .MoveNext on each iteration.
Jamie.

--



Robin Hammond[_2_]

Slim Jim
 
Depending on your stored proc uou might also have to use the NoCount option

create proc somethingorother
as
begin
SET NOCOUNT ON
'proc code here
SET NOCOUNT OFF
END

Robin Hammond
www.enhanceddatasystems.com

"Jamie Collins" wrote in message
oups.com...

Counting Records (correct version) wrote:
I am connecting to a SQL Server stored procedure in Excel
VBA.

However, I am having difficulty COUNTING the number of
records returned from that Stored Procedure.

I have this code...

Dim conDB As New ADODB.Connection
Dim rstSP As New ADODB.Recordset
Dim strConnect As String

strConnect = "driver={sql server};server=
(local);database=mydb;uid=;pwd=;"


With conDB
.ConnectionString = strConnect
.Open
End With

'open the SP into recordset
conDB.storedprocname, rstSP

Now, I need to work out how many records have been
returned from the stored procedure into the recordset...

RecordCount DOES NOT WORK (it always returns -1), and I
need to be able to loop through the recordset but I can't
because I don't know how long it is !!


Take a look at ADO's CursorType property. Because you didn't specify
one for your recordset, you got the default according to your
Connection object's CursorLocation ... which you didn't specify either.
So you got a server side cursor by default which gives you a forward
only cursor by default which does not support the RecordCount property.

The simplest tweak would be to add the line:

.CursorLocation = adUseClient

just above your .Open line. With a client side cursor you get a static
cursor by default, which means the RecordCount property will be
available.

However, I'd encourage you to consider which properties you do and
don't need; you may have to create a recordset and explicitly set some
properties rather than use the defaults.

Note with a forward only cursor you can still loop through the
recordset, testing for .EOF as you .MoveNext on each iteration.
Jamie.

--




Jamie Collins

Slim Jim
 

Gary Brown wrote:
Is there a .MoveLast method that can be used followed by a

..RecordCount?


Surely:

http://msdn.microsoft.com/library/de...hmovefirst.asp
Jamie.

--



All times are GMT +1. The time now is 09:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com