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

后端 未结 3 1947
迷失自我
迷失自我 2020-12-07 04:51

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 M

3条回答
  •  猫巷女王i
    2020-12-07 05:12

    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

提交回复
热议问题