Results 1 to 12 of 12

Thread: [RESOLVED] VBA - Copy database from one destination to another

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Resolved [RESOLVED] VBA - Copy database from one destination to another

    Hello everyone,
    I am working with MS Access databases (.mdb) Can anyone please give me a simple directive on copying a database from one destination to another (from Source to destination)
    Actually I am intend to copy the database from a Server db to my local C:\ drive.

    Below is my set up so far:
    Code:
    Private Sub cmddbCopy_Click()
    Dim dbsource As Database
    Dim dbdestination As Database
    
    Set dbdestination = CurrentDb
    
    Set dbsource = OpenDatabase("M:\\Livctrls03-08\data1\App\UCCProd\Databases\Unclaimed Checks.mdb", True, True)
    Set dbdestination = OpenDatabase("C:Unclaimed Checks.mdb", True, False)
    Any hint will be greatly appreciated.
    Giftx.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: VBA - Copy database from one destination to another

    If this is a simple task to copy the entire database over, then because it's an MDB, use the FileCopy method to copy the MDB over.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: VBA - Copy database from one destination to another

    Thanks for your quick response.
    Question: should I create the destination database already then copy over the data or will it just copy the entire database and name it?

    Also, can you give me a code example
    Thanks.
    GiftX.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: VBA - Copy database from one destination to another

    MS Access databases are simply .MDB files. Treat it like any other file. This means that you needn't create the database first, just overwrite it if the file already exists.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA - Copy database from one destination to another

    To be safe you will want to make sure no users are in hte db too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: VBA - Copy database from one destination to another

    Please, can you give me an example of what you mean? Also, you can see an excerpt of the code I started in my thread.
    Giftx.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA - Copy database from one destination to another

    Are you doing this from within Access' VBA IDE?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: VBA - Copy database from one destination to another

    Hi Rob.
    Yes, I am doing this from MS Access environment.

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VBA - Copy database from one destination to another

    Hi Giftx

    If you are copying the mdb as any other file then check these three ways... take your pick

    1) API WAY

    you can use this API. this works in vb6. I am sure this will work in vba as well.

    Code:
    Option Explicit
    
    Private Type SHFILEOPSTRUCT
            hwnd As Long
            wFunc As Long
            pFrom As String
            pTo As String
            fFlags As Integer
            fAnyOperationsAborted As Long
            hNameMappings As Long
            lpszProgressTitle As String
    End Type
    
    Private Declare Function SHFileOperation Lib "shell32.dll" _
    Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    
    Private Const FOF_ALLOWUNDO = &H40
    Private Const FOF_NOCONFIRMATION = &H10
    Private Const FO_COPY = &H2
    
    Public Function ShellFileCopy(src As String, dest As String, _
        Optional NoConfirm As Boolean = False) As Boolean
    
    'PURPOSE: COPY FILES VIA SHELL API
    'THIS DISPLAYS THE COPY PROGRESS DIALOG BOX
    'PARAMETERS: src: Source File (FullPath)
                'dest: Destination File (FullPath)
                'NoConfirm (Optional): If set to
                'true, no confirmation box
                'is displayed when overwriting
                'existing files, and no
                'copy progress dialog box is
                'displayed
                
                'Returns (True if Successful, false otherwise)
                
    'EXAMPLE:  
      'dim bSuccess as boolean
      'bSuccess = ShellFileCopy ("C:\MyFile.txt", "D:\MyFile.txt")
    
    Dim WinType_SFO As SHFILEOPSTRUCT
    Dim lRet As Long
    Dim lflags As Long
    
    lflags = FOF_ALLOWUNDO
    If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION
    With WinType_SFO
        .wFunc = FO_COPY
        .pFrom = src
        .pTo = dest
        .fFlags = lflags
    End With
    
    lRet = SHFileOperation(WinType_SFO)
    ShellFileCopy = (lRet = 0)
    
    End Function
    2) FSO WAY

    Code:
    Dim fs As Object
    Dim oldPath As String, newPath As String
    oldPath = "C:\Documents and Settings\user\My Documents" 'Folder file is located in
    newPath = "C:\Documents and Settings\user\My Documents\Misc" 'Folder to copy file to
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFile oldPath & "\" & "myfile.mdb", newPath & "\" & "myfile.mdb" 
    Set fs = Nothing
    3) FILE COPY

    Code:
    Dim sDest As String
    Dim sSource As String
    
    sSource = "\\MYSERVER\KOOLSID\MYFILE.MDB"
    sDest = "C:\BACKUP\MYFILE.MDB"
    
    FileCopy sSource, sDest
    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA - Copy database from one destination to another

    Ah I see you like the API way too I have code similar to that on the Forums but for folder copying.

    Copying from within Access while its open will work but you will need to either read the ldb file to see if anyone else is in it or try other techniques. You will be limited since you are in Access already.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: VBA - Copy database from one destination to another

    Thanks all you Gurus. All the examples will do just fine.
    Thanks and enjoy your weekend.
    Giftx.

  12. #12
    New Member
    Join Date
    May 2011
    Posts
    1

    Re: [RESOLVED] VBA - Copy database from one destination to another

    Hey All,

    I have a question related to this.
    I am using API way to copy data, but my user is on a VPN and VPN gets disconnected in between and excel throws out an error. Is there a way to copy data bit by bit from one location to another?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width