#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 212
Default 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




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default 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.

--

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default 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.

--


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 575
Default 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.

--





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default 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.

--

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



All times are GMT +1. The time now is 04:06 AM.

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"