Using criteria (If/then) when exporting Excel data to Access DB

bdubb1979

New Member
Joined
Dec 9, 2009
Messages
3
Hi,

I have an Excel VBA procedure that exports sample data from an Excel worksheet into an Access DB table (the Access table has already been created, I'm only adding a new record to it). I only want this export to occur once daily (Day1, Day2, Day3, etc.) so there are not redundant records added to the Access table.

Is there a way to add conditions to the Excel VBA procedure to have it check the Access DB to see if a record has been added for that day? I want to use an If - then statement to check if the data to be added has the same date as a record already in the Access DB. However, I don't know how to reference an Access field from Excel VBA. Additionally, there are hundreds of records in the Access DB, so I would also need to specify where to look.

Any help would be great!

I'm running Excel 2003 and Access 2002.

Code:
Sub ADOFromExcelToAccess()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
    Set cn = New ADODB.Connection
    MyConn = "MyConnection"
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Open MyConn
    End With
    Set rs = New ADODB.Recordset
    rs.Open Updtbrx, cn, adOpenKeyset, adLockOptimistic, adCmdTable
        With rs
            .AddNew
            .Fields("Empty Field") = "Empty"
            .Fields("Sample Date") = SampleDate '[COLOR=Red]***Want to check Excel data against this field in the Access file[/COLOR]
            .Update
        End With
  
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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