Good Afternoon,
Sorry for the runtime error in the header, but I am getting more confused as I go.
I am trying to get the code below to open access, run a query and pull the data in to Excel/
The code is debugging at "Opening the connection" I have had this error before, but cant remeber for the life of me how I fixed it. I think I changed something in the >Tools>references but am not sure.
Any help greatly appreciated
Thanks in advance
Gavin
Sorry for the runtime error in the header, but I am getting more confused as I go.
I am trying to get the code below to open access, run a query and pull the data in to Excel/
The code is debugging at "Opening the connection" I have had this error before, but cant remeber for the life of me how I fixed it. I think I changed something in the >Tools>references but am not sure.
Any help greatly appreciated
Thanks in advance
Gavin
Rich (BB code):
Dim cnn As New ADODB.Connection
Dim rs As Object
Dim AccessFile As String
Dim strQuery As String
Dim i As Integer
'Disable screen flickering.
Application.ScreenUpdating = False
'Set the name of the query you want to run adn retrieve the data.
strQuery = "GavinMazzaOpen"
On Error Resume Next
'Create the ADODB connection object.
Set cnn = CreateObject("ADODB.connection")
'Check if the object was created.
If Err.Number <> 0 Then
MsgBox "Connection was not created!", vbCritical, "Connection Error"
Exit Sub
End If
On Error GoTo 0
'Open the connection. ERRORS HERE
cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;" & _
"Data Source=L:\SHARED\Multi Tenancy\New Housing\customer journey\Developer Work\Developer Workload.accdb"
On Error Resume Next
'Create the ADODB recordset object.
Set rs = CreateObject("ADODB.Recordset")
'Check if the object was created.
If Err.Number <> 0 Then
'Error! Release the objects and exit.
Set rs = Nothing
Set cnn = Nothing
'Display an error message to the user.
MsgBox "Recordset was not created!", vbCritical, "Recordset Error"
Exit Sub
End If
On Error GoTo 0
'Set thee cursor location.
rs.CursorLocation = 3 'adUseClient on early binding
rs.CursorType = 1 'adOpenKeyset on early binding
'Open the recordset.
rs.Open strQuery, cnn
'Check if the recordet is empty.
If rs.EOF And rs.BOF Then
'Close the recordet and the connection.
rs.Close
cnn.Close
'Release the objects.
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Exit Sub
End If
'Copy the recordset headers.
For i = 0 To rs.Fields.Count - 1
Sheets("Daily_Log").Cells(1, i + 1) = rs.Fields(i).name
Next i
'Write the query values in the sheet.
Sheets("Daily_Log").Range("A6").CopyFromRecordset rs
'Close the recordet and the connection.
rs.Close
cnn.Close
'Release the objects.
Set rs = Nothing
Set cnn = Nothing
'Adjust the columns' width.
Columns("A:B").AutoFit
'Enable the screen.
Application.ScreenUpdating = True
'Inform the user that the macro was executed successfully.
MsgBox "All data were successfully retrieved from the '" & strQuery & "' query!", vbInformation, "Done"