What I'm trying to do is pretty simple:
- Iterate through all of the
.accdbfiles in a directory - Open them and export all the tables to separate
.xlsxfiles
The issue I'm running into is the scope of DoCmd and I'm having difficulty changing it. The issue is coming up because I'm running the VBA from what is basically and empty Database1.accdb. When the VBA gets to DoCmd.TransferSpreadsheet, it is using CurrentDb and not the Access file that was opened with OpenDatabase. It is saying a table that is clearly does see (Debug.Print(tbl.Name) works) doesn't exist.
See below:
Option Compare Database
Public Const PROJECT_PATH = "\\192.168.1.2\Migration\"
Public Const DESC_PATH = PROJECT_PATH & "Descriptions\"
Public Const DESC_DB_PATH = DESC_PATH & "DBs\"
Public Const DESC_EXPORTS = DESC_PATH & "Exports\"
Sub ExportDescriptionTables()
Dim wrkSpace As Workspace, _
db As Database, _
tbl As TableDef
' ************************** TEMPLATE FILES - BEGIN *************************
Set dbDirObj = CreateObject("Scripting.FileSystemObject")
Set dbFolder = dbDirObj.GetFolder(DESC_DB_PATH)
Set dbFiles = dbFolder.Files
' ************************** TEMPLATE FILES - END ***************************
' Create the Access object for this file
Set wrkSpace = CreateWorkspace("", "admin", "", dbUseJet)
' Iterate through each Access file
For Each dbFile In dbFiles
' Open each Access database file
Set db = wrkSpace.OpenDatabase(DESC_DB_PATH & dbFile.Name)
' Iterate through each table
For Each tbl In db.TableDefs
' Export based on the table name matching specific criteria
If Left(tbl.Name, 1) <> "~" And Left(tbl.Name, 4) <> "MSys" Then
Debug.Print (tbl.Name)
' ################### THIS WHERE THE ERROR OCCURES ##############
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, tbl.Name, DESC_EXPORTS & tbl.Name & dbFile.Name, True, tbl.Name
End If
Next tbl
' Close database
db.Close
' Clear out access object for this file
Set db = Nothing
Next dbFile
End Sub
So my questions:
- Is there a way I'm simply not seeing for setting the scope of
DoCmd? - If not, what would be a better approach to this (perhaps running from Excel potentially forcing the
CurrentDbto be the only open Acccess DB, which should bringDoCmdinto proper scope)?