Import MS Access data into Excel based on values in Excel

ITSLICE

New Member
Joined
Dec 11, 2017
Messages
1
Hi,

Hope someone can help me with this problem that I can't figure out. I have a database(accdb) table with 4 fields, ID, First Name, Last Name, Start Date and I have an Excel spreadsheet that has 3 columns, IDEMP, Full Name, Start Date.

I'm trying to create a macro in Excel to import the Start Date from DB that match the IDEMP cell in the Excel sheet and update the Start Date column in excel.

I'm able to import all the data from DB to Excel through VBA but I only want to import the start dates for only the IDEMP values that match the ID.

Appreciate any help.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I assume that Id is not duplicate in database.

Code:
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim rEmpID As Range


Set con = New ADODB.Connection


con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Desktop\Sample.accdb;Persist Security Info=False;"


con.Open


Sql = "Select * from EmpData"


Set rs = con.Execute(Sql)


For Each rEmpID In Range("A1:A100")
    rs.MoveFirst
    Do Until rs.EOF
        If rs.Fields("ID") = rEmpID.Value Then rEmpID.Offset(0, 2) = rs.Fields("StartDate")
        rs.MoveNext
    Loop
Next rEmpID


rs.Close
con.Close
 
Upvote 0

Forum statistics

Threads
1,224,808
Messages
6,181,072
Members
453,020
Latest member
mattg2448

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