VBA Access Database Update For Developers – 64 bit

For a client I am currently doing work for, we have a mixture of Access versions. Both versions are Access 2016, but one version is 32 bit, and the other is 64 bit (Office 365 version).

Whenever a new version of the database is released, the users can click a button to upgrade the database they are using to the most recent version.

The .accdb file is pulled down from the server and is loaded on the user’s machine, without user or admin intervention.

That’s great, empower the users.

Here is the code that works great for the 32 bit users:

'*******************************************************
'IN A BASIC MODULE:
'*******************************************************
'this is an windows api call (put PtrSafe with 64 bit versions, this works with 32 bit)
Public Declare PtrSafe Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long


Public Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Long
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As Long
End Type



Public Function VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
    Dim op As SHFILEOPSTRUCT
    Dim i As Long
    
    With op
        .wFunc = FO_COPY '(2 is the numerical value for FO_COPY)
        .pTo = strTarget
        .pFrom = strSource
        .fFlags = False
    End With

    '~~> Perform operation
    'success = 0
    'not successful = 87
    i = SHFileOperation(op)
End Function


Function FileOrDirExists(PathName As String) As Boolean
     'Purpose: Function returns TRUE if the specified file
     '               or folder exists, false if not.
     
    Dim intTemp As Integer
     
    'Ignore errors to allow for error evaluation
    On Error Resume Next
    
    intTemp = GetAttr(PathName)
     
    'Check if error exists and set response appropriately when it does
    Select Case Err.Number
      Case Is = 0
          FileOrDirExists = True
      Case Else
          FileOrDirExists = False
    End Select
     
     'Resume error checking
    On Error GoTo 0
End Function

'*******************************************************
'after clicking on form button:
'*******************************************************

Private Sub btnDBUpdate_Click()
    Dim strSourcePath As String
    Dim strDestPath As String

    'network drive
    strSourcePath = "R:\DatabaseFiles"

    'local drive
    strDestPath = "C:\DatabaseFolder"

    'delete old existing database
    If FileOrDirExists(strDestPath & "\Interface.accdb") Then
        'if the old existing database file exists, delete it
        Kill strDestPath & "\Interface.accdb"
    End If          

    'copy newly updated database
    Call VBCopyFolder(strSourcePath & "Interface.accdb", strDestPath & "\Interface.accdb")


    'copy all the contents of a folder
    Call VBCopyFolder(strSourcePath & "ImageFiles", strDestPath & "\ImageFiles")


    
End Sub

This code will show you the professional copying files image like this:

…of course it will be customized with your information.

but for 64 bit Access users …

…you will need to add some extra text to the “Long” datatypes.

THAT’S ALL YOU HAVE TO DO! (not real complex…just trying to find out the solution is a pain in the hind quarters )

Here’s the code (all the same, but change Long to LongPtr)

'*******************************************************
'IN A BASIC MODULE:
'*******************************************************
'this is an windows api call (put PtrSafe with 64 bit versions, this works with 32 bit)
Public Declare PtrSafe Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long


Public Type SHFILEOPSTRUCT
    hWnd As LongPtr
    wFunc As LongPtr
    pFrom As String
    pTo As String
    fFlags As LongPtr
    fAnyOperationsAborted As LongPtr
    hNameMappings As LongPtr
    lpszProgressTitle As LongPtr
End Type



Public Function VBCopyFolder(ByRef strSource As String, ByRef strTarget As String)
    Dim op As SHFILEOPSTRUCT
    Dim i As Long
    
    With op
        .wFunc = FO_COPY '(2 is the numerical value for FO_COPY)
        .pTo = strTarget
        .pFrom = strSource
        .fFlags = False
    End With

    '~~> Perform operation
    'success = 0
    'not successful = 87
    i = SHFileOperation(op)
End Function


Function FileOrDirExists(PathName As String) As Boolean
     'Purpose: Function returns TRUE if the specified file
     '               or folder exists, false if not.
     
    Dim intTemp As Integer
     
    'Ignore errors to allow for error evaluation
    On Error Resume Next
    
    intTemp = GetAttr(PathName)
     
    'Check if error exists and set response appropriately when it does
    Select Case Err.Number
      Case Is = 0
          FileOrDirExists = True
      Case Else
          FileOrDirExists = False
    End Select
     
     'Resume error checking
    On Error GoTo 0
End Function

'*******************************************************
'after clicking on form button:
'*******************************************************

Private Sub btnDBUpdate_Click()
    Dim strSourcePath As String
    Dim strDestPath As String

    'network drive
    strSourcePath = "R:\DatabaseFiles"

    'local drive
    strDestPath = "C:\DatabaseFolder"

    'delete old existing database
    If FileOrDirExists(strDestPath & "\Interface.accdb") Then
        'if the old existing database file exists, delete it
        Kill strDestPath & "\Interface.accdb"
    End If          

    'copy newly updated database
    Call VBCopyFolder(strSourcePath & "Interface.accdb", strDestPath & "\Interface.accdb")


    'copy all the contents of a folder
    Call VBCopyFolder(strSourcePath & "ImageFiles", strDestPath & "\ImageFiles")


    
End Sub

Hope it works for you, and let me know if you have any questions.




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

How to Delete Blank Rows in Excel Using VBA (Step-by-Step Guide)

How to Delete Blank Rows in Excel Using VBA (Step-by-Step Guide) When you work with large datasets in Excel, it’s common to end up with thousands of blank rows scattered throughout your worksheet. Maybe your data came from a mainframe export, a CSV download, or a legacy system that doesn’t format properly. Deleting all those […]

How to Copy Data from One Worksheet to Another Using VBA in Excel

Learn how to copy data between worksheets in Excel using VBA step-by-step! This quick tutorial shows how to automate data movement, save time, and boost productivity. Watch: How to Copy Data from One Worksheet to Another Using VBA Why Automate Copying Data Between Worksheets? Copying and pasting data manually can take up valuable time and […]

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How to Fix Run Time Error 1004 in Excel

If you work with Microsoft Excel frequently, chances ar ling for a solution. Fortunately, this error is well-documented, and there are several ways to resolve it. In this article, we’ll explore the causes of run time error 1004, practical steps to fix it, and preventive measures to reduce the chances of it happening again. What […]


Support these sponsors:
Previous Post

How To Do A VBA CSV Import For One To Multiple Files

Next Post

MS Access VBA Form Filter Example