Help backing up a database that is stored on a USB drive (1 Viewer)

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
Hi all,

I have created a database for my girlfriend to use at work to create quotes, she wants to be able to run it from a USB drive on both her work PC & her home laptop, she also wants to be able to back her database up at the push of a button.

On the databases I have created at work I run them as split databases which are stored on a server, the code I use on a button works (I think I might have found it on here & modified it a bit, thanks to the original creator of the code) fine but I created a folder on a USB with the same name as hers (both the drive name & folder name) & the drive has the same drive letter assigned but her database that I split won't work on her laptop, I presume the backend is also picking up the PC name as well as the drive name & folder name.

This is the code I use on my databases
Code:
Private Sub Button_Backup_Click()
Dim str As String
Dim buf As String
Dim MD_Date As Variant
Dim fs As Object
Dim source As String
Const conPATH_FILE_ACCESS_ERROR = 75
On Error GoTo Backup_Button_Backup
'buf = Back Up Folder
'buf is created if it does not exist
'CurrentProject.Path = F:\Access Quoting\Back End
buf = CurrentProject.Path & "\Backups\"
MkDir buf
    Resume Backup_Button_Backup
Backup_Button_Backup:
'Use yyyy-mm-dd hh-mm-ss as folder name. Change as needed.
MD_Date = Format(Date, "yyyy.mm.dd ") & Format(Time, "- hh.mm")
str = CurrentProject.Path & "\Backups\" & MD_Date
'Source = where the data is stored
source = CurrentProject.Path & "\Back End\"
MkDir str
Set fs = CreateObject("Scripting.FileSystemObject")
'Change the file extension as needed
fs.CopyFile source & "*.accdb", str
Set fs = Nothing
MsgBox "Access Quoting database backup  " & vbCrLf & MD_Date & vbCrLf & "Completed successfully! Press Ok to close", _
        vbInformation, "Backup Successful"
Exit_Button_Backup:
    DoCmd.Quit
  Exit Sub
Err_Button_Backup:
  If Err.Number = conPATH_FILE_ACCESS_ERROR Then
    MsgBox "The following Path, " & str & ", already exists or there was an Error " & _
           "accessing it!", vbExclamation, "Path/File Access Error"
  Else
    MsgBox Err.Description, vbExclamation, "Error Creating " & str
  
End If
    Resume Exit_Button_Backup
End Sub


Is there a way to change the code so a split database can still be backed up on her USB on both her home laptop & her work PC & for the database to work when split when plugged into both computers? Or is there a way to backup a non-split database if that will make it work?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:21
Joined
Oct 29, 2018
Messages
21,446
Hi Paul. Since the drive letter for the USB drive is automatically assigned by Windows and may change from time to time, it's probably better to use a relative path rather than a specified drive letter in your code. It does look like you're already doing this by using the CurrentProject.Path property. Have you tried stepping through your code to see why it's not working or where it was failing?


FWIW: This is what I use to backup and compact the BE.
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
Hi Paul. Since the drive letter for the USB drive is automatically assigned by Windows and may change from time to time, it's probably better to use a relative path rather than a specified drive letter in your code. It does look like you're already doing this by using the CurrentProject.Path property. Have you tried stepping through your code to see why it's not working or where it was failing?


FWIW: This is what I use to backup and compact the BE[/url].

Thank you for your reply @theDBguy

I will have to give your code a try, I think there will still be a problem with the database being split, if it doesn't work once I have split it while it is on my USB & named the same with the same drive letter & put it on hers do you think it will make a difference if she splits it straight on her USB drive?

With the CurrentProjectPath can I change the line & remove the "F:" & leave the rest?
 

vba_php

Forum Troll
Local time
Yesterday, 23:21
Joined
Oct 6, 2019
Messages
2,884
Paul, I thought I would give you this but I'm not sure what you can do with it. It was written by an unknown person years ago to back up the back end file:
Code:
Public Function BackupSource()

'******************************************************************************
'                                                                             *
'Author: Unknown                                                              *
'Modified By: Adam Evanovich                                                  *
'Date: 9/25/2010                                                              *
'Purpose: To backup the source BackEnd file of a database                     *
'                                                                             *
'Arguments: None                                                              *
'                                                                             *
'******************************************************************************

On Error GoTo Err_BackupSource

Dim strBu As String
   Dim buf As String
      Dim MD_Date As Variant
         Dim fs As Object
            Dim strSourceName As String
               Dim strSourceFile As String

                  Const conPATH_FILE_ACCESS_ERROR = 75

strSourceName = CurrentProject.Name
strSourceFile = CurrentProject.Path
buf = CurrentProject.Path & "\Backups\"

   If GetAttr(buf) <> vbDirectory Then
      MkDir buf
   End If

Continue:

      MD_Date = Format(Date, "yyyy-mm-dd ") & Format(time, "hh-mm-ss")
      strSourceFile = CurrentProject.Path
      strBu = CurrentProject.Path & "\Backups\" & MD_Date & "\"
   
         MkDir (strBu)
         
            Set fs = CreateObject("Scripting.FileSystemObject")
               fs.CopyFile strSourceFile & "\" & strSourceName, strBu
            Set fs = Nothing
 
'Successful
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successful!", _
        vbInformation, "Backup Successful"
 
Exit_BackupSource:
  Exit Function
 
Err_BackupSource:
  If err.Number = conPATH_FILE_ACCESS_ERROR Then
    MsgBox "The following Path, " & strBu & ", already exists or there was an Error " & _
           "accessing it!", vbExclamation, "Path/File Access Error"
  Else
      If err.Number = 53 Then
         MkDir buf
            GoTo Continue
      Else
         MsgBox err.Description, vbExclamation, "Error Creating " & strBu
      End If
  End If

End Function '//LL
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
Paul, I thought I would give you this but I'm not sure what you can do with it. It was written by an unknown person years ago to back up the back end file:
Code:
Public Function BackupSource()

'******************************************************************************
'                                                                             *
'Author: Unknown                                                              *
'Modified By: Adam Evanovich                                                  *
'Date: 9/25/2010                                                              *
'Purpose: To backup the source BackEnd file of a database                     *
'                                                                             *
'Arguments: None                                                              *
'                                                                             *
'******************************************************************************

On Error GoTo Err_BackupSource

Dim strBu As String
   Dim buf As String
      Dim MD_Date As Variant
         Dim fs As Object
            Dim strSourceName As String
               Dim strSourceFile As String

                  Const conPATH_FILE_ACCESS_ERROR = 75

strSourceName = CurrentProject.Name
strSourceFile = CurrentProject.Path
buf = CurrentProject.Path & "\Backups\"

   If GetAttr(buf) <> vbDirectory Then
      MkDir buf
   End If

Continue:

      MD_Date = Format(Date, "yyyy-mm-dd ") & Format(time, "hh-mm-ss")
      strSourceFile = CurrentProject.Path
      strBu = CurrentProject.Path & "\Backups\" & MD_Date & "\"
   
         MkDir (strBu)
         
            Set fs = CreateObject("Scripting.FileSystemObject")
               fs.CopyFile strSourceFile & "\" & strSourceName, strBu
            Set fs = Nothing
 
'Successful
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successful!", _
        vbInformation, "Backup Successful"
 
Exit_BackupSource:
  Exit Function
 
Err_BackupSource:
  If err.Number = conPATH_FILE_ACCESS_ERROR Then
    MsgBox "The following Path, " & strBu & ", already exists or there was an Error " & _
           "accessing it!", vbExclamation, "Path/File Access Error"
  Else
      If err.Number = 53 Then
         MkDir buf
            GoTo Continue
      Else
         MsgBox err.Description, vbExclamation, "Error Creating " & strBu
      End If
  End If

End Function '//LL

Thank you for that code, I've just tested it & it can back up an un-split database as well which even though I have learnt that you should split a database but I think she is going to get an error, I could use that code on her database & try to edit it so it shuts the database down after the backup has been created like I did with the code I use on my other databases.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:21
Joined
Sep 21, 2011
Messages
14,216
FWIW I used this code to backup from a normal Access Switchboard
You will need to create a Backup folder under the BE path?
Amend the table name for a table in GetAccessBE_PathFilename for your BE

Code:
Sub CreateBackup(Optional strDBType As String)
    Dim strDBpath As String, ext As String, tmp As String
    Dim strPath As String, strBackupPath As String, strDB As String
    
    
    'tmp = CurrentDb.Name    'or maybe this should be the name of your BE
    'strDBType = "FE"
    strDBpath = GetAccessBE_PathFilename("tblUser")
    'strPath = Left(strDBpath, InStrRev(strDBpath, "\"))
    strPath = Left(strDBpath, Len(strDBpath) - InStrRev(strDBpath, "\") + 1)
    strBackupPath = strPath & "Backup\"
    
    'Will now backup front and back end database
    If strDBType = "FE" Then
        strDBpath = CurrentDb.Name
    End If
    strDB = Right(strDBpath, Len(strDBpath) - InStrRev(strDBpath, "\"))
    
    With CreateObject("Scripting.FileSystemObject")
        'ext = "." & .GetExtensionName(tmp)
        tmp = strBackupPath & Format(Now(), "yyyymmdd_hhnnss") & "_" & strDB
        .CopyFile strDBpath, tmp
    End With
    MsgBox strDBType & " Database saved as " & tmp
    
    
End Sub
which uses
Code:
Function GetAccessBE_PathFilename(pTableName As String) As String
'strive4peace

   ' RETURN
   '  the file path and file name of the BE database
   '  "" if the table is not linked
   
   On Error GoTo Proc_Err
   
   Dim db As DAO.Database _
      , tdf As DAO.TableDef
   
   GetAccessBE_PathFilename = ""
   
   Set db = CurrentDb
   Set tdf = db.TableDefs(pTableName)
   
   If Len(tdf.Connect) = 0 Then
      GoTo Proc_Exit
   End If
   
   ' look at Connect string - Database Type is the first thing specified
   ' if the BE is Access
   If InStr(tdf.Connect, ";DATABASE=") <> 1 Then
      GoTo Proc_Exit
   End If
   
   GetAccessBE_PathFilename = Mid(tdf.Connect, 11)
    
Proc_Exit:
   On Error Resume Next
   Set tdf = Nothing
   Set db = Nothing
   Exit Function
  
Proc_Err:
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   GetAccessBE_PathFilename"

   Resume Proc_Exit
   Resume
             
End Function

Called with
Code:
Function CreateBackupFE()
' Have to do it this way as Switchboard does not allow parameters.
CreateBackup ("FE")
End Function
Function CreateBackupBE()
' Have to do it this way as Switchboard does not allow parameters.
CreateBackup ("BE")
End Function




HTH
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
Thank you @Gasman

I will have to try that one too.

I'm not sure any of the codes shared will help with the backend issue though & I'm worried she will split the database on her USB drive when it is in her laptop & it won't open when she uses it in her work PC.

@vba_php 's code works really well but I am having trouble altering the code so it closes the database down after it has backed up, can someone help with that please

Edit

I'm still not sure how to sort the split database problem but I've sorted the problem with getting the database to close after the backup

I changed

Code:
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successful!", _
        vbInformation, "Backup Successful"
 
Exit_BackupSource:
  Exit Function

to

Code:
MsgBox "Data backup at " & vbCrLf & MD_Date & vbCrLf & "successful!", _
        vbInformation, "Backup Successful"

Exit_Button_Backup:
    DoCmd.Quit

  Exit Sub
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 05:21
Joined
Jan 14, 2017
Messages
18,207
Paul, I thought I would give you this but I'm not sure what you can do with it. It was written by an unknown person years ago to back up the back end file

Unknown person. :rolleyes:
Oh come on Adam. Stop being so disingenuous.
Now we also know your surname ...and your LinkedIn profile has the same photo.
Any other bits of information you want to trumpet?
 

vba_php

Forum Troll
Local time
Yesterday, 23:21
Joined
Oct 6, 2019
Messages
2,884
@vba_php 's code works really well but I am having trouble altering the code so it closes the database down after it has backed up, can someone help with that please
really Paul? just add one of these two lines b4 the error handler:
Code:
docmd.quit 'or:
application.closecurrentdatabase
Unknown person. :rolleyes:
Oh come on Adam. Stop being so disingenuous.
Now we also know your surname ...and your LinkedIn profile has the same photo.
Any other bits of information you want to trumpet?
I swear I didn't write that code! I found it years ago on the web somewhere. I can share plenty, what do u wanna know? You already know my instrument, but I do play piano too.

I like people to know who they're dealing with if I answer their question. But I don't believe, in my former tenure here, I ever had a picture uploaded. Nor did anyone know the meaning of my name (other than you, dog).
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
really Paul? just add one of these two lines b4 the error handler:
Code:
docmd.quit 'or:
application.closecurrentdatabase

I am still learning vba_php :D

I added the below from the old code I was using

Code:
Exit_Button_Backup:
    DoCmd.Quit

I guess Exit_Button_Backup: isn't needed?

Edit
I just tested it, not needed :rolleyes:
 

vba_php

Forum Troll
Local time
Yesterday, 23:21
Joined
Oct 6, 2019
Messages
2,884
r u satisfied with what uve got now then?
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
r u satisfied with what uve got now then?

It's all working so thank you all for your help, there are other things I need to suss out & learn how to do on all my databases but I'll save that for another day
 

vba_php

Forum Troll
Local time
Yesterday, 23:21
Joined
Oct 6, 2019
Messages
2,884
well I spose we'll see you again soon then! have a good day bro.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:21
Joined
Sep 21, 2011
Messages
14,216
Other than the issue with changing code in a non split DB, if she is the only one using it, why split it.?

I have a DB I use that is not split, as I am the only person using it, and so admittedly update the code as I see fit, but perhaps importing the data into the new DB might be a choice?
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
Other than the issue with changing code in a non split DB, if she is the only one using it, why split it.?

I have a DB I use that is not split, as I am the only person using it, and so admittedly update the code as I see fit, but perhaps importing the data into the new DB might be a choice?

She is the only one using it so she can deal without it being split & it won't make any difference to her
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:21
Joined
Sep 21, 2011
Messages
14,216
She is the only one using it so she can deal without it being split & it won't make any difference to her
#

So let her just run it from the USB stick each time. Might be a little slow?, but easier to work with?
 

jdraw

Super Moderator
Staff member
Local time
Today, 00:21
Joined
Jan 23, 2006
Messages
15,380
PaulD,
Is the intent to backup to the same USB where the database is located?
If you lose the USB, or it fails, you've lost the whole 'kit and caboodle"!!!

An "offsite"(another USB or other disk) backup would save the day.
Good luck.
 

PaulD2019

Registered User.
Local time
Today, 05:21
Joined
Nov 23, 2019
Messages
75
PaulD,
Is the intent to backup to the same USB where the database is located?
If you lose the USB, or it fails, you've lost the whole 'kit and caboodle"!!!

An "offsite"(another USB or other disk) backup would save the day.
Good luck.

I've already spoke to her about that, she is going to make regular backups on her home laptop :)
 

Users who are viewing this thread

Top Bottom