Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

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
Avatar of Rey Obrero (Capricorn1)
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers