473,385 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VBA to update matching fields from one table and set unmatching fields to null

23
In my Access Database in Table1, I have one record that looks something like this. (eg. "fldProd" represents field name, while "Apple" represents field value.)
fldProd - Apple (text value)
fldPIO - CF (text value)
fldFam - 1 (text value)
fldSer - 2 (text value)
fldTra - 3 (text value)
fldInt - LZ (text value)
fildQty - 5 (integer value)

The MasterTable looks like this.
fldProd - Apple
fldDesc - 3D All
fldPio - CF
fldFam - 1
fldSer - null
fldTra - 3
fldInt - null

So essentially I would need a VBA module the would match up the MasterTable fields from a record with corresponding fields in the record from Table1. Notice, "fldDesc" in MasterTable does not exist in Table1, so it wouldn't be apart of the code's matching process. For each record it will only attempt to match "fldProd","fldPio","fldFam","fldSer","fldTra","fld Int". So "fldQty" in Table1 would remain untouched as well.

Then the module will need to nullify fields in Table1 that appeared "null" in the MasterTable. So after the VBA module was executed, the record in Table1 would look like this.

fldProd - Apple (text value)
fldPIO - CF (text value)
fldFam - 1 (text value)
fldSer - null
fldTra - 3 (text value)
fldInt - null
fildQty - 5 (integer value)

Any of you experts have any ideas? I'm not a code writer, so I'm totally stuck.

I attached an excel file that illustrates what I'm trying to accomplish for your reference.
Attached Files
File Type: zip Example1.zip (14.9 KB, 134 views)
Mar 6 '10 #1

✓ answered by KPR1977

Thanks so much for answering my post. =)

I've been trying to get this figured out for three days now, but I finally received an answer in another forum. Here's the function that will accomplish precisely what I need. The credit goes to ajetrumpet from Iowa City.


Expand|Select|Wrap|Line Numbers
  1. Function kp()
  2.  
  3. Dim updaterec As Boolean
  4. Dim db As dao.Database
  5. Dim rs As dao.Recordset
  6. Dim rs2 As dao.Recordset
  7.  
  8. Dim Ctr As Integer
  9.  
  10. Set db = CurrentDb
  11. Set rs = db.OpenRecordset("SELECT " & _
  12.                           "fldPio, fldFam, fldSer, fldTra, fldInt, fldupdated " & _
  13.                           "FROM table1", dbOpenDynaset)
  14. Set rs2 = db.OpenRecordset("SELECT " & _
  15.                            "fldPio, fldFam, fldSer, fldTra, fldInt " & _
  16.                            "FROM mastertable", dbOpenDynaset)
  17.  
  18. rs.MoveFirst
  19. rs2.MoveFirst
  20.  
  21. With rs2
  22.  
  23.    Do Until .EOF
  24.       Do Until rs.EOF
  25.  
  26.       If rs!fldupdated = 0 Then
  27.  
  28.          updaterec = True
  29.  
  30.             For Ctr = 0 To 4
  31.                If Not IsNull(.Fields(Ctr)) Then
  32.                   If .Fields(Ctr) <> rs.Fields(Ctr) Then
  33.                      updaterec = False
  34.                         Exit For
  35.                   End If
  36.                End If
  37.             Next Ctr
  38.  
  39.          If updaterec = True Then
  40.             rs.Edit
  41.                'rs!fldprod = IIf(IsNull(!fldprod), Null, !fldprod)
  42.                rs!fldPio = IIf(IsNull(!fldPio), Null, !fldPio)
  43.                rs!fldFam = IIf(IsNull(!fldFam), Null, !fldFam)
  44.                rs!fldSer = IIf(IsNull(!fldSer), Null, !fldSer)
  45.                rs!fldTra = IIf(IsNull(!fldTra), Null, !fldTra)
  46.                rs!fldInt = IIf(IsNull(!fldInt), Null, !fldInt)
  47.                rs!fldupdated = -1
  48.             rs.Update
  49.          End If
  50.  
  51.       End If
  52.          rs.MoveNext
  53.  
  54.       Loop
  55.            .MoveNext
  56.          rs.MoveFirst
  57.    Loop
  58.  
  59. End With
  60.  
  61. rs.Close
  62. rs2.Close
  63.  
  64. Set db = Nothing
  65. Set rs = Nothing
  66. Set rs2 = Nothing
  67.  
  68. MsgBox "Done!"
  69. End Function
  70.  

6 2259
TheSmileyCoder
2,322 Expert Mod 2GB
Could you attach your DB to this thread? Would make it alot easier to write the SQL statements.

Make a new DB, and just import those 2 tables into the new db, and attach that. That would give us only the information we need. Should be rather simple to write the 2 SQL statements to perform what you want.
Mar 6 '10 #2
KPR1977
23
Thanks so much for answering my post. =)

I've been trying to get this figured out for three days now, but I finally received an answer in another forum. Here's the function that will accomplish precisely what I need. The credit goes to ajetrumpet from Iowa City.


Expand|Select|Wrap|Line Numbers
  1. Function kp()
  2.  
  3. Dim updaterec As Boolean
  4. Dim db As dao.Database
  5. Dim rs As dao.Recordset
  6. Dim rs2 As dao.Recordset
  7.  
  8. Dim Ctr As Integer
  9.  
  10. Set db = CurrentDb
  11. Set rs = db.OpenRecordset("SELECT " & _
  12.                           "fldPio, fldFam, fldSer, fldTra, fldInt, fldupdated " & _
  13.                           "FROM table1", dbOpenDynaset)
  14. Set rs2 = db.OpenRecordset("SELECT " & _
  15.                            "fldPio, fldFam, fldSer, fldTra, fldInt " & _
  16.                            "FROM mastertable", dbOpenDynaset)
  17.  
  18. rs.MoveFirst
  19. rs2.MoveFirst
  20.  
  21. With rs2
  22.  
  23.    Do Until .EOF
  24.       Do Until rs.EOF
  25.  
  26.       If rs!fldupdated = 0 Then
  27.  
  28.          updaterec = True
  29.  
  30.             For Ctr = 0 To 4
  31.                If Not IsNull(.Fields(Ctr)) Then
  32.                   If .Fields(Ctr) <> rs.Fields(Ctr) Then
  33.                      updaterec = False
  34.                         Exit For
  35.                   End If
  36.                End If
  37.             Next Ctr
  38.  
  39.          If updaterec = True Then
  40.             rs.Edit
  41.                'rs!fldprod = IIf(IsNull(!fldprod), Null, !fldprod)
  42.                rs!fldPio = IIf(IsNull(!fldPio), Null, !fldPio)
  43.                rs!fldFam = IIf(IsNull(!fldFam), Null, !fldFam)
  44.                rs!fldSer = IIf(IsNull(!fldSer), Null, !fldSer)
  45.                rs!fldTra = IIf(IsNull(!fldTra), Null, !fldTra)
  46.                rs!fldInt = IIf(IsNull(!fldInt), Null, !fldInt)
  47.                rs!fldupdated = -1
  48.             rs.Update
  49.          End If
  50.  
  51.       End If
  52.          rs.MoveNext
  53.  
  54.       Loop
  55.            .MoveNext
  56.          rs.MoveFirst
  57.    Loop
  58.  
  59. End With
  60.  
  61. rs.Close
  62. rs2.Close
  63.  
  64. Set db = Nothing
  65. Set rs = Nothing
  66. Set rs2 = Nothing
  67.  
  68. MsgBox "Done!"
  69. End Function
  70.  
Mar 6 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Im glad you solved your problem.

We also appreciate that you take the time to return, and properly close your question. :)
Mar 6 '10 #4
NeoPa
32,556 Expert Mod 16PB
Thanks for posting your answer.

I'm a little confused though. Mainly with the description of the problem. Is it an update of existing records that's required? Or an Append of new ones?

Either way, I would expect a simple UPDATE (or APPEND) SQL script to do the whole job for you in a single go. Am I missing something? Is it more complicated than I'm giving it credit for?
Mar 7 '10 #5
KPR1977
23
Hi NeoPa, it's somewhat more complicated. Basically I'm wanted to update existing records. I wasn't able to get an update query to accomplish fully what I needed. The above VBA module actually identifies which records did not have any match in the MasterTable, which is extremely pertinent to another process that I'm working with. Hope this makes sense. =)
Mar 8 '10 #6
NeoPa
32,556 Expert Mod 16PB
It makes sense, but it doesn't really explain why an UPDATE query would not have done the job for you.

Never mind. I'm sure it's fine if you're happy.
Mar 10 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: David Berman | last post by:
I'm having a problem with an update operation in a stored procedure. It runs so slowly that it is unusable, unless I comment a part out in which case it is very fast. However, I need the whole...
3
by: Dan Berlin | last post by:
I have two tables: T1 : Key as bigint, Data as char(20) - size: 61M records T2 : Key as bigint, Data as char(20) - size: 5M records T2 is the smaller, with 5 million records. They both have...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
8
by: Greg Peck | last post by:
Hi I need to update a field (fldX) in one DataTable (dt1) where there is a match on fldA in another DataTable (dt2). Is the only way to do this by itterating through the DataRows of dt1,...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
11
by: John | last post by:
Hi I had a working vs 2003 application with access backend. I added a couple fields in a table in access db and then to allow user to have access to these fields via app I did the following; ...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.