Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-30
2
Medium Priority
?
2,874 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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

927 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