Ready to learn more?
Free
Estimate!
Tell us about your project
needs. A knowledgeable
Project Manager will
provide a free estimate.
Access Database Development | Business Software Experts | Microsoft Access Technical Support
Importing External Database Tables Using Code
|
Database Professional
Sometimes a Microsoft Access database user may find it desirable to automate the process of importing tables from
an external Access database file into their current database. Automation can greatly simplify tasks that are repeated
on a regular basis or are very complex. Besides simplifying these tasks, automation can save you time, which will pay
off in the long run. Below is a segment of code that provides an example of how to accomplish this.
‘*******CODE*******
Dim root As String
Dim srcDB As database
Dim tdf As TableDef
Dim x As Integer
Root = “C:\”
Set srcDB = OpenDatabase(root)
For Each tdf In srcDB.TableDefs
x = 0
If (tdf.Name = "CULVERT") Then
DoCmd.TransferDatabase acImport, "Microsoft Access", root, acTable, "CULVERT", "CULVERT"
errorCheck = 1
End If
Next tdf
‘******************
After the variable declarations, the first line of code sets the variable “root” equal to “C:\” which is the location of the
external database holding the tables we wish to import. The next line sets our database variable equal to the external
database. The next lines of code search through the external database to find the table named “CULVERT.” If this
table is found, the table is imported using the DoCmd function. The “acImport” variable simply tells the function that we
wish to import information from the database. “Microsoft Access” specifies the type of database containing the table.
Following that is the folder location (“root”) of the external database. The variable actable specifies that the object we
wish to import is a table. The next two strings contain the table name to import and the name to assign to the imported
table when it is imported into the current database. It should be noted that if the table named to be imported already
exists in the current database, MS Access will automatically rename the table being imported.
Once the desired tables have been imported, they can be utilized just like any of the other tables in the database.