PDA

View Full Version : Database back up with Timestamp



wiley2111
08-09-2016, 01:18 PM
Hello, I am creating VBA that backups my access database in a different folder... I got the code to work but I want to add a timestamp to the newly saved file. Below are my 2 codes... The first one works just fine but I want to add the timestamp. The second code doesn't work when I attempt to add the timestamp myself... Does anyone know what I am doing wrong. Thank you.

1st code:

Private Sub Command1_Click()

Dim YesOrNoAnswerToMessageBox As String
Dim QuestionToMessageBox As String
QuestionToMessageBox = "Do you want to backup test Database?"
YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "VBA Expert or Not")
If YesOrNoAnswerToMessageBox = vbYes Then

Dim fs As Object
Dim oldPath As String, newPath As String
oldPath = "C:\Users\wiley2111\Desktop" 'Folder file is located in
newPath = "C:\Users\wiley2111\Desktop\test" 'Folder to copy file to
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath & "\" & "test database.accdb", newPath & "\" & "test database" & "_" & ".accdb" 'This file was an .accdb file
Set fs = Nothing


Else
MsgBox "You chose not to backup the database"
End If

End Sub





2nd code: (All changes in red font)



Private Sub Command1_Click()

Dim YesOrNoAnswerToMessageBox As String
Dim QuestionToMessageBox As String
Dim Now As Date

QuestionToMessageBox = "Do you want to backup test Database?"
YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "VBA Expert or Not")

Now = Format(Now(), "yyyy-MM-dd hh:mm:ss")
If YesOrNoAnswerToMessageBox = vbYes Then

Dim fs As Object
Dim oldPath As String, newPath As String
oldPath = "C:\Users\wiley2111\Desktop" 'Folder file is located in
newPath = "C:\Users\wiley2111\Desktop\test" 'Folder to copy file to
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath & "\" & "test database.accdb", newPath & "\" & "test database" & "_" & Now & ".accdb" 'This file was an .accdb file
Set fs = Nothing


Else
MsgBox "You chose not to backup the database"
End If

End Sub