Solved

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

Posted on 2014-01-30
2
2,693 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now