Results 1 to 3 of 3
  1. #1
    cazeek is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    11

    LARGE file when Creating/Updating Access DB through Excel VBA

    Hello,



    I am by no means an expert / teach myself as I go, so please keep that in mind. I have created code to either create a new or update an existing access DB using ADODB through VBA for Excel. Everything works perfectly, but the resulting database files are very large (26MB). They can be brought back down to normal size (1MB) using the Compact/Repair feature within Access, but that's very inefficient/time-consuming. Can anyone take a look / offer some suggestions? Relevant code is posted below. I'm also open to any other suggestions on the code in general if something looks inefficient.

    Thanks!!

    Code:
    Sub Update_DB_OnTime()
    
    
    If Range("error_check") = True Then
        DoEvents
        Application.OnTime Now() + TimeValue("00:00:01"), "Update_DB_OnTime"
        Exit Sub
    Else
        Dim dbConn As New ADODB.Connection
        Dim rs As New ADODB.Recordset
        Dim dbLoc As String
        Dim connect As String, strSQL As String
        Dim dbName As String
        
        dbName = Range("contract_code") & ".accdb"
        
        dbLoc = "C:\...Folder Location...\" & dbName
        
        Set dbConn = New ADODB.Connection
        connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
        connect = connect & "Data Source="
        connect = connect & dbLoc
        
        dbConn.Open connect
    
    
        strSQL = "SELECT * FROM Table1"
        
        Set rs = New ADODB.Recordset
        rs.Open strSQL, dbConn, adOpenKeyset, adLockOptimistic
        
        Update_DB rs
        
        rs.Close
        Set rs = Nothing
        dbConn.Close
        Set dbConn = Nothing
    End If
    
    
    Sub Update_DB(rs As ADODB.Recordset)
    
    
    Dim start_record As Integer
    
    
    If Cells(Range("record_count") + 2, 18) = Cells(Range("record_count_old") + 2, 3) Then
        start_record = Range("record_count_old")
        rs.Move (start_record)
    Else
        start_record = 0
        If rs.BOF <> rs.EOF Then
            rs.MoveFirst
        End If
    End If
    
    
    
    
    With rs
        For i = start_record To Range("record_count") - 1
            If Cells(i + 3, 16) <> "" Then
                If .EOF Then
                    .AddNew
                    .MoveLast
                End If
                For j = 1 To rs.Fields.Count - 1
                    If IsError(Cells(i + 3, j + 16)) Then
                        With Worksheets("Error Log")
                            .Calculate
                            .Range("C3").Offset(Range("error_count"), 0) = Range("contract_code")
                            .Range("D3").Offset(Range("error_count"), 0) = Cells(i + 3, 1 + 16)
                            .Range("E3").Offset(Range("error_count"), 0) = Cells(2, j + 16)
                        End With
                    Else
                        .Fields(j).Value = Cells(i + 3, j + 16)
                        .Update
                    End If
                Next
                .MoveNext
            End If
        Next
    End With
    
    
    End Sub

  2. #2
    Peter M is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2017
    Posts
    67
    I'm guessing the Excel file is also fairly small? I suspect, but do not know, that when "Select * from Table1" is called Access allocates a large amount of space to handle the unknown incoming file.
    Instead of selecting the data from the excel file, can you link to it? (DoCmd.TransferSpreadsheet)

    Excel files can be imported or linked use the following:
    DoCmd.TransferSpreadsheet [TransferType], [SpreadsheetType], [TableName], [FileName], [HasFieldNames], [Range]

    • Transfer Type: acImport, acExport, or acLink
    • Spreadsheet Type: the version of Excel. Using acSpreadsheetTypeExcel12 seems to work for xlsx files.
    • Table Name: the MS Access table that the data is going into
    • Filename: where the data is coming from
    • HasFieldNames: TRUE if the first row contains field names for each column
    • Range: usually the sheetname e.g. “Sheet1!” with the exclamation (bang) at the end, can also use Named Ranges

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    the method used (record by record, field by field) is pretty much the slowest way of doing it.

    Peters suggestion of using transferspreadsheet is a good one, but based on running in Access and importing from Excel. You can create an Access object in your excel VBA and perhaps can run it that way - or look at the transferdatabase method. Another alternative perhaps is to create a data connection and run a dao append query.

    In summary, will need to know more before a better way can be suggested - some example before and after data and why you are doing this - appending new records to a log table? why not keep in excel?

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

Similar Threads

  1. Taking a large Excel workbook and creating a database
    By desertratz in forum Database Design
    Replies: 3
    Last Post: 01-16-2016, 03:11 PM
  2. Updating Values in Excel File from Access After Query
    By Breezer23 in forum Programming
    Replies: 5
    Last Post: 03-24-2014, 08:52 AM
  3. Importing large excel file into multiple access files
    By Ghost in forum Import/Export Data
    Replies: 10
    Last Post: 11-05-2013, 11:19 AM
  4. Need help exporting large Access file into Excel
    By phidelt in forum Import/Export Data
    Replies: 2
    Last Post: 02-27-2013, 06:14 PM
  5. Excel file that I export from Access is extremely large
    By Ronald Mcdonald in forum Access
    Replies: 1
    Last Post: 05-25-2012, 03:32 PM

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