Sub Main()
On Error GoTo EH
'http://analysistabs.com/excel-vba/ado-sql-macros-connecting-database/
'Step 1:Add reference for Microsoft Activex Data Objects Library
'1. Go to VBE (Alt+F11) and Select References.. from Tools Menu.
'2. Then select ” Microsoft Activex Data Objects Library” from the list.
'3. And Create sub procedure to write the code:
'Step 2: Create the Connection String with Provider and Data Source options
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.connection
Dim rs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
sconnect = "xxxxxx;"
'Step 3: Open the Connection to data source
Conn.Open sconnect
sSQLSting = "Select * From Delays"
'Step 5: Get the records by Opening this Query with in the Connected data source
rs.Open sSQLSting, Conn
'Step 5a - Pull the headers
For i = 0 To rs.Fields.Count - 1
Sheet1.Cells(1, i + 1) = rs.Fields(i).Name
Next i
'Step 6: Copy the reords into our worksheet
Sheet1.Range("A2").CopyFromRecordset rs
'Step 7: Close the Record Set and Connection
'Close Recordset
rs.Close
'Close Connection
Conn.Close
Exit Sub
EH:
MsgBox Err.Description
End Sub
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY