How to Open MDB With VBA

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Open MDB With VBA
Use VBA to connect to an Access database. (computer image by blaine stiger from

Visual Basic for Applications (VBA) provides programmers with a lightweight tool to interact with Office products in software such as Excel. VBA is used in Excel, Word, Access and PowerPoint. An MDB file is an Access database. You can use VBA to connect to an Access database in any Microsoft Office application.

Skill level:


  1. 1

    Create the database object and open the MDB file. The following code sets the MDB file to which you want to connect:

    Dim dB As Database

    Set dB = OpenDatabase("theDB.mdb")

    Replace "theDB" with the name of your MDB file.

  2. 2

    Open one of your MDB file tables. You need to specify the table that has the records you want to view. The following code connects to a table named "theTable":

    Dim rs As RecordSet

    Set rs = db.OpenRecordSet("theTable", dbOpenDynaset)

  3. 3

    Print out the first record in the table. You can add, delete, retrieve and edit records in the database. In this example, the first record is simply printed to the screen. The following code prints it to the screen:


    This code prints out the "Name" field in your connected table. Replace "name" with the field name in your MDB table.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.