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:
Other People Are Reading
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.
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)
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for