Growing MS Access File Size problem

后端 未结 9 474
庸人自扰
庸人自扰 2021-01-12 08:41

I have a large MS Access application with a lot of computations in VBA code. When I run it it eventually crashes due to excessive file size. There are a lot of intermediat

9条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 09:30

    I have encountered a similar issue where my database was bloating on raw data import. Instead of splitting the database and compacting the backend routinely, I decided to use the database object (DAO) to create a temp database, import the data, query/modify data in that temp database, pull it over to your original database via SQL and then delete it. YBase code shown below:

    Sub tempAccessDatabaseImport()
        Dim mySQL As String
        Dim tempDBPath As String
        Dim myWrk As DAO.Workspace
        Dim tempDB As DAO.Database
        Dim myObject
    
        'Define temp access database path
        tempPathArr = Split(Application.CurrentProject.Path, "\")
        For i = LBound(tempPathArr) To UBound(tempPathArr)
            tempDBPath = tempDBPath + tempPathArr(i) + "\"
        Next i
        tempDBPath = tempDBPath + "tempDB.accdb"
    
        'Delete temp access database if exists
        Set myObject = CreateObject("Scripting.FileSystemObject")
        If myObject.FileExists(tempDBPath) Then
            myObject.deleteFile (tempDBPath)
        End If
    
        'Open default workspace
        Set myWrk = DBEngine.Workspaces(0)
    
        'DAO Create database
        Set tempDB = myWrk.CreateDatabase(tempDBPath, dbLangGeneral)
    
        'DAO - Import temp xlsx into temp Access table
        mySQL = "SELECT * INTO tempTable FROM (SELECT vXLSX.*FROM [Excel 12.0;HDR=YES;DATABASE=" & RAWDATAPATH & "].[" & WORKSHEETNAME & "$] As vXLSX)"
    
        'DAO Execute SQL
        Debug.Print mySQL
        Debug.Print
        tempDB.Execute mySQL, dbSeeChanges
    
        'Do Something Else
    
        'Close DAO Database object
        tempDB.Close
        Set tempDB = Nothing
    
        myWrk.Close
        Set myWrk = Nothing
    
        'Delete temp access database if exists
        If myObject.FileExists(tempDBPath) Then
            'myObject.deleteFile (tempDBPath)
        End If
    End Sub
    

提交回复
热议问题