Print recordset if value exists in array - arrays

New to vb and am struggling with returning data from my recordset if variable equals a value in my array.
I think what I've done so far is correct but having trouble with the final bit. I need to script something to say "if value from range equals a value in my array then print the recordset".
I hope someone can help. I'm also new to vb so any suggestions on how to improve my code would be great. Thanks in advance!! Brian
Sub FindCardOrdersv2()
' Initialize variables.
Dim cn As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim provStr As String
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim rsFilter As Range
Dim i As Integer
Dim rng As Variant
Dim payid(1 To 10) As String
Dim tw As ThisWorkbook
Workbooks("cleanse.xlsm").Activate
Worksheets("Sheet1").Activate
' Create new instances
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
' sql query
sql = "SELECT TOP 100 t.tri_transactionidcode," _
& "SUBSTRING(t.tri_reference, 1, 9) AS merchantref," _
& "t.tri_additionalreferencenumber, t.CreatedOn, t.tri_amount, ISNULL(t.tri_paymenttransactiontypeidName, 'Online')" _
& " FROM dbo.tri_onlinepayment t INNER JOIN dbo.tri_transaction tr ON tr.tri_onlinepaymentid = t.tri_onlinepaymentId" _
& " WHERE t.tri_transactionresult = 9"
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Specify connection string on Open method.
cn.Open "Data Source=IFL-SQL11;Database=IFL_MSCRM;Trusted_Connection=yes;Integrated Security=SSPI"
' Assign active connection to recordset
Set rs.ActiveConnection = cn
'intMaxCol = rs.Fields.Count
' Define cursors and open sql
With rs
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open sql
End With
For i = 1 To 3
payid(i) = rs.Fields.Item(0)
Debug.Print rs(0)
Debug.Print rs(1)
Debug.Print rs(3)
rs.MoveNext
Next i
'rsFilter = Range("A1:A10")
For Each rsFilter In Range("A1:A10").Cells
If rsFilter.Value = payid Then
Debug.Print rs(1)
rs.MoveNext
End If
Next
'If rs.RecordCount > 0 Then
' With Worksheets("Sheet1")
' .Range("B1:B2").CopyFromRecordset rs
' End With
'End If
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub

Add an extra for loop inside to compare every value in the range to the array values
For Each rsFilter In Range("A1:A10").Cells
For i=1 To 3
If rsFilter.Value = payid(i) Then
Debug.Print rs(1)
rs.MoveNext
End If
Next i
Next

Related

How to format data inserted from SQL Server into Excel table using VBA?

I'm using the code below to import tables from SQL Server into Excel.
So the old data be removed and new be inserted. I need to insert table to related data. I find something called ListObject but I didn't know how to apply it. Is there any way to insert table after the data are inserted?
Option Explicit
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Sub Connect_To_SQLServer(ByVal Server_Name As String, ByVal Database_Name As String, ByVal SQL_Statement As String)
Dim strConn As String
Dim wsReport As Worksheet
Dim col As Integer
Dim tbl As ListObject
strConn = "Provider=SQLOLEDB;"
strConn = strConn & "Server=" & Server_Name & ";"
strConn = strConn & "Database=" & Database_Name & ";"
strConn = strConn & "Trusted_Connection=yes;"
Set conn = New ADODB.Connection
With conn
.Open ConnectionString:=strConn
.CursorLocation = adUseClient
End With
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = conn
.Open Source:=SQL_Statement
End With
'// here i selected the sheet where the data will be written
Set wsReport = Sheets("RDWH")
With wsReport
'// here I clear the old data and insert new set
wsReport.Select
Selection.Clear
For col = 0 To rst.Fields.Count - 1
.Cells(1, col + 1).Value = rst.Fields(col).Name
Next col
.Range("A2").CopyFromRecordset rst
End With
Set wsReport = Nothing
Call Close_Connections
End Sub
Private Sub Close_Connections()
If rst.State <> 0 Then rst.Close
If conn.State <> 0 Then conn.Close
'// Release Memory
Set rst = Nothing
Set conn = Nothing
End Sub
Sub Run_Report()
Dim Server_Name As String
Server_Name = "NL-1012716\SQLEXPRESS"
Call Connect_To_SQLServer(Server_Name, "project", "SELECT * FROM [2_RDWH_CAST]")
End Sub
It is easier if the sheet name matches the table name and just delete any sheet with that name. Then recreate the sheet with a new table. This example shows how multiple tables can be downloaded in one operation with each query creating a new sheet.
-- Update ; Added sheet name and position parameters to sub Run_Report.
Option Explicit
Sub Run_Reports()
Dim conn As ADODB.Connection
Const SERVER_NAME As String = "NL-1012716\SQLEXPRESS"
Const DB_NAME = "test"
Dim sTable As String
sTable = ThisWorkbook.Sheets("Control").Range("H6").Value
' connect to DB, run reports and disconnect
Set conn = Connect_To_SQLServer(SERVER_NAME, DB_NAME)
Call Run_Report(conn, sTable, "Sheet3", "A3")
conn.Close
Set conn = Nothing
End Sub
Sub Run_Report(ByRef conn As ADODB.Connection, ByVal TABLE_NAME As String, _
SHEET_NAME As String, START_CELL As String)
If Len(TABLE_NAME) = 0 Then
MsgBox "TABLE_NAME missing", vbCritical, "ERROR"
Exit Sub
End If
Dim rst As ADODB.Recordset
Dim wb As Workbook, ws As Worksheet, i As Integer
Dim wsReport As Worksheet, tblResult As ListObject
' query
Dim SQL As String
SQL = "SELECT * FROM [" & TABLE_NAME & "]"
' execute query
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = conn
.Open Source:=SQL
End With
' output
Set wb = ThisWorkbook
' clear sheet
Set wsReport = wb.Sheets(SHEET_NAME)
wsReport.Cells.Clear
With wsReport.Range(START_CELL)
' write headers
For i = 0 To rst.Fields.Count - 1
.Offset(0, i).Value = rst.Fields.Item(i).Name
Next
' write data
.Offset(1, 0).CopyFromRecordset rst
' create table
Set tblResult = wsReport.ListObjects.Add(xlSrcRange, .CurrentRegion, , xlYes)
' add table name
.Offset(-1, 0) = TABLE_NAME
End With
MsgBox "Rows written = " & rst.RecordCount, vbInformation, TABLE_NAME
rst.Close
Set rst = Nothing
End Sub
Function Connect_To_SQLServer(SERVER_NAME As String, DB_NAME As String) As ADODB.Connection
Dim strConn As String
strConn = "Provider=SQLOLEDB;" & _
"Server=" & SERVER_NAME & ";" & _
"Database=" & DB_NAME & ";" & _
"Trusted_Connection=yes;"
Set Connect_To_SQLServer = New ADODB.Connection
With Connect_To_SQLServer
.Open ConnectionString:=strConn
.CursorLocation = adUseClient
End With
End Function

How to use VBA to connect SQL server and export SQL result

I have used below code but failed at step
ActiveWorkbook.Sheets("Sheet1").Cells.CopyFromRecordset rs
Can anyone help to check why?
Sub get_Data_From_DB()
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
' Open a connection by referencing the ODBC driver.
cnn.ConnectionString = "driver={SQL Server};" & _
"server=aaaaa,2431;uid=bbb;pwd=ccc;database=ddd"
cnn.Open
' Find out if the attempt to connect worked.
If cnn.State = adStateOpen Then
MsgBox "Welcome to Pubs!"
Sql = "SELECT top 10 * from tableA(NOLOCK)"
Set rs = cnn.Execute(Sql)
ActiveWorkbook.Sheets("Sheet1").Cells.CopyFromRecordset rs
Else
MsgBox "Sorry. No Pubs today."
End If
' Close the connection.
cnn.Close
End Sub
Change this:
ActiveWorkbook.Sheets("Sheet1").Cells.CopyFromRecordset rs
To something like this:
ActiveWorkbook.Sheets("Sheet1").Range("A1").CopyFromRecordset rs
You can change "A1" to another cell if you'd like.
EDIT: Here's how I'd actually organize this to make it flexible/reusable.
Sub runPubsQuery
Dim sqlStr As String
sqlStr = "SELECT top 10 * FROM tableA(NOLOCK)"
Call writeSqlResults(sqlStr,getConnectionString(),ThisWorkbook.Sheets("Sheet1"))
End Sub
Sub writeSQLResults(sqlStr As String, connStr As String, destWS As Worksheet, _
Optional errMsg As String = "Sorry. No Pubs today.", Optional welcMsg As String = "Welcome to Pubs!")
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim headerArr As Variant
cn.Open (connStr)
If Not cn.State = adStateOpen Then
MsgBox errMsg
Else
MsgBox welcMsg
Set rs = cn.Execute(sqlStr)
If Not rs.EOF Then
headerArr = getRecordHeaders(rs)
With destWS
.Cells.Clear
.Range(.Cells(1, 1), .Cells(1, UBound(headerArr, 2))).Value = headerArr
.Range("A2").CopyFromRecordset rs
End With
rs.Close
End If
End If
cn.Close
End Sub
Function getConnectionString(Optional serverName As String = "aaaa,2431", Optional dbName As String = "ddd", _
Optional uidStr As String = "bbb", Optional pwdStr As String = "ccc") As String
getConnectionString = "Driver={SQL Server};" & _
"Server=" & serverName & ";" & _
"Uid=" & uidStr & ";" & _
"Pwd=" & pwdStr & ";" & _
"Database=" & dbName & ";"
End Function
Function getRecordHeaders(rs As Variant) As Variant
If Not TypeName(rs) = "Recordset" Then
MsgBox "Error: Parameter rs is not a valid recordset"
Stop
Exit Function
End If
Dim i As Long
Dim j As Long
If Not rs.EOF Then
ReDim headerArr(1 To 1, 1 To rs.Fields.Count) As Variant
j = 0
For i = LBound(headerArr, 2) To UBound(headerArr, 2)
headerArr(1, i) = rs.Fields(j).Name
j = j + 1
Next
getRecordHeaders = headerArr
Else
MsgBox "Error: Recordset is empty"
Stop
Exit Function
End If
End Function
This is how I would do it, to get all field naems and all records as well.
Sub ADOExcelSQLServer()
' Carl SQL Server Connection
'
' FOR THIS CODE TO WORK
' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library
'
Dim Cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "Your_Server_NameS" ' Enter your server name here
Database_Name = "Your_DB_Name" ' Enter your database name here
User_ID = "" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [Customers]" ' Enter your SQL here
Set Cn = New ADODB.Connection
Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, Cn, adOpenStatic
' Dump to spreadsheet
For iCols = 0 To rs.Fields.Count - 1
Worksheets("Sheet1").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
With Worksheets("sheet1").Range("a2:z500") ' Enter your sheet name and range here
'.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
Cn.Close
Set Cn = Nothing
End Sub

VBA Getting data from SQL Server

I am having trouble when getting data from SQL Server. Here is the code.
Private Sub Form_Load()
Dim blsCritical As Boolean
'----------------------------------------------
ListBox.AddItem "Initializing..."
'----------------------------------------------
Me.Repaint
'----------------------------------------------
ListBox.AddItem "Welcome"
'----------------------------------------------
Me.Repaint
'----------------------------------------------
ListBox.AddItem "Examining your access rights..."
Call ConnectSQLServer
'----------------------------------------------
Me.Repaint
ListBox.AddItem strSQL
'----------------------------------------------
ListBox.AddItem "Opening database connection..."
'----------------------------------------------
Me.Repaint
End Sub
Sub ConnectSQLServer()
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Dim strConn As String
Dim par As ADODB.Parameter
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
objMyConn.ConnectionString = "DRIVER=SQL Server;SERVER=CHU-AS-0004;DATABASE=RTC_LaplaceD_DEV;Trusted_Connection=Yes;"
objMyConn.Open
strSQL = "SELECT [currentVersion], [standardVersion] FROM [dbo].[Version]"
If currentVersion = "" Then
MsgBox ("No currentVersion value")
ElseIf Not IsNull(currentVersion) Then
If currentVersion < standardVersion Then
MsgBox ("Upgrade is needed")
ElseIf currentVersion = standardVersion Then
MsgBox ("PASS")
Else
End If
Else
End If
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
End Sub
I have data in SQL Server:
but I cannot get data from SQL Server. When I execute it, 'No CurrentVersion value' message pops up. I don't see any mistakes in my code. Could you help me to solve this problem?(It would be great if you can share your fixed code...)
Just whipped this up for you to show you where you went wrong... untested...
strSQL = "SELECT [currentVersion], [standardVersion] FROM [dbo].[Version]"
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
while objMyRecordset.EOF = false
currentVersion = objMyRecordset!currentVersion
objMyRecordset.MoveNext
wend
If currentVersion = "" Then
MsgBox ("No currentVersion value")
ElseIf Not IsNull(currentVersion) Then
If currentVersion < standardVersion Then
MsgBox ("Upgrade is needed")
ElseIf currentVersion = standardVersion Then
MsgBox ("PASS")
Else
End If
Else
End If
Something like this should do the job.
Sub GetDataFromADO()
'Declare variables'
Set objMyconn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
Dim rc As Long
'Open Connection'
objMyconn.ConnectionString = "Provider=SQLOLEDB;Data Source=SAXAM\SQLEXPRESS;Initial Catalog=AdventureWorks2012; Integrated Security=SSPI;"
objMyconn.Open
'Set and Excecute SQL Command'
Set objMyCmd.ActiveConnection = objMyconn
objMyCmd.CommandText = "select * from [Person].[BusinessEntity] "
objMyCmd.CommandType = adCmdText
objMyCmd.Execute
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyconn
objMyRecordset.Open objMyCmd
'Copy Data to Excel'
'ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
Application.ActiveCell.CopyFromRecordset (objMyRecordset)
rc = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(rc + 1, 1).Select
'Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Value
objMyconn.Close
End Sub
Here's one more idea. Let's say you had a bunch of Select statements in a bunch of cells from A1, down to whatever, you can add sheets dynamically, and import some sample data on to each sheet, to get a sense of the data structures of multiple tables.
Assuming the following in A1:A3.
SELECT TOP 1000 * FROM [YOUR_DB].[dbo].[YOUR_TABLE1]
SELECT TOP 1000 * FROM [YOUR_DB].[dbo].[YOUR_TABLE2]
SELECT TOP 1000 * FROM [YOUR_DB].[dbo].[YOUR_TABLE3]
Run the script below.
Sub Download_From_Multiple_Tables()
'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
Dim rCell As Range
Dim rRng As Range
Dim sht As Worksheet
Dim LastRow As Long
Set cnn = New ADODB.Connection
'For a trusted Connection, where your user ID has permissions on the SQL Server:
cnn.Open ConnectionString:="Provider=SQLOLEDB.1;" & _
"Data Source=" & "YOUR_SERVER_NAME" & ";Initial Catalog=" & "YOUR_DB_NAME" & _
";TRUSTED_CONNECTION=YES"
'Opens connection to the database
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900
Set sht = ThisWorkbook.Worksheets("Sheet1")
'Ctrl + Shift + End
LRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
Set rRng = Sheet1.Range("A1:A" & LRow)
i = 2
For Each rCell In rRng.Cells
LPosition = InStrRev(rCell.Value, "[dbo]") + 5
' Name the newly added worksheet, based on the cell value
Name = Mid(rCell.Value, LPosition + 1, 99)
' Remove [] characters, as these are not permitted in tab names
Name = Replace(Name, "[", "")
Name = Replace(Name, "]", "")
SheetName = Left(Name, 31)
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = SheetName
Worksheets(SheetName).Activate
StrQuery = rCell.Value
'Performs the actual query
rst.Open StrQuery, cnn
'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
' Dump field names to the worksheet
For intFieldIndex = 0 To rst.Fields.Count - 1
ActiveSheet.Cells(1, intFieldIndex + 1).Value = rst.Fields(intFieldIndex).Name
Next intFieldIndex
' Dump the records to the worksheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rst
' Sheets(i).Range("A1").CopyFromRecordset rst
i = i + 1
rst.Close
Next rCell
End Sub

Loop through excel spreadsheet using macro

I'm trying to loop through excel cells to get value from sql server table. I've written a macro that will take the jobnumber from the excel and query sql server tale to get customer details and dump it on the spreadsheet. So far I'm only able to get data for single cell. How can I loop through the excel cells for more than one jobnumber. I'm new to VBA. Thanks for your help. Here is my macro:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim newrow As String
newrow = Worksheets("Sheet1").Cells(1, "A").Value
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=0.0.0.0;" & _
"Initial Catalog=asset;" & _
"User ID=Temp;Password=test123;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("SELECT customer FROM job_tab where jobnum2='" & Trim(newrow) & "';")
' Check we have data.
If Not rs.EOF Then
' Transfer result.
Sheets(1).Range("B1").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub
You can do so, by using the IN SQL statement:
SELECT customer FROM job_tab WHERE jobnum2 IN ( 'Value1', 'Value2', ... );
Therefore you have to loop through all the customers in column A of your table and alter the SQL request to use the above mentioned statement:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim newrow As String
'MODIFIED: create the search string for the IN-Statement
newrow = "("
For i = 1 To Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).Row
newrow = newrow & "'" & Trim(Worksheets("Sheet1").Cells(i, "A").value) & "',"
Next i
newrow = Left(newrow, Len(newrow) - 1)
newrow = newrow & ")"
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=0.0.0.0;" & _
"Initial Catalog=asset;" & _
"User ID=Temp;Password=test123;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
'MODIFIED: altered the SQL statement to use the search string with IN
Set rs = conn.Execute("SELECT customer FROM job_tab where jobnum2 IN " & newrow & "';")
' Check we have data.
If Not rs.EOF Then
' Transfer result.
Sheets(1).Range("B1").CopyFromRecordset rs
' Close the recordset
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing
End Sub

VB6 ADODB.Recordset RecordCount property always returns -1

I am trying to get some old VB6 code to work with SQL Server Compact.
I can connect, open the database and all seems well. I can run insert select commands which work.
However the ADODB.Recordset RecordCount property always returns -1 even though I can access the Fields and see the data. Changing the CursorLocation = adUseClient causes a problem when executung the SQL (multiple-step operation generated errors).
Option Explicit
Private Const mSqlProvider As String = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;"
Private Const mSqlHost As String = "Data Source=C:\Database.sdf;"
Private mCmd As ADODB.Command ' For executing SQL
Private mDbConnection As ADODB.Connection
Private Sub Command1_Click()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim DbConnectionString As String
DbConnectionString = mSqlProvider & _
mSqlHost
Set mDbConnection = New ADODB.Connection
mDbConnection.CursorLocation = adUseServer
Call mDbConnection.Open(DbConnectionString)
If mDbConnection.State = adStateOpen Then
Debug.Print (" Database is open")
' Initialise the command object
Set mCmd = New ADODB.Command
mCmd.ActiveConnection = mDbConnection
mCmd.CommandText = "select * from myTestTable"
mCmd.CommandType = adCmdText
Set rs = mCmd.Execute
Debug.Print rs.RecordCount ' Always returns -1 !!
Debug.Print rs.Fields(0) ' returns correct data for first row, first col
Debug.Print rs.Fields(1) ' returns correct data for first row, 2nd col
Debug.Print rs.Fields(2) ' returns correct data for first row, 3rd col
End If
End Sub
Any advice would be gratefully accepted.
Actually the CursorLocation plays a major role in this case. Use rs.CursorLocation = adUseClient to set the cursor location and try.
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
Dim DbConnectionString As String
DbConnectionString = mSqlProvider & _
mSqlHost
Set mDbConnection = New ADODB.Connection
mDbConnection.CursorLocation = adUseServer
Call mDbConnection.Open(DbConnectionString)
If mDbConnection.State = adStateOpen Then
Debug.Print (" Database is open")
' Initialise the command object
Set mCmd = New ADODB.Command
mCmd.ActiveConnection = mDbConnection
mCmd.CommandText = "select * from myTestTable"
mCmd.CommandType = adCmdText
Set rs = mCmd.Execute
Debug.Print rs.RecordCount ' This should now return the right value.
Debug.Print rs.Fields(0) ' returns correct data for first row, first col
Debug.Print rs.Fields(1) ' returns correct data for first row, 2nd col
Debug.Print rs.Fields(2) ' returns correct data for first row, 3rd col
End If
End Sub
That's a result of the type of cursor used to access the data, this post covers the issue and possible fixes.
http://www.devx.com/tips/Tip/14143
EDIT
I apologize for not being more attentive to the fact that you were dealing with Compact. With Compact the situation is similar to the one I referenced, as it uses forward only cursors by default (which do not support row count) but there are two other cursor types available as documented in the link below.
http://support.microsoft.com/kb/272067
From memory with working with VB6/ADO a long time ago the .RecordCount field doesn't return meaningful data until you've moved to the end of the recordset.
rs.MoveLast
rs.MoveFirst
Debug.Print rs.RecordCount
Though with this you'll need to make sure you have the appropriate cursor type (i.e., not forward only).
The only other solution I can think of is to do a separate SELECT COUNT(*) FROM myTestTable, etc but this has issues with the data changing between that call, and the one that actually returns the rows.
With Compact the default cursor attribute is adOpenForwardOnly for improved performance. As such RecordCount is returned as "-1" which means its not available, rather than blank. This is by design because the # of records in a dynamic cursor could change and result in pinging back and forth between the client server to maintain accuracy. However, if the record count is vital try setting it to use adOpenKeyset or adOpenStatic with a server-side cursor.
Check Recordset Property
The follow is result that RecordCount value returned by com.status.live code
+------------------+-------------------+-------------+---------------+--------------+
| CursorTypeEnum|adOpenForwardOnly=0|dOpenKeyset=1|adOpenDynamic=2|adOpenStatic=3|
|CursorLocationEnum| |
+------------------+-------------------+-------------+---------------+--------------+
|adUseServer = 2 | X | O | X | O |
|adUseClient = 3 | O | O | O | O |
+------------------+-------------------+-------------+---------------+--------------+
You may try something like this..
Set rs = mCmd.Execute
rs.MoveFirst
Do Until rs.EOF = true
Debug.Print rs.RecordCount ' Always returns -1 !!
Debug.Print rs.Fields(0) ' returns correct data for first row, first col
Debug.Print rs.Fields(1) ' returns correct data for first row, 2nd col
Debug.Print rs.Fields(2) ' returns correct data for first row, 3rd col
counter = counter + 1
rs.MoveNext
Loop
Here is a solution for you that I used
Dim recordnumber As Long
Dim SalRSrec As New ADODB.Recordset
Set SalRSrec = Nothing
SalRSrec.Open ("SELECT count(*) from SALARY where EMPID= '" & cmb_empid & "' ;"), Dbase, adOpenKeyset, adLockOptimistic
recordnumber = SalRSrec.GetString
MsgBox recordnumber
Replace Set rs = mCmd.Execute with:
set rs = new ADODB.Recordset
rs.Open "select * from myTestTable", mDBConnection, adOpenDynamic, adLockOptimistic
The adOpenDynamic will allow a forward/backward read through to get your recordcount.
Try using following code if still returns -1
Set Conn = createobject("ADODB.connection")
Set Rs = createobject("ADODB.recordset")
Conn.Open "DSN=DSN_QTP"
'Rs.Open "Select * From orders",Conn,adOpenDynamic,adLockBatchOptimistic
Rs.Open "Select * from [QTP-Table]",Conn,1 'Use either 1 or 3
'I tried using adopendynamic but it still returned -1. Using 1 it gave me correct count. 'Though I am using this code in QTP (Vbscript) same should work for VB6 also.
msgbox Rs.RecordCount
Below code might help you,
set conn = CreateObject("ADODB.Connection")
conn.open "<connection string>"
set rs = CreateObject("ADODB.Recordset")
sql = "SELECT columns FROM table WHERE [...]"
rs.open sql,conn,1,1
if not rs.eof then
nr = rs.recordcount
response.write "There were " & nr & " matches."
' ... process real results here ...
else
response.write "No matches."
end if
rs.close: set rs = nothing
conn.close: set conn = nothing
Set cn = CreateObject("ADODB.Connection")
strVerb = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=C:\test.accdb"
tab1 = "tabelle1"
strSQL = "SELECT Count(*) FROM " & tab1
Debug.Print strSQL
cn.Open strVerb
Set rs = cn.Execute(strSQL)
Debug.Print rs.Fields(0)
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
you must set the CONNECTIOn to aduseClient, no recordset
and be carefoul to set only a new connection, if you use the same connection in your proyect, you can get other errors.
CRAETE A NEW CONNEXTION with aduseclient
Dim Sql As String
Dim CnCommand As New ADODB.Connection
On Error GoTo VerError:
Dim Comando As ADODB.Command
Set Comando = New ADODB.Command
CnCommand.ConnectionString = Cn.ConnectionString 'your exist connection in application
CnCommand.Open
CnCommand.CursorLocation = adUseClient
Set Comando.ActiveConnection = CnCommand
'Comando.ActiveConnection.CursorLocation = adUseClient
Comando.Parameters.Append Comando.CreateParameter("#Usuario", adInteger, adParamInput, , V_General.Usuario.Codigo)
Comando.CommandType = adCmdStoredProc
Comando.CommandText = "SP_Contratac"
Dim Rs As Recordset
Set Rs = New ADODB.Recordset
Set Rs = Comando.Execute()
This following code returns the recortcount exactly...
Public Sub test()
Dim cn As New ADODB.Connection()
Dim sPath As String = Application.ExecutablePath
sPath = System.IO.Path.GetDirectoryName(sPath)
If sPath.EndsWith("\bin") Then
sPath = sPath.Substring(0, Len(sPath) - 4)
End If
Dim DbConnectionString As String
DbConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & sPath & "\students.mdb"
cn.ConnectionString = DbConnectionString
cn.Open()
Dim rs As New ADODB.Recordset()
rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
rs.Open("select * from students", cn)
MsgBox(rs.RecordCount)
rs.ActiveConnection = Nothing
cn.Close()
End Sub

Resources