简体   繁体   中英

Import Excel data to Access through Excel VBA

Sub AccImport()
    Dim dbConnection As ADODB.Connection
    Dim dbFileName As String
    Dim dbRecordset As ADODB.Recordset
    Dim xRow As Long, xColumn As Long
    Dim LastRow As Long

    'Go to the worksheet containing the records you want to transfer.
    Worksheets("Completed").Activate

    'Determine the last row of data based on column A.
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row

    'Create the connection to the database.
    Set dbConnection = New ADODB.Connection

    'Define the database file name
    dbFileName = "C:/..."

    'Define the Provider and open the connection.
    With dbConnection
        .Provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFileName & _
                    ";Persist Security Info=False;"
        .Open dbFileName
    End With

    'Create the recordset
    Set dbRecordset = New ADODB.Recordset

    dbRecordset.CursorLocation = adUseServer
    dbRecordset.Open Source:="Resolution", _
                              ActiveConnection:=dbConnection, _
                              CursorType:=adOpenDynamic, _
                              LockType:=adLockOptimistic, _
                              Options:=adCmdTable

    'Loop thru rows & columns to load records from Excel to Access.
    'Assume row 1 is the header row, so start at row 2.
    For xRow = 2 To LastRow
        dbRecordset.AddNew
        'Assume this is an 26-column (field) table starting with column A.
        For xColumn = 1 To 26
            dbRecordset(Cells(1, xColumn).Value) = Cells(xRow, xColumn).Value
        Next xColumn
        dbRecordset.Update
    Next xRow

    'Close the connections.
    dbRecordset.Close
    dbConnection.Close

    'Release Object variable memory.
    Set dbRecordset = Nothing
    Set dbConnection = Nothing
    'Optional:
    'Clear the range of data (the records) you just transferred.
    Range("A2:Z" & LastRow).ClearContents
 End Sub

When I tried exporting the data its giving me error in as:

Error 3265. Item cannot be found in the collection.

I'm unable to export the data it's giving me error in the line

dbRecordset(Cells(1, xColumn).Value) = Cells(xRow, xColumn).Value

... Any Ideas

Instead of doing it like that. Use docmd.transferSpreadsheet

One line will import the data into Access. You will need to add the reference Microsoft Access 14.0 Object Library to be able to use docmd . Or just run it from Access

So it will be something like docmd.TransferSpreadsheet acExport,acSpreadsheetTypeExcel12, "AccessTableName", "FullFileName",true,"Range"

Replacing, AccessTablename with the name of the table you want to import to. FullFileName with the full file name of the excel sheet. Range with the range excel sheet. You can remove range if you want to just copy all from Excel.

EDIT. Updated to open Access

Dim appAccess As Access.Application
    Set appAccess = CreateObject("Access.Application")

    appAccess.Visible = True

    appAccess.OpenCurrentDatabase "FILENAME OF ACCESS DB"

    appAccess.docmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "AccessTableName", "FullFileName", False

This will open the Access DB. Run the code and import the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM