Solved

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

Posted on 2014-01-30
2
2,770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

752 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