Solved

Parse Excel file and update data in access using VBA from access

Posted on 2014-01-30
2
2,747 Views
Last Modified: 2014-01-31
Hi,
I was wondering if some one can help me modify my access file.

I have access database and form which transfers data from excel file to access. The simple transfer is working but I have the following problem:
The transfer works vin a very simple way it takes content of excel file and transfers it to access table. Now in many instances excel file has the same datasets as already in the database plus new datasets. I need to do the following

1. For new data I just need to add it to DB, this seems to be working
2. For datasets that are not new and already exist in db, i need to compare and see if there are any changes in fields value_4 and value_6. If it is different then i need to replace the values in those fields. If it is the same then I do not touch that dat row.


I have atatched sample files excel and db file.
The Value 9 field in both excel and access are calculated fields(value1 +value2+value3). So records have to be compared by those fileds since that represents uniquness .

The module basFileImport is where the code for updating data happens.

In excel file row 26 and 20 is different than in db. rows 33 and 39 are not in db at all.

Any help will be apreciated.

Thank you!
testing-auto-update.xlsx
data-transfer-sample.accdb
0
Comment
Question by:maximyshka
2 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39822702
That's a fairly complex routine. What have you tried so far that didn't work?

If Value1 + Value2 + Value3 define a unique record, and if you have THOSE values in your incoming file, you could first check to see if that exists in the destination table (you'd do this in the ProcessFileImport, just after the PostMessage "Processing Record" line). If you determine that a record already exists, you wouldn't use AddNew:

intRec = intRec + 1
               PostMessage "Processing record " & intRec & ".  {StoreID=" & wks.Cells(intRow, 1) & "}"

Dim rstCheck As DAO.Recordset
Set rstCheck = Currentdb.OpenRecordset("SELECT Count(*) FROM YourDestinationTable WHERE Value1=" & rstRead("Value1") & " AND Value2=" & rstRead("Value2") & " AND Value3=" & rstRead(Value3"))

If (rstCheck.EOF And rstCheck.BOF) Then
  '/ did not find a record, so continue on with the current code you have
               rstWrite.AddNew
               Do Until rstRead.EOF
               etc etc

Else
  '/ found a record, so update it:
  Currentdb.Execute "UPDATE YourDestinationTable SET Value4=" & rstRead("Value4") & " AND Value5=" & rstRead("Value5") & " AND so on .... WHERE Value1=" rst("VAlue1") & " AND Value2=" & rstRead("Value2") & " AND Value3=" & rstRead("Value3")

End If

Open in new window

So essentially you have two distinct paths the process can go - either it adds a new record, or if finds an existing record and updates that record.
0
 

Author Comment

by:maximyshka
ID: 39824070
I did it alittle bit differently. I created temp table where i first copy all records from excel using my procedure and then i run 2 queries. 1- append query with outter join sub query, 2- update query based on join. It works but i gues not very eficient. Your solution is better .

thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question