Vba code for backup issue

Kaybaj

Registered User.
Local time
Yesterday, 22:45
Joined
Apr 15, 2010
Messages
67
Hi all i tried creating a backup code with vba, but i get this message
"FILE NOT FOUND", this is confusing as i feel i've put everything in place.
This my code:

Function BACKUPS()
Dim fso As Object
Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String
Dim db As New Access.Application
Dim buf As String
Dim strBu As String

sSourceFile = CurrentProject.Name
sSourcePath = CurrentProject.Path
sBackupFile = CurrentProject.Name
sBackupPath = CurrentProject.Path
buf = CurrentProject.Path & "\Backups\"

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True
Set fso = Nothing

Beep
MsgBox "Backup was successful and saved @ " & Chr(13) & Chr(13) & sBackupPath & Chr(13) & Chr(13) & "The backup file name is " & Chr(13) & Chr(13) & sBackupFile, vbInformation, "Backup Completed"

End Function

Any help would be highly appreciated
 
I hate breaking this to you , but "I feel" cuts no ice with Access. :D

Check what your variables actually contain

debug.print myVariable

shows the value in the Immediate Window
 
Thank you all for the advise, I'll try it.
 
I tried putting some error handler but still the same response, maybe you guys can try it as the can be in any application unaltered.

Function BACKUPS()
On Error GoTo Err_BACKUPS
Dim fso As Object
Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String
'Dim db As New Access.Application
Dim buf As String
Dim strBu As String

sSourceFile = CurrentProject.Name
sSourcePath = CurrentProject.Path
sBackupFile = CurrentProject.Name
sBackupPath = CurrentProject.Path & "\Backups\"

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True
Set fso = Nothing

Beep
MsgBox "Backup was successful and saved @ " & Chr(13) & Chr(13) & sBackupPath & Chr(13) & Chr(13) & "The backup file name is " & Chr(13) & Chr(13) & sBackupFile, vbInformation, "Backup Completed"
Exit_BACKUPS:
Exit Function
Err_BACKUPS:
MsgBox Err.Number & Err.Description
Resume Exit_BACKUPS
End Function
 
hi all thanks very much, because i've seen the error, it was due to lack of a trailing back slash in my sSourcepath
i replaced:
sSourcePath = CurrentProject.Path
with
sSourcePath = CurrentProject.Path & "\"
and it was perfect.
thank you all
 
I found your thread and am having a similar problem. I have written the code, but when I click on the "Backup Button" nothing happens, I don't even get an error. This is what I have written

Function BACKUPS()
Dim fso As Object
Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String
Dim db As New Access.Application
Dim buf As String
Dim strBu As String
sSourceFile = Sudweeks.Name
sSourcePath = Sudweeks.Path & "\"
sBackupFile = Sudweeks.Name
sBackupPath = C:\Documents And Settings\ My Documents\Sudweeks.Path
buf = Sudweeks.Path & "\Backups\"
Set fso = CreateObject
fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True
Set fso = Nothing
Beep
MsgBox "Backup was successful and saved @ " & Chr(13) & Chr(13) & sBackupPath & Chr(13) & Chr(13) & "The backup file name is " & Chr(13) & Chr(13) & sBackupFile, vbInformation, "Backup Completed"
End Function
Private Sub Backup_Click()
End Sub


If you can help me I would really appreciate it. I am trying to automatically save the backup from a usb stick onto the C: drive
 
I have the following code. I am not a programmer, so go easy on me!
I am trying to put a button on a form that the user clicks, and a copy of the database is made to c:\Documents And Settings\My Documents.

Do I have to have Private Sub in front of the code, and End Sub at the bottom of the code ? I did try that but received a compile error.

Please can anyone help me?

Function BACKUPS()
On Error GoTo Err_BACKUPS
Dim fso As Object
Dim sSourcePath As String
Dim sSourceFile As String
Dim sBackupPath As String
Dim sBackupFile As String
'Dim db As New Access.Application
Dim buf As String
Dim strBu As String
sSourceFile = CurrentProject.Name
sSourcePath = CurrentProject.Path & "\"
sBackupFile = CurrentProject.Name
sBackupPath = CurrentProject.Path & "\Backups\"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile sSourcePath & sSourceFile, sBackupPath & sBackupFile, True
Set fso = Nothing
Beep
MsgBox "Backup was successful and saved @ " & Chr(13) & Chr(13) & sBackupPath & Chr(13) & Chr(13) & "The backup file name is " & Chr(13) & Chr(13) & sBackupFile, vbInformation, "Backup Completed"
Exit_BACKUPS:
Exit Function
Err_BACKUPS:
MsgBox Err.Number & Err.Description
Resume Exit_BACKUPS
End Function
 
aussie This code actually goes in a module not in a sub. Please reasearch how to create a module in access and just paste the code in there.
 
I've done it!!!:D For all you non programmers out there (including myself)

To get a database to backup onto your c:\ drive

1. In Forms, Design View Create a button.
2. Select Properties for the button, and under the "Other" Tab in "Name" type Backup
3. Select "On Click"
4. Select "Code Builder"
5. Copy and paste the following

Private Sub Backup_Click()
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile CurrentProject.FullName, "C:\", True
Set fs = Nothing
MsgBox "Database has been backed up successfully"
End Sub
Private Sub Command32_Click()
End Sub

5. Close Code Builder and you're done. When you click on the button you will receive a message to say the "database has been backed up successfully"
 

Users who are viewing this thread

Back
Top Bottom