I'm trying to use an ADODB connection to create a record set from a query, then copy the contents of that record set into a range of cells
Below is the outline of my code, but I keep getting errors
Run-time error '-2147467259 (80004005)' Unspecified error
(I've replaced my specific references with all caps dummy references)
Sub Subroutine()
'establish ADODB connection to retrieve data from TABLE
Dim objConnection As New ADODB.connection
Dim objRecordset As New ADODB.Recordset
With objConnection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source = MYDATASOURCE" & _
"Extended Properties = Excel 8.0;HDR = Yes;"
.Open
End With
'use query to record data to Excel range
Set objRecordset = objConnection.OpenRecordset("Select * From TABLE Where FieldNm = NAME")
With objRecordset
.Open
.MoveFirst
Do Until .EOF
Worksheets("WORKSHEET").Cells(14, 1) = objRecordset.Fields.Item("FieldNm")
.MoveNext
Loop
End With
End Sub
The debug goes to the .Open in my With objConnection block. Before that I was having problems with the .MoveNext method.
Worksheets("WORKSHEET").Range("A14").CopyFromRecordset objRecordset?objRecordset.Open sSelectStatement, objConnectionSelect * From TABLE Where FieldNm = NAME. Also, wrap your sub in error handling to get specific message.