Excel data into an Access Table

TCH

New Member
Joined
May 23, 2012
Messages
9
*** THIS IS AREPOST AS I CAN’T FIND THE ORIGINAL POST ***<o:p></o:p>
<o:p> </o:p>
Hi. I’m anewbie to Access but seasoned in Excel and VBA. <o:p></o:p>
<o:p> </o:p>
Starting in row2 of an Excel worksheet I have 1 to 50 rows containing 7 columns of data thatif not found in an Access table of over 50,000 records must be transferred tothe Access table.<o:p></o:p>
<o:p> </o:p>
The columnarlayout of the Excel file and the Access table are identical.

Within Access fields 2, 3 and 7 are designated as “Primary” fields ifthat’s an issue.
<o:p></o:p>

<o:p> </o:p>
I know how tofind a record based on one field within Access. But once a recordmatches criteria 1, how to compare the additional two fields of that record andcontinuing the testing to the EOF if not found has me stumped.<o:p></o:p>
<o:p> </o:p>
In summary, thequestion is how to loop through an Access table to find if a record existsmatching three fields of a seven field record (in Excel) then adding the newrecord into Access if the Excel row’sdata wasn’t found in the Access table. If a three-way match recordwas found, I'd like to overwrite the existing record entirely andthen advance to the next Excel row then repeat the matching for that row's"record" from the beginning of the Access table.

I'm using Access 2007 and Excel 2007.
<o:p></o:p>

<o:p> </o:p>
Any insightwould be most appreciated. Thanks inadvance for your help. <o:p></o:p>
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this code as a starting point.
Code:
Sub PushToAccess_2()
    Dim cnn As ADODB.Connection
    Dim MyConn
    Dim strPath As String
    Dim strAppend As String
    Dim strUpdate As String
    Dim Rw As Long
    
    strPath = ActiveWorkbook.Path & "\Database5.mdb"
    
    'create the connection to the database
    Set cnn = New ADODB.Connection
    MyConn = strPath

    With cnn
       .Provider = "Microsoft.Jet.OLEDB.4.0"
       .Open MyConn
    End With
    
    Sheets("Sheet3").Activate
    On Error GoTo Err_Handle
    For Rw = 2 To Range("A" & Rows.Count).End(xlUp).Row
        strAppend = "INSERT INTO Orders (Client, OrderNo, Qty, State, Postcode, Price, Item )" _
                & "VALUES ('" & Cells(Rw, 1) & "'," & Cells(Rw, 2) & "," _
                & Cells(Rw, 3) & ",'" & Cells(Rw, 4) & "','" & Cells(Rw, 5) _
                & "'," & Cells(Rw, 6) & ",'" & Cells(Rw, 7) & "' )"
        cnn.Execute strAppend
    Next Rw
    
Err_Exit:
    cnn.Close
    Set cnn = Nothing
Exit Sub

Err_Handle:
    Select Case Err.Number
        Case -2147467259 'Primary key violation
            strUpdate = "UPDATE Orders SET Client = '" & Cells(Rw, 1) _
                    & "', State = '" & Cells(Rw, 4) & "', Postcode = '" _
                    & Cells(Rw, 5) & "', Price = " & Cells(Rw, 6) _
                    & " WHERE OrderNo&Qty&Item = '" & strKey & "'"
            cnn.Execute strUpdate
            Resume Next
        Case Else
            MsgBox Err.Number & "; " & Err.Description
            Resume Err_Exit
    End Select
    
End Sub
Note assumptions:
1. Database is called Database5.mdb and is in the same directory as the Excel file. If your file is an .accdb you will need to change the provider line to this:
Code:
       .Provider = "Microsoft.ACE.OLEDB.12.0"
2. You will need to set a reference to the Microsoft ActiveX Data Objects 2.8 Library
3. The database table is called Orders. The field datatypes are Text, Number, Number, Text, Text, Number, Text

I tested an insert/append of 160 records into 180,000 records. The process is quite slow; takes maybe a minute to run.

Denis
 
Upvote 0
Deniis, Thanks so much for your straight-forward solution. I'll check out next Wend. when back at work. I can't access the database until then. Once again Thanks! and Happy New Year to you and yours. Ted H.
 
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,006
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top