samedi 18 avril 2015

VBA (Excel) loop through recordset and match

I'm having issues with a recordset in VBA. What I'm trying to do is to match values in one column with one column in a recordset which I've retrieved from SQL. So it's supposed to get every value in a column and match every value with every value in the column of the recordset, not just a1 = b1 and a2 = b2, but a1= b1, a1 = b2, a1 = b3 etc. When it finds a match in the recordset, it's supposed to get that recordset row and print it next to the matched column row in Excel. Here's my code so far:



Public Sub ExecSPGetTransactions()
Dim con As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim par As String
Dim WSP As Worksheet
Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset


Dim rngRange As Range
Set rngRange = Range(Cells(20, 3), Cells(Rows.Count, 1)).EntireRow
rngRange.ClearContents

con.Open "Provider=SQLOLEDB;Data Source=LABSQL;Initial Catalog=ALDW;Integrated Security=SSPI;Trusted_Connection=Yes;"
cmd.ActiveConnection = con

Dim prm1 As ADODB.Parameter
Dim prm2 As ADODB.Parameter

cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, adParamInput, 10, Range("Config!C3").Text)
cmd.Parameters.Append cmd.CreateParameter("Param2", adInteger, adParamInput, 10, Range("A1").Text)

cmd.CommandText = "GetTransactions"
Set rs = cmd.Execute(, , adCmdStoredProc)

'Dont really know what I'm doing here:
With rs
.MoveFirst
Do While Not .EOF
For Each MyField In .Fields
If InStr(1, MyField.Value, varSearchVal) > 0 Then
strResults = strResults & .Fields("VerText") & "|" & MyField.Name & "|" & MyField.Value & vbCrLf
End If
Next MyField
.MoveNext
Loop
.Close


Set rs = Nothing
Set cmd = Nothing

con.Close
Set con = Nothing

End Sub

Aucun commentaire:

Enregistrer un commentaire