Results 1 to 5 of 5
  1. #1
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328

    Copy an open dbfile

    I posted this earlier, but at least two things happened. It got tied up with someone else's post-both displayed if you clicked on it. this may have caused some confusion. Also, the one person who did reply early on just gave me a manual procedure asked why I wanted to do it with code. I have a good reason to do this with code.. Any help much appreciated

    I found a site that showed a way to make a copy of a file while the file was open. One of several, but this one works. How do I get the expression to save the copied file to the same folder as the original file? I have tried Application.currentproject.path in several combinations, but am missing something.

    This is code that I found and am using, but need to change to save the copy in same folder as current file.

    Dim external_db As Object
    Set external_db = CreateObject("Scripting.FileSystemObject")
    external_db.CopyFile "C:\Users\GG\Documents\TypesExamplesHints\COPY Live DBfile.accdb", "C:\Users\GG\Documents\TypesExamplesHints\COPY _of _COPYLiveDBfile.accdb", True
    Set external_db = Nothing



    Using A2007 and A2003. Any help much appreciated. From various websites, I know that most people consider copying a live file a bad idea. But here, I have a good reason.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    Do you want this to update user's copy of frontend of split database? I do it with:
    Code:
    If Me.tbxVersion <> Me.lblVersion.Caption Then
       If DLookup("Permissions", "Users", "UserNetworkID='" & Environ("UserName") & "'") <> "admin" Then
            'copy Access file
            CreateObject("Scripting.FileSystemObject").CopyFile _
                gstrBasePath & "Program\Install\MaterialsDatabase.accdb", "c:\", True
            'allow enough time for file to completely copy before opening
            Dim Start As Double
            Start = Timer
            While Timer < Start + 3
                DoEvents
            Wend
            'load new version - SysCmd function gets the Access executable file path
            'Shell function requires literal quote marks in the target filename string argument, hence the quadrupled quote marks
            Shell SysCmd(acSysCmdAccessDir) & "MSAccess.exe " & """" & CurrentProject.FullName & """", vbNormalFocus
            'close current file
            DoCmd.Quit
        End If
    End If
    This code is in Load event of form that opens on default when the db opens.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    Thanks for quick reply. I am a self-taught guy who learns as needs arise. So, right now, this iooks very intimidating. I'm not sure that I will even get past the first "if" statements. I will study and play with. I was hoping that someone could just tell me how to modify my code to store a copy in Current folder.

    In my case, the plan is to start code from a form button.

    Quote Originally Posted by June7 View Post
    Do you want this to update user's copy of frontend of split database? I do it with:
    Code:
    If Me.tbxVersion <> Me.lblVersion.Caption Then
       If DLookup("Permissions", "Users", "UserNetworkID='" & Environ("UserName") & "'") <> "admin" Then
            'copy Access file
            CreateObject("Scripting.FileSystemObject").CopyFile _
                gstrBasePath & "Program\Install\MaterialsDatabase.accdb", "c:\", True
            'allow enough time for file to completely copy before opening
            Dim Start As Double
            Start = Timer
            While Timer < Start + 3
                DoEvents
            Wend
            'load new version - SysCmd function gets the Access executable file path
            'Shell function requires literal quote marks in the target filename string argument, hence the quadrupled quote marks
            Shell SysCmd(acSysCmdAccessDir) & "MSAccess.exe " & """" & CurrentProject.FullName & """", vbNormalFocus
            'close current file
            DoCmd.Quit
        End If
    End If
    This code is in Load event of form that opens on default when the db opens.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    I tested your code and it works. You want the path to be dynamic, not hard coded?

    Try:

    external_db.CopyFile CurrentProject.FullName, CurrentProject.Path & "\Copy_" & CurrentProject.Name, True
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    It works!-Thanks so much for sticking with it. This was really frustrating. Like all things, once someone else shows you how, it seems simple. . As soon as I figure out how to do it, I will mark solved.

    Thanks again

    gg80




    Quote Originally Posted by June7 View Post
    I tested your code and it works. You want the path to be dynamic, not hard coded?

    Try:

    external_db.CopyFile CurrentProject.FullName, CurrentProject.Path & "\Copy_" & CurrentProject.Name, True

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Copy Open db file
    By gg80 in forum Programming
    Replies: 4
    Last Post: 04-24-2013, 04:35 PM
  2. Replies: 10
    Last Post: 06-13-2012, 05:57 AM
  3. Replies: 1
    Last Post: 05-03-2012, 02:25 PM
  4. Replies: 2
    Last Post: 02-26-2010, 08:14 AM
  5. Replies: 1
    Last Post: 06-04-2009, 07:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums