ADOFromExcelToAccess

haffy311

Board Regular
Joined
Jan 20, 2011
Messages
84
Hello,

I have VBA code which works perfectly to export the excel data into an access table.

One thing I would like to do is only export the rows where the columns containing the date stamp (column I) = the current date. This would help me from exporting data that.s already recorded in the access table.

Any advice would appreciated - current code is below.

Code:
Private Sub ADOFromExcelToAccess()


Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim r As Long
'as long as the excel file is stored in the same location as the access file, this will find it.
dbpath = Application.ActiveWorkbook.Path & "\JLR CANADA_Concept_Log.accdb"
Set cn = New ADODB.Connection


cn.Open "Provider=Microsoft.ace.OLEDB.12.0;Data Source=" & dbpath & ";"


Set rs = New ADODB.Recordset
'opens the table in access
rs.Open "Concept_Logging", cn, adOpenKeyset, adLockOptimistic, adCmdTable


r = 1
Do While Len(Range("A" & r).Formula) > 0
With rs
.AddNew
'Access table field translation
.Fields("Material Number") = Range("A" & r).Value
.Fields("Width") = Range("B" & r).Value
.Fields("Depth") = Range("C" & r).Value
.Fields("Height") = Range("D" & r).Value
.Fields("WEIGHT") = Range("E" & r).Value
.Fields("PKG QTY") = Range("F" & r).Value
.Fields("Put away Qty") = Range("G" & r).Value
.Fields("WHN") = Range("H" & r).Value
.Fields("Date Stamp") = Range("I" & r).Value
.Fields("Time Stamp") = Range("J" & r).Value
.Fields("USER") = Range("K" & r).Value
.Fields("Option 1") = Range("L" & r).Value
.Fields("Option 2") = Range("M" & r).Value
.Fields("Option 3") = Range("N" & r).Value
.Fields("Option 4") = Range("O" & r).Value
.Fields("Option 5") = Range("P" & r).Value
.Update
End With
r = r + 1
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
    
End Sub
 
Last edited by a moderator:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
try like this:
Code:
Private Sub ADOFromExcelToAccess[COLOR=#ff0000]Today[/COLOR]()


    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim r As Long
    'as long as the excel file is stored in the same location as the access file, this will find it.
    dbpath = Application.ActiveWorkbook.Path & "\JLR CANADA_Concept_Log.accdb"
    Set cn = New ADODB.Connection
    
    cn.Open "Provider=Microsoft.ace.OLEDB.12.0;Data Source=" & dbpath & ";"
    
    Set rs = New ADODB.Recordset
    'opens the table in access
    rs.Open "Concept_Logging", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    
    r = 1
    Do While Len(Range("A" & r).Formula) > 0
[COLOR=#ff0000]        If Range("I" & r).Value = Date Then[/COLOR]
            With rs
                .AddNew
                    'Access table field translation
                    .Fields("Material Number") = Range("A" & r).Value
                    .Fields("Width") = Range("B" & r).Value
                    .Fields("Depth") = Range("C" & r).Value
                    .Fields("Height") = Range("D" & r).Value
                    .Fields("WEIGHT") = Range("E" & r).Value
                    .Fields("PKG QTY") = Range("F" & r).Value
                    .Fields("Put away Qty") = Range("G" & r).Value
                    .Fields("WHN") = Range("H" & r).Value
                    .Fields("Date Stamp") = Range("I" & r).Value
                    .Fields("Time Stamp") = Range("J" & r).Value
                    .Fields("USER") = Range("K" & r).Value
                    .Fields("Option 1") = Range("L" & r).Value
                    .Fields("Option 2") = Range("M" & r).Value
                    .Fields("Option 3") = Range("N" & r).Value
                    .Fields("Option 4") = Range("O" & r).Value
                    .Fields("Option 5") = Range("P" & r).Value
                .Update
            End With
[COLOR=#ff0000]        End If[/COLOR]
        r = r + 1
    Loop
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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