ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ADODB (https://www.excelbanter.com/excel-programming/375530-adodb.html)

Mr Struggler

ADODB
 
The following code cycles through the recordset OK, but the RS.RecordCount
is allways = -1. I Need to be able to run a query from which I can tell if a
record exists, if I cant determine the record count I dont know how to
procede

Any help is appreciated


Sub Northwind()

Dim rs As New ADODB.Recordset
Dim con As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rec As New ADODB.Record

Dim sqlString As String
sqlString = "SELECT * FROM CUSTOMERS"

con.ConnectionString = "Driver={SQL
Server};Server=MYSERVER;Database=NORTHWIND;Uid=sa; Pwd=password;"
con.Open
cmd.ActiveConnection = con
cmd.CommandText = sqlString
cmd.CommandType = adCmdText

Set rs = cmd.Execute(sqlString)

rs.MoveFirst
Do While Not rs.EOF
Debug.Print rs.Fields(0)
rs.MoveNext
Loop

con.Close
Set con = Nothing
Set cmd = Nothing
End Sub




Goofy

ADODB
 
Thanks Robin,

This seems to have done the trick.


Regards


"Robin Hammond" wrote in
message ...
Try this syntax which sets your cursor type:

rs.Open sqlString, con, adOpenStatic, adLockBatchOptimistic, adCmdText

--
Robin Hammond
www.enhanceddatasystems.com


"Mr Struggler" <lkjhlkj wrote in message
...
The following code cycles through the recordset OK, but the
RS.RecordCount is allways = -1. I Need to be able to run a query from
which I can tell if a record exists, if I cant determine the record count
I dont know how to procede

Any help is appreciated


Sub Northwind()

Dim rs As New ADODB.Recordset
Dim con As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rec As New ADODB.Record

Dim sqlString As String
sqlString = "SELECT * FROM CUSTOMERS"

con.ConnectionString = "Driver={SQL
Server};Server=MYSERVER;Database=NORTHWIND;Uid=sa; Pwd=password;"
con.Open
cmd.ActiveConnection = con
cmd.CommandText = sqlString
cmd.CommandType = adCmdText

Set rs = cmd.Execute(sqlString)

rs.MoveFirst
Do While Not rs.EOF
Debug.Print rs.Fields(0)
rs.MoveNext
Loop

con.Close
Set con = Nothing
Set cmd = Nothing
End Sub









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

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