Hi everyone
I'm new to here and the world of VBA (just started coding couple of days ago).
I have a spreadsheet that contains order ids and what I'd like to do is populate the adjacent column with related information from our sql server db.
I have made a lot of progress but what I'm struggling with is passing the order id from Sheet1 to my subroutine. I have tried defining rsFilter and using that in the WHERE clause in my sql query.
I think I'm on the right path but I need to list all values in my filter (A1:A10) so my recordset only returns the info for the existing order ids. Getting a 'subscript out of range' error at rrsFilter = Worksheets("Sheet1").Range("A1:A10").Value
.
I constructed this using a variety of sources so this might be a little inefficient. Thanks in advance. Brian
I'm new to here and the world of VBA (just started coding couple of days ago).
I have a spreadsheet that contains order ids and what I'd like to do is populate the adjacent column with related information from our sql server db.
I have made a lot of progress but what I'm struggling with is passing the order id from Sheet1 to my subroutine. I have tried defining rsFilter and using that in the WHERE clause in my sql query.
I think I'm on the right path but I need to list all values in my filter (A1:A10) so my recordset only returns the info for the existing order ids. Getting a 'subscript out of range' error at rrsFilter = Worksheets("Sheet1").Range("A1:A10").Value
.
I constructed this using a variety of sources so this might be a little inefficient. Thanks in advance. Brian
Code:
Sub FindCardOrders()
' 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 String
' Create new instances
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Define Filter
rsFilter = Worksheets("Sheet1").Range("A1:A10").Value
' 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"
'& " AND t.tri_transactionidcode = '" & rsFilter & "'"
' Specify the OLE DB provider.
cn.Provider = "sqloledb"
' Specify connection string on Open method.
cn.Open "Data Source=xxxx;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
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockBatchOptimistic
rs.Open sql
intMaxCol = rs.Fields.Count
intMaxRow = rs.RecordCount
rs.MoveLast
rs.MoveFirst
rs.Filter = rsFilter
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
Last edited by a moderator: