VBA ODBC Connect to SQL Database

VBA to pull SQL database using ODBC connection.  First you have to setup the ODBC connection in ODBC Data Source Administrator.  Then the VBA references the ODBC name for the connection.  Make sure to enable Microsoft ActiveX Data Object Library otherwise you’ll get the error “Compile error:  User-defined type not defined.”.

Dim cnDB As New ADODB.Connection

'Enable Microsoft ActiveX Data Objects v.0 Library
Dim rsRecords As New ADODB.Recordset

'Open the ODBC Connection using this statement
cnDB.Open "DataWarehouse"
rsRecords.Open "SELECT TOP 10 * FROM account.data_account", cnDB

'Print the table to Excel
Range("A1").CopyFromRecordset rsRecords

'Close everything and set the references to nothing
Set rsRecords = Nothing
Set cnDB = Nothing