As ever, you use this free program at your own risk.
Feel free to use the free software on this site but please honor the copyright.
This page will walk through the steps needed in order to connect an
Access™ database to Excel™. The collateral (including the video
demonstration) is available as a Zip file. It includes the VBA code, the
sample Access database and the Excel spreadsheet.
If you chose to use the Access database correctly you will need to change the
VBA code to point to the fully qualified location where you have placed
the database.
As normal the usual disclaimers apply.
|
Public Sub ConnectDatabase() Dim conn As New ADODB.Connection, rs As New ADODB.Recordset Dim DBPATH, PRVD, connString, qry As String Dim WorkSheet As String Worksheet = "Sheet1 " ' Specify the target Worksheet ' Declare fully qualified name of database. DBPATH = " D:\Abbydale Systems\Access Databases\Abbydale Sample.accdb;" PRVD = "Microsoft.ace.OLEDB.12.0;" ' Connection Provider ' Declare the ODBC Connection Name connString = "Provider=" & PRVD & "Data Source=" & DBPATH conn.Open connString ' Open the connection ' Build the query qry = "SELECT * FROM tblSample1 ORDER BY tblSample1.[fldManufacturer] ,tblSample1.[fldColor] ;" rs.Open qry, conn, adOpenStatic ' Now run it If rs.RecordCount > 0 Then x = 1 Do Until rs.EOF ThisWorkbook.Sheets(Worksheet).Cells(x, 1).Value = rs.Fields(1).Value ' Make ThisWorkbook.Sheets(Worksheet).Cells(x, 2).Value = rs.Fields(2).Value ' Model ThisWorkbook.Sheets(Worksheet).Cells(x, 3).Value = rs.Fields(3).Value ' Color rs.MoveNext ' Move to next record x = x + 1 ' Next output line Loop End If rs.Close ' Close the recordset conn.Close ' Close the database connection Set rs = Nothing Set conn = Nothing End Sub |
The code alongside is available either in the Zip file or by clicking the button
below
If you chose to rename the tables and fields in the database you will need to change the fields denoted in purple to match your own table and field names. You should now be able to read the data from the databases into the worksheet. That is all there is to it. |
Abbydale Systems LLC Lic. 802696149. All rights reserved.