This runs but returns an "run time" and "automation error" I dont know why.
Thanks in advance for help
Private Sub CommandButton1_Click()
Dim cnn As Object, strQuery As String, rst As Object
Dim strPathToDB As String, strFormula As String, i As Long
Dim wks As Worksheet
Dim lngNextNum As Long, lngRow As Long, lngCol As Long
Dim varData
' output to activesheet
Set wks = ActiveSheet
' Path to database
strPathToDB = "Z:\spreadchecks stand alone.mdb"
Set cnn = CreateObject("ADODB.Connection")
' open connection to database
With cnn
.ConnectionTimeout = 500
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & strPathToDB & ";"
.Open
.CommandTimeout = 500
End With
' SQL query string - change to suit
strQuery = "SELECT Data.Date, Data.ValueRight,"
' create new recordset
Set rst = CreateObject("ADODB.Recordset")
' open recordset using query string and connection
With rst
.Open strQuery, cnn, 3, 2, 1
' check for records returned
If Not (.EOF And .BOF) Then
'Populate field names starting at col AA
For i = 1 To .Fields.Count
wks.Cells(1, 27 + i) = .Fields(i - 1).Name
Next i
' Copy data from AA2
wks.Cells(2, "AA").CopyFromRecordset rst
End If
.Close
End With
' clean up
Set rst = Nothing
cnn.Close
Set cnn = Nothing
End Sub
Bookmarks