Sub AccessDBAutomation()
Dim cnct As New ADODB.Connection
Dim recordset As New ADODB.recordset
Dim col As Integer
Cells.Clear
'Open the connection
cnct.Open "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Users\Ravi\Desktop\budget data.accdb;"
'Create RecordSet
recordset.Open "SELECT * FROM Budget", cnct, adOpenKeyset
'Write the field names
For col = 0 To recordset.Fields.Count - 1
Range("A1").Offset(0, col).Value = recordset.Fields(col).Name
Next
'Write the recordset
If recordset.RecordCount > 0 Then
Range("a1").Offset(1, 0).CopyFromRecordset recordset, recordset.RecordCount, recordset.Fields.Count
End If
Set recordset = Nothing
Set cnct = Nothing
End Sub
No comments:
Post a Comment