473,783 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

23 New Member
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","fldP io","fldFam","f ldSer","fldTra" ,"fldInt". 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, 135 views)
Mar 6 '10 #1
6 2282
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
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 New Member
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 Recognized Expert Moderator Top Contributor
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,577 Recognized Expert Moderator MVP
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 New Member
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,577 Recognized Expert Moderator MVP
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
9112
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 thing :). I have a table of email addresses of people who want to get invited to parties. Each row contains information like email address, city, state, country, and preferences for what types of events are of interest. The primary key is an...
3
10082
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 clustered indexes on Key. I want to do:
14
4301
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 salvage the records from the many table and without going into detail, one of the reasons I can't do the opposite as there are records in the ONE table that I need to keep even if they don't have any child records in the MANY table. Below I created...
18
5986
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 against a different table (that uses the LIKE predicate) but cannot get around the SQL0132 error . I have tried the hex notation after the LIKE such as (without the quotes)... " where colNewPartNum like ( X'27' || nnn.colPartNum || X'27) " ,...
8
3724
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: "Date","P1","P2","P3","P4","P5","P6","P7","P8","P9","P10","P11","P12","P13","P14","P15","P16","P17","P18","P19","P20","P21" 1/1/2005,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 1/2/2005,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
8
5355
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, serching dt2 for a matching fldA and if found do the update and then move on to the next row? Thanks Greg
2
19609
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 field in table2. What I have is a form that is linked to Table2. If the users want to change a field in the main database (table1), they fill the change in to the form (which is linked to table2) If there is no change to the field, they simply...
11
1848
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; 1. Regenerated the data adapter sqls by running the data adapter wizard and pasting the resulting code into my app. 2. Deleted the data adapter correspond to the relevant access table from the
16
3520
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 renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
2
2638
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 update the information which is stored in a SQL database. In testing we noticed that the form was updating correctly but the update mechanism was also updating the first record of the table in the sql database every time. No error messages are on...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.