That was an excellent write up! (Bookmarked)
Here is the working code. As you said, I hooked up the Command object to the RecordSet object, and then using your guide I was able to tweak my paramater properties. Very excited to start using ADO this way. I will use MS Query to build connections no more!
Code:
Sub AdoTest
Dim objConn As ADODB.Connection, objCmd As ADODB.Command, objRS As ADODB.Recordset, fld As ADODB.Field
Dim a As String, stConn, sSQL, sPath As String
Dim x As Integer
Dim ReviewDate As Range, MAnalyst
Set ReviewDate = Sheets(1).Range("G1")
Set MAnalyst = Sheets(1).Range("G2")
Set objConn = New ADODB.Connection
Set objCmd = New ADODB.Command
Set objRS = New ADODB.Recordset
sPath = "C:\Users\Desktop\SQL.txt"
a = "C:\Users\Desktop\Reporting Database.mdb"
sSQL = LoadTextFile(sPath)
stConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & a & ";"
'=======================================================================
With objRS
.CursorType = adOpenForwardOnly
.LockType = adLockOptimistic
End With
objConn.Open (stConn)
With objCmd
.ActiveConnection = objConn
.CommandType = adCmdText
.CommandText = sSQL
.Parameters.Append .CreateParameter("MA", adVarChar, adParamInput, 30, MAnalyst.Text)
.Parameters.Append .CreateParameter("DateRev", adDate, adParamInput, , ReviewDate)
Set objRS = objCmd.Execute
End With
x = 1
For Each fld In objRS.Fields
Sheets(3).Cells(2, x) = fld.Name
x = x + 1
Next fld
With Sheets(3)
.Cells(3, 1).CopyFromRecordset objRS
End With
objConn.Close
End Sub