troubleshooting Question

Trying to create new database file and export existing tables (2 of them) to the new database file via VBA code

Avatar of SteveL13
SteveL13Flag for United States of America asked on
Microsoft Access
3 Comments1 Solution119 ViewsLast Modified:
I am trying to create a new database file and export existing tables (2 of them) to the new database file via VBA code.  This is my code so far.  But I get an error:

Run-time error 3051.  The Microsoft Access database engine cannot open or write to the file 'C:\Database Name.accdb'.  It is already opened exclusively by another user, or you need permission to view and write its data.


Code:

Private Sub cmdArchiveTables_Click()

   Dim ws As Workspace
   Dim db As Database
   Dim LFilename As String
   Dim tblDataHeader As Object
   Dim tblDataDetail As Object

   'Get default Workspace
   Set ws = DBEngine.Workspaces(0)

   'Path and file name for new mdb file
   LFilename = "C:\Database Name.accdb"

   'Make sure there isn't already a file with the name of the new database
   If Dir(LFilename) <> "" Then Kill LFilename

   'Create a new accdb file
   Set db = ws.CreateDatabase(LFilename, dbLangGeneral)

   'Export tables to new database
   DoCmd.TransferDatabase acExport, "Microsoft Access", LFilename, acTable, "tblDataHeader", "tblDataHeader", False
   DoCmd.TransferDatabase acExport, "Microsoft Access", LFilename, acTable, "tblDataDetail", "tblDataDetail", False

   db.Close
   Set db = Nothing
    
End Sub
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros