0

I just want to Create Backup file of access, so users from fronted can press button and a backup is created.

I am trying this code, This makes the copy of database, but also copies my VBA Code. I just want to copy Tables. No forms, No reports.

Is it possible to ZIP it from VBA??

Function fMakeBackup()

Dim Source As String
Dim Target As String
Dim retval As Integer


Source = CurrentDb.Name

Target = "C:\Users\Documents\FileName"
Target = Target & Format(Date, "dd-mm") & "   "
Target = Target & Format(Time, "hh-mm") & ".accdb"

' create the backup
retval = 0
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
retval = objFSO.CopyFile(Source, Target, True)
Set objFSO = Nothing

End Function

Update 1 Using solution given by @Sergey

I modified code as below

Function fMakeBackup()

Dim Target As String
Target = "C:\Users\adarsh.madrecha\Downloads\Delete "
Target = Target & Format(Date, "dd-mm") & " "
Target = Target & Format(Time, "hh-mm") & ".accdb"

' create the Blank file for copying
Access.DBEngine.CreateDatabase Target, DB_LANG_GENERAL

Dim tdf As TableDef    
For Each tdf In CurrentDb.TableDefs
   DoCmd.CopyObject Target, , acTable, tdf.Name
Next

End Function

When I execute this, a popup is shown

Access Security Box

If I click on either on the options, the code gives error.

Runtime error 3024 
Copy object action was canceled 

2 Answers 2

2

You can copy database objects one-by-one to remote database. For instance for tables use code like this:

DoCmd.CopyObject TargetFileName, "MyLocalTableName", acTable, "MyRemoteTableName"

Target file should be already created.

For coping all local tables to remote database:

Dim tdf As TableDef

For Each tdf In CurrentDb.TableDefs
    DoCmd.CopyObject TargetFileName, tdf.Name, acTable, tdf.Name
Next
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, But when I run your code, getting Runtime error 3024 Could not find the file . Should I also another line of code before your code ? I am new in VBA Programming.
As I said "Target file should be already created", so you need to create it manually first or copy from empty template using procedure you already used
I created a blank access database file using Access.DBEngine.CreateDatabase Target, DB_LANG_GENERAL But still getting Runtime error 3024 Copy object action was canceled
Not able to add new line in comments, hence making update 1 to the above question.
So, is there any way copy remote objects to local database?
1

Access by default will not allow your newly-created target database to be enabled, hence the error you are getting. If you create the target and make sure it's enabled via the Trust Center Settings, you can get past this issue (but it also means you need to rethink how you rename the database by adding the date and time).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.