Can an Ms Access database create a backup of itself while it's open using vba?












2















Background: I have a process that links Quickbooks to Ms Access. If a button is pressed, some information will be queried from Quickbooks and then updates Ms Access. If the power goes out, or if the user forces Ms Access to close during the sync process, it can cause some of the information to be corrupted.



Goal: I want to have a button on a form that users can press and it'll save the current database to a predefined location with the date and time attached to the filename.



I keep reading how it's possible to backup other closed databases (using FileCopy), but you need a hacky-workaround solution to do it on an open database, which can lead to data corruption. I'm not entirely convinced since the user can use "Save As" at any time.



Is there a way to backup a currently open Ms Access database, or something that will fulfill my needs?










share|improve this question



























    2















    Background: I have a process that links Quickbooks to Ms Access. If a button is pressed, some information will be queried from Quickbooks and then updates Ms Access. If the power goes out, or if the user forces Ms Access to close during the sync process, it can cause some of the information to be corrupted.



    Goal: I want to have a button on a form that users can press and it'll save the current database to a predefined location with the date and time attached to the filename.



    I keep reading how it's possible to backup other closed databases (using FileCopy), but you need a hacky-workaround solution to do it on an open database, which can lead to data corruption. I'm not entirely convinced since the user can use "Save As" at any time.



    Is there a way to backup a currently open Ms Access database, or something that will fulfill my needs?










    share|improve this question

























      2












      2








      2








      Background: I have a process that links Quickbooks to Ms Access. If a button is pressed, some information will be queried from Quickbooks and then updates Ms Access. If the power goes out, or if the user forces Ms Access to close during the sync process, it can cause some of the information to be corrupted.



      Goal: I want to have a button on a form that users can press and it'll save the current database to a predefined location with the date and time attached to the filename.



      I keep reading how it's possible to backup other closed databases (using FileCopy), but you need a hacky-workaround solution to do it on an open database, which can lead to data corruption. I'm not entirely convinced since the user can use "Save As" at any time.



      Is there a way to backup a currently open Ms Access database, or something that will fulfill my needs?










      share|improve this question














      Background: I have a process that links Quickbooks to Ms Access. If a button is pressed, some information will be queried from Quickbooks and then updates Ms Access. If the power goes out, or if the user forces Ms Access to close during the sync process, it can cause some of the information to be corrupted.



      Goal: I want to have a button on a form that users can press and it'll save the current database to a predefined location with the date and time attached to the filename.



      I keep reading how it's possible to backup other closed databases (using FileCopy), but you need a hacky-workaround solution to do it on an open database, which can lead to data corruption. I'm not entirely convinced since the user can use "Save As" at any time.



      Is there a way to backup a currently open Ms Access database, or something that will fulfill my needs?







      vba ms-access






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 16 '17 at 15:56









      JaitniumJaitnium

      118111




      118111
























          3 Answers
          3






          active

          oldest

          votes


















          2














          The users "save as" does a different thing than just copying a file, it actually creates a new database, and exports everything to it. You can do the same if you wish (if there are no locked records), but it does require some coding.



          The "backup database" is unavailable from the save as menu if the file is opened by other users (and closes all open objects when used).



          You can, of course, create a new file, and then iterate through all tables, queries, forms, reports, macros and modules to copy them, and then iterate through all relationships to add them to the copy. Then you can copy all database properties to the new database. But that requires some work.



          See the following code to create a backup that ignores relationships and database properties



          Public Sub BackupDatabase(newLocation As String)
          'Make sure there isn't already a file with the name of the new database
          If Dir(newLocation) <> "" Then Kill newLocation
          'Create a new database using the default workspace
          'dbVersion30 = Jet 3, dbVersion40 = Jet4, dbVersion120 = 2007 accdb, dbVersion150 = 2013 accdb
          DBEngine.Workspaces(0).CreateDatabase newLocation, dbLangGeneral, Option:=dbVersion150

          'Iterate through common object collections, put the files in
          Dim iterator As Variant
          For Each iterator In CurrentDb.TableDefs
          If Not iterator.Name Like "MSys*" Then
          DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acTable, iterator.Name, iterator.Name
          End If
          Next iterator
          For Each iterator In CurrentDb.QueryDefs
          If Not iterator.Name Like "~sq_*" Then
          DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acQuery, iterator.Name, iterator.Name
          End If
          Next iterator
          For Each iterator In CurrentProject.AllForms
          DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acForm, iterator.Name, iterator.Name
          Next iterator
          For Each iterator In CurrentProject.AllReports
          DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acReport, iterator.Name, iterator.Name
          Next iterator
          For Each iterator In CurrentProject.AllMacros
          DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acMacro, iterator.Name, iterator.Name
          Next iterator
          For Each iterator In CurrentProject.AllModules
          DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acModule, iterator.Name, iterator.Name
          Next iterator
          End Sub


          Note that, depending on your security settings, you might get a lot of security popups.






          share|improve this answer


























          • I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

            – Jaitnium
            Aug 16 '17 at 16:30











          • See the current edit. Never had problems with it, but haven't done extensive testing

            – Erik von Asmuth
            Aug 16 '17 at 16:51











          • I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

            – Jaitnium
            Aug 16 '17 at 17:29











          • You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

            – Erik von Asmuth
            Aug 16 '17 at 17:31













          • I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

            – Jaitnium
            Aug 16 '17 at 17:41





















          0














          You can use the following line of code, This is assuming you have a split database:



          Public Sub CompactDB()
          dim strFrom as string
          dim strTo as string

          strFrom = "C:Your Database Location Including File Name and Extension"
          strTo = "C:Your new Database backup location File Name and Extension"

          DBEngine.CompactDatabase strFrom, strTo

          End Sub


          NOTE This will not compact your current backend (strFrom), This makes a copy of back end located at strFrom to the new location (strTo).



          Just have a button click or event from another from call this sub.



          But, the way I handle this is make a table that stores 2 fields. Field 1 is named "DestinationFrom", Field 2 is named "DestinationTo". Then I store records like below:



          DestinationFrom = C:Destination of current back end



          DestinationTo = C:Back Up destination



          Then use the following code:



          Public sub CompactDB()
          dim rst as dao.recordset
          dim strSQL as string
          dim strLocation as string
          Dim strDestination as string

          strsql = "SELECT * " & _
          "FROM DestinationTable;"
          set rst = currentdb.openrecordset(strsql)
          strlocation = rst![DestinationFrom]
          strdestination = rst![DestinationTo]
          rst.close
          set rst = nothing

          DBEngine.CompactDatabase rst![DestinationFrom] , rst![DestinationTo]

          if not rst is nothing then
          rst.close
          set rst = nothing
          end if
          End Sub


          This way, if my code ever fails cause a folder was deleted or moved, I can change the string location in the field on the table without needing to change anything that was hard coded and needing to release a new copy. Very useful when allowing multiple users in a split database






          share|improve this answer


























          • edited second code, forgot a " before SELECT

            – Chris
            Sep 15 '17 at 15:40











          • this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

            – DRC
            Mar 1 '18 at 16:48













          • BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

            – Chris
            Mar 2 '18 at 16:34



















          0














          You can try using FileSystemObject like this:



              'strFrom = Application.CurrentProject.FullName
          'strTo = "C:FolderNameNewFileName.accdb"

          Public Sub copyFile(strFrom As String, strTo As String)
          Dim fso As FileSystemObject
          Set fso = New FileSystemObject

          fso.copyFile strFrom, strTo

          Set fso = Nothing

          End Sub





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45718163%2fcan-an-ms-access-database-create-a-backup-of-itself-while-its-open-using-vba%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            The users "save as" does a different thing than just copying a file, it actually creates a new database, and exports everything to it. You can do the same if you wish (if there are no locked records), but it does require some coding.



            The "backup database" is unavailable from the save as menu if the file is opened by other users (and closes all open objects when used).



            You can, of course, create a new file, and then iterate through all tables, queries, forms, reports, macros and modules to copy them, and then iterate through all relationships to add them to the copy. Then you can copy all database properties to the new database. But that requires some work.



            See the following code to create a backup that ignores relationships and database properties



            Public Sub BackupDatabase(newLocation As String)
            'Make sure there isn't already a file with the name of the new database
            If Dir(newLocation) <> "" Then Kill newLocation
            'Create a new database using the default workspace
            'dbVersion30 = Jet 3, dbVersion40 = Jet4, dbVersion120 = 2007 accdb, dbVersion150 = 2013 accdb
            DBEngine.Workspaces(0).CreateDatabase newLocation, dbLangGeneral, Option:=dbVersion150

            'Iterate through common object collections, put the files in
            Dim iterator As Variant
            For Each iterator In CurrentDb.TableDefs
            If Not iterator.Name Like "MSys*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acTable, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentDb.QueryDefs
            If Not iterator.Name Like "~sq_*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acQuery, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentProject.AllForms
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acForm, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllReports
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acReport, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllMacros
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acMacro, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllModules
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acModule, iterator.Name, iterator.Name
            Next iterator
            End Sub


            Note that, depending on your security settings, you might get a lot of security popups.






            share|improve this answer


























            • I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

              – Jaitnium
              Aug 16 '17 at 16:30











            • See the current edit. Never had problems with it, but haven't done extensive testing

              – Erik von Asmuth
              Aug 16 '17 at 16:51











            • I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

              – Jaitnium
              Aug 16 '17 at 17:29











            • You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

              – Erik von Asmuth
              Aug 16 '17 at 17:31













            • I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

              – Jaitnium
              Aug 16 '17 at 17:41


















            2














            The users "save as" does a different thing than just copying a file, it actually creates a new database, and exports everything to it. You can do the same if you wish (if there are no locked records), but it does require some coding.



            The "backup database" is unavailable from the save as menu if the file is opened by other users (and closes all open objects when used).



            You can, of course, create a new file, and then iterate through all tables, queries, forms, reports, macros and modules to copy them, and then iterate through all relationships to add them to the copy. Then you can copy all database properties to the new database. But that requires some work.



            See the following code to create a backup that ignores relationships and database properties



            Public Sub BackupDatabase(newLocation As String)
            'Make sure there isn't already a file with the name of the new database
            If Dir(newLocation) <> "" Then Kill newLocation
            'Create a new database using the default workspace
            'dbVersion30 = Jet 3, dbVersion40 = Jet4, dbVersion120 = 2007 accdb, dbVersion150 = 2013 accdb
            DBEngine.Workspaces(0).CreateDatabase newLocation, dbLangGeneral, Option:=dbVersion150

            'Iterate through common object collections, put the files in
            Dim iterator As Variant
            For Each iterator In CurrentDb.TableDefs
            If Not iterator.Name Like "MSys*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acTable, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentDb.QueryDefs
            If Not iterator.Name Like "~sq_*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acQuery, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentProject.AllForms
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acForm, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllReports
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acReport, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllMacros
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acMacro, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllModules
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acModule, iterator.Name, iterator.Name
            Next iterator
            End Sub


            Note that, depending on your security settings, you might get a lot of security popups.






            share|improve this answer


























            • I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

              – Jaitnium
              Aug 16 '17 at 16:30











            • See the current edit. Never had problems with it, but haven't done extensive testing

              – Erik von Asmuth
              Aug 16 '17 at 16:51











            • I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

              – Jaitnium
              Aug 16 '17 at 17:29











            • You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

              – Erik von Asmuth
              Aug 16 '17 at 17:31













            • I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

              – Jaitnium
              Aug 16 '17 at 17:41
















            2












            2








            2







            The users "save as" does a different thing than just copying a file, it actually creates a new database, and exports everything to it. You can do the same if you wish (if there are no locked records), but it does require some coding.



            The "backup database" is unavailable from the save as menu if the file is opened by other users (and closes all open objects when used).



            You can, of course, create a new file, and then iterate through all tables, queries, forms, reports, macros and modules to copy them, and then iterate through all relationships to add them to the copy. Then you can copy all database properties to the new database. But that requires some work.



            See the following code to create a backup that ignores relationships and database properties



            Public Sub BackupDatabase(newLocation As String)
            'Make sure there isn't already a file with the name of the new database
            If Dir(newLocation) <> "" Then Kill newLocation
            'Create a new database using the default workspace
            'dbVersion30 = Jet 3, dbVersion40 = Jet4, dbVersion120 = 2007 accdb, dbVersion150 = 2013 accdb
            DBEngine.Workspaces(0).CreateDatabase newLocation, dbLangGeneral, Option:=dbVersion150

            'Iterate through common object collections, put the files in
            Dim iterator As Variant
            For Each iterator In CurrentDb.TableDefs
            If Not iterator.Name Like "MSys*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acTable, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentDb.QueryDefs
            If Not iterator.Name Like "~sq_*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acQuery, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentProject.AllForms
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acForm, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllReports
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acReport, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllMacros
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acMacro, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllModules
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acModule, iterator.Name, iterator.Name
            Next iterator
            End Sub


            Note that, depending on your security settings, you might get a lot of security popups.






            share|improve this answer















            The users "save as" does a different thing than just copying a file, it actually creates a new database, and exports everything to it. You can do the same if you wish (if there are no locked records), but it does require some coding.



            The "backup database" is unavailable from the save as menu if the file is opened by other users (and closes all open objects when used).



            You can, of course, create a new file, and then iterate through all tables, queries, forms, reports, macros and modules to copy them, and then iterate through all relationships to add them to the copy. Then you can copy all database properties to the new database. But that requires some work.



            See the following code to create a backup that ignores relationships and database properties



            Public Sub BackupDatabase(newLocation As String)
            'Make sure there isn't already a file with the name of the new database
            If Dir(newLocation) <> "" Then Kill newLocation
            'Create a new database using the default workspace
            'dbVersion30 = Jet 3, dbVersion40 = Jet4, dbVersion120 = 2007 accdb, dbVersion150 = 2013 accdb
            DBEngine.Workspaces(0).CreateDatabase newLocation, dbLangGeneral, Option:=dbVersion150

            'Iterate through common object collections, put the files in
            Dim iterator As Variant
            For Each iterator In CurrentDb.TableDefs
            If Not iterator.Name Like "MSys*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acTable, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentDb.QueryDefs
            If Not iterator.Name Like "~sq_*" Then
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acQuery, iterator.Name, iterator.Name
            End If
            Next iterator
            For Each iterator In CurrentProject.AllForms
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acForm, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllReports
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acReport, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllMacros
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acMacro, iterator.Name, iterator.Name
            Next iterator
            For Each iterator In CurrentProject.AllModules
            DoCmd.TransferDatabase acExport, "Microsoft Access", newLocation, acModule, iterator.Name, iterator.Name
            Next iterator
            End Sub


            Note that, depending on your security settings, you might get a lot of security popups.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 16 '17 at 18:26

























            answered Aug 16 '17 at 16:21









            Erik von AsmuthErik von Asmuth

            18.9k62038




            18.9k62038













            • I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

              – Jaitnium
              Aug 16 '17 at 16:30











            • See the current edit. Never had problems with it, but haven't done extensive testing

              – Erik von Asmuth
              Aug 16 '17 at 16:51











            • I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

              – Jaitnium
              Aug 16 '17 at 17:29











            • You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

              – Erik von Asmuth
              Aug 16 '17 at 17:31













            • I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

              – Jaitnium
              Aug 16 '17 at 17:41





















            • I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

              – Jaitnium
              Aug 16 '17 at 16:30











            • See the current edit. Never had problems with it, but haven't done extensive testing

              – Erik von Asmuth
              Aug 16 '17 at 16:51











            • I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

              – Jaitnium
              Aug 16 '17 at 17:29











            • You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

              – Erik von Asmuth
              Aug 16 '17 at 17:31













            • I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

              – Jaitnium
              Aug 16 '17 at 17:41



















            I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

            – Jaitnium
            Aug 16 '17 at 16:30





            I'm looking for a way to backup my tables and their information. Since your solutions does that, I'm very interested.

            – Jaitnium
            Aug 16 '17 at 16:30













            See the current edit. Never had problems with it, but haven't done extensive testing

            – Erik von Asmuth
            Aug 16 '17 at 16:51





            See the current edit. Never had problems with it, but haven't done extensive testing

            – Erik von Asmuth
            Aug 16 '17 at 16:51













            I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

            – Jaitnium
            Aug 16 '17 at 17:29





            I have several linked tables that I'd like to avoid copying because they require Quickbooks to be open. Should I just check iterator.Name for the tables' names to skip over those tables?

            – Jaitnium
            Aug 16 '17 at 17:29













            You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

            – Erik von Asmuth
            Aug 16 '17 at 17:31







            You don't need to check their names, a simple If iterator.Connect <> "" Then will exclude them. There also is an option to store the data from the linked table in an unlinked table in the backup.

            – Erik von Asmuth
            Aug 16 '17 at 17:31















            I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

            – Jaitnium
            Aug 16 '17 at 17:41







            I made this modification to the first conditional: If Not iterator.Name Like "MSys*" And iterator.Connect <> "" Then but I'm getting error 3709: "The search key was not found in any record."

            – Jaitnium
            Aug 16 '17 at 17:41















            0














            You can use the following line of code, This is assuming you have a split database:



            Public Sub CompactDB()
            dim strFrom as string
            dim strTo as string

            strFrom = "C:Your Database Location Including File Name and Extension"
            strTo = "C:Your new Database backup location File Name and Extension"

            DBEngine.CompactDatabase strFrom, strTo

            End Sub


            NOTE This will not compact your current backend (strFrom), This makes a copy of back end located at strFrom to the new location (strTo).



            Just have a button click or event from another from call this sub.



            But, the way I handle this is make a table that stores 2 fields. Field 1 is named "DestinationFrom", Field 2 is named "DestinationTo". Then I store records like below:



            DestinationFrom = C:Destination of current back end



            DestinationTo = C:Back Up destination



            Then use the following code:



            Public sub CompactDB()
            dim rst as dao.recordset
            dim strSQL as string
            dim strLocation as string
            Dim strDestination as string

            strsql = "SELECT * " & _
            "FROM DestinationTable;"
            set rst = currentdb.openrecordset(strsql)
            strlocation = rst![DestinationFrom]
            strdestination = rst![DestinationTo]
            rst.close
            set rst = nothing

            DBEngine.CompactDatabase rst![DestinationFrom] , rst![DestinationTo]

            if not rst is nothing then
            rst.close
            set rst = nothing
            end if
            End Sub


            This way, if my code ever fails cause a folder was deleted or moved, I can change the string location in the field on the table without needing to change anything that was hard coded and needing to release a new copy. Very useful when allowing multiple users in a split database






            share|improve this answer


























            • edited second code, forgot a " before SELECT

              – Chris
              Sep 15 '17 at 15:40











            • this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

              – DRC
              Mar 1 '18 at 16:48













            • BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

              – Chris
              Mar 2 '18 at 16:34
















            0














            You can use the following line of code, This is assuming you have a split database:



            Public Sub CompactDB()
            dim strFrom as string
            dim strTo as string

            strFrom = "C:Your Database Location Including File Name and Extension"
            strTo = "C:Your new Database backup location File Name and Extension"

            DBEngine.CompactDatabase strFrom, strTo

            End Sub


            NOTE This will not compact your current backend (strFrom), This makes a copy of back end located at strFrom to the new location (strTo).



            Just have a button click or event from another from call this sub.



            But, the way I handle this is make a table that stores 2 fields. Field 1 is named "DestinationFrom", Field 2 is named "DestinationTo". Then I store records like below:



            DestinationFrom = C:Destination of current back end



            DestinationTo = C:Back Up destination



            Then use the following code:



            Public sub CompactDB()
            dim rst as dao.recordset
            dim strSQL as string
            dim strLocation as string
            Dim strDestination as string

            strsql = "SELECT * " & _
            "FROM DestinationTable;"
            set rst = currentdb.openrecordset(strsql)
            strlocation = rst![DestinationFrom]
            strdestination = rst![DestinationTo]
            rst.close
            set rst = nothing

            DBEngine.CompactDatabase rst![DestinationFrom] , rst![DestinationTo]

            if not rst is nothing then
            rst.close
            set rst = nothing
            end if
            End Sub


            This way, if my code ever fails cause a folder was deleted or moved, I can change the string location in the field on the table without needing to change anything that was hard coded and needing to release a new copy. Very useful when allowing multiple users in a split database






            share|improve this answer


























            • edited second code, forgot a " before SELECT

              – Chris
              Sep 15 '17 at 15:40











            • this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

              – DRC
              Mar 1 '18 at 16:48













            • BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

              – Chris
              Mar 2 '18 at 16:34














            0












            0








            0







            You can use the following line of code, This is assuming you have a split database:



            Public Sub CompactDB()
            dim strFrom as string
            dim strTo as string

            strFrom = "C:Your Database Location Including File Name and Extension"
            strTo = "C:Your new Database backup location File Name and Extension"

            DBEngine.CompactDatabase strFrom, strTo

            End Sub


            NOTE This will not compact your current backend (strFrom), This makes a copy of back end located at strFrom to the new location (strTo).



            Just have a button click or event from another from call this sub.



            But, the way I handle this is make a table that stores 2 fields. Field 1 is named "DestinationFrom", Field 2 is named "DestinationTo". Then I store records like below:



            DestinationFrom = C:Destination of current back end



            DestinationTo = C:Back Up destination



            Then use the following code:



            Public sub CompactDB()
            dim rst as dao.recordset
            dim strSQL as string
            dim strLocation as string
            Dim strDestination as string

            strsql = "SELECT * " & _
            "FROM DestinationTable;"
            set rst = currentdb.openrecordset(strsql)
            strlocation = rst![DestinationFrom]
            strdestination = rst![DestinationTo]
            rst.close
            set rst = nothing

            DBEngine.CompactDatabase rst![DestinationFrom] , rst![DestinationTo]

            if not rst is nothing then
            rst.close
            set rst = nothing
            end if
            End Sub


            This way, if my code ever fails cause a folder was deleted or moved, I can change the string location in the field on the table without needing to change anything that was hard coded and needing to release a new copy. Very useful when allowing multiple users in a split database






            share|improve this answer















            You can use the following line of code, This is assuming you have a split database:



            Public Sub CompactDB()
            dim strFrom as string
            dim strTo as string

            strFrom = "C:Your Database Location Including File Name and Extension"
            strTo = "C:Your new Database backup location File Name and Extension"

            DBEngine.CompactDatabase strFrom, strTo

            End Sub


            NOTE This will not compact your current backend (strFrom), This makes a copy of back end located at strFrom to the new location (strTo).



            Just have a button click or event from another from call this sub.



            But, the way I handle this is make a table that stores 2 fields. Field 1 is named "DestinationFrom", Field 2 is named "DestinationTo". Then I store records like below:



            DestinationFrom = C:Destination of current back end



            DestinationTo = C:Back Up destination



            Then use the following code:



            Public sub CompactDB()
            dim rst as dao.recordset
            dim strSQL as string
            dim strLocation as string
            Dim strDestination as string

            strsql = "SELECT * " & _
            "FROM DestinationTable;"
            set rst = currentdb.openrecordset(strsql)
            strlocation = rst![DestinationFrom]
            strdestination = rst![DestinationTo]
            rst.close
            set rst = nothing

            DBEngine.CompactDatabase rst![DestinationFrom] , rst![DestinationTo]

            if not rst is nothing then
            rst.close
            set rst = nothing
            end if
            End Sub


            This way, if my code ever fails cause a folder was deleted or moved, I can change the string location in the field on the table without needing to change anything that was hard coded and needing to release a new copy. Very useful when allowing multiple users in a split database







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 2 '18 at 16:35

























            answered Sep 14 '17 at 21:18









            ChrisChris

            22615




            22615













            • edited second code, forgot a " before SELECT

              – Chris
              Sep 15 '17 at 15:40











            • this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

              – DRC
              Mar 1 '18 at 16:48













            • BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

              – Chris
              Mar 2 '18 at 16:34



















            • edited second code, forgot a " before SELECT

              – Chris
              Sep 15 '17 at 15:40











            • this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

              – DRC
              Mar 1 '18 at 16:48













            • BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

              – Chris
              Mar 2 '18 at 16:34

















            edited second code, forgot a " before SELECT

            – Chris
            Sep 15 '17 at 15:40





            edited second code, forgot a " before SELECT

            – Chris
            Sep 15 '17 at 15:40













            this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

            – DRC
            Mar 1 '18 at 16:48







            this looked promising. I tried to use it to backup the current backend to a new location using this line of code: DBEngine.CompactDatabase BackendPath("tbl"), BackendPath("tbl") & "_backup" However, when that code runs, I get a Run-time error '3704': You attempted to open a database that is already opened by user 'Admin' on machine {Computer Name}. Try again when the databse is available. I really wish this worked for me, though.

            – DRC
            Mar 1 '18 at 16:48















            BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

            – Chris
            Mar 2 '18 at 16:34





            BAH! my mistake, I have edited my second code to fix the issue. The problem is when the rst opened a recordset to the strSQL query. it created a connection to the database. Since there is a current connection, the database was unable to compact and repair to the destination location.

            – Chris
            Mar 2 '18 at 16:34











            0














            You can try using FileSystemObject like this:



                'strFrom = Application.CurrentProject.FullName
            'strTo = "C:FolderNameNewFileName.accdb"

            Public Sub copyFile(strFrom As String, strTo As String)
            Dim fso As FileSystemObject
            Set fso = New FileSystemObject

            fso.copyFile strFrom, strTo

            Set fso = Nothing

            End Sub





            share|improve this answer




























              0














              You can try using FileSystemObject like this:



                  'strFrom = Application.CurrentProject.FullName
              'strTo = "C:FolderNameNewFileName.accdb"

              Public Sub copyFile(strFrom As String, strTo As String)
              Dim fso As FileSystemObject
              Set fso = New FileSystemObject

              fso.copyFile strFrom, strTo

              Set fso = Nothing

              End Sub





              share|improve this answer


























                0












                0








                0







                You can try using FileSystemObject like this:



                    'strFrom = Application.CurrentProject.FullName
                'strTo = "C:FolderNameNewFileName.accdb"

                Public Sub copyFile(strFrom As String, strTo As String)
                Dim fso As FileSystemObject
                Set fso = New FileSystemObject

                fso.copyFile strFrom, strTo

                Set fso = Nothing

                End Sub





                share|improve this answer













                You can try using FileSystemObject like this:



                    'strFrom = Application.CurrentProject.FullName
                'strTo = "C:FolderNameNewFileName.accdb"

                Public Sub copyFile(strFrom As String, strTo As String)
                Dim fso As FileSystemObject
                Set fso = New FileSystemObject

                fso.copyFile strFrom, strTo

                Set fso = Nothing

                End Sub






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 27 '18 at 16:04









                landrewlandrew

                363




                363






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45718163%2fcan-an-ms-access-database-create-a-backup-of-itself-while-its-open-using-vba%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    鏡平學校

                    ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                    Why https connections are so slow when debugging (stepping over) in Java?