Not sure if anyone's posted anything similar before, but the below, which is derived from code passed on by Bob Larson (thanks again Bob), is for a 'backup this database' button for users who need clear instructions how and reminders to regularly backup a standalone database. I found that FileCopy would not work while the database was open because permission was denied, so this is the way around it.
--------------------------------------
---------------------------------------
--------------------------------------
Code:
Private Sub BackUp_Click()
Dim CopyFile As String
Dim FileName As String
Dim FileAddName As String
Dim CopyAddName As String
Dim CopyAddName2 As String
Dim todaysdate As String
Dim CopyNameDef As String
Dim FileAdd As String
Dim CopyAdd As String
Dim strFilter As String
If MsgBox("DO YOU WANT TO BACK UP THE DATABASE?", vbYesNo) = vbYes Then
' record date of backup in a table (so can be displayed clearly on main menu)
DoCmd.SetWarnings False
DoCmd.openquery "BackupDate1"
DoCmd.openquery "BackupDate2"
' set name of database to be copied (without the '.mdb' bit):
FileName = "NameOfTheDatabase"
' set folder address for database
FileAdd = "D:\Users\MyName\FolderWhereDatabaseIsKept\"
' set address for second backup location which the user will not see
CopyAdd = "D:\Users\MyName\FolderWhereOtherBackupsAreKep t\"
' set the name and address of the file to be backed up:
FileAddName = FileAdd & FileName & ".mdb"
' include date in a default name for the new backup file
todaysdate = Date$
CopyNameDef = FileName & todaysdate & ".mdb"
'Ask for alternative name for backup file
strFilter = ahtAddFilterItem(strFilter, "Access Files (*.mdb)", "*.mdb")
CopyAddName = ahtCommonFileOpenSave(OpenFile:=False, Filter:=strFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY, FileName:=CopyNameDef, _
InitialDir:=FileAdd, DialogTitle:="Backup Database")
CopyAddName2 = CopyAdd & FileName & todaysdate & ".mdb"
' set the file name of the batch file to create:
CopyFile = FileAdd & "BackupDb.cmd"
' create the batch file:
Open CopyFile For Output As #1
Print #1, "Echo Off"
Print #1, "ECHO Making backup of database"
Print #1, "Copy /Y """ & FileAddName & """ """ & CopyAddName & """"
Print #1, ""
Print #1, "Copy /Y """ & FileAddName & """ """ & CopyAddName2 & """"
Print #1, ""
Print #1, "START /I MSAccess.exe """ & FileAddName & """"
Close #1
' run the batch file:
Shell CopyFile
'close the database:
DoCmd.Quit
End If
End Sub
---------------------------------------
Last edited by a moderator: