473,512 Members | 15,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to loop through a table, check for values, and insert if they don't exist?

beacon
579 Contributor
Hi everybody,

[Access 2003]

I have three tables: tblForms, tblFormDeficiency, tblDeficiencyType. The tblForms and tblDeficiencyType have a many-to-many relationship with each other with tblFormDeficiency acting as the JOIN table.

Over the past couple of years, I haven't had to update the forms or the deficiency types that often and I've always used INSERT INTO when a user selects a form or deficiency type on a form. This allowed me to delete the JOIN records for the forms/deficiencies when it was decided that a form/deficiency would no longer be needed.

However, yesterday I learned that I will need to make mass changes to the tblFormDeficiency and determined that the best way for me to do this would be to make sure there was a deficiency type linked to every form and use a yes/no field to determine whether or not it would be available to the user. This may not be the best route, but I want to try this out in expectation of another component that I will be working on in the near future that will allow others to update the forms and deficiencies through a form.

So, my question is how do I loop through all the records in tblFormDeficiency, compare the FormIDFK (foreign key for tblForms) and the DeficiencyTypeIDFK (foreign key for tblDeficiencyType) for each record to a counter for both the FormID (primary key for tblForms) and DeficiencyTypeID (primary key for tblDeficiencyType), and insert a record if that particular form/deficiency combo doesn't exist in tblFormDeficiency?

There's one other slight issue to consider. The FormID in tblForms is easy...it's counter will be 1 to 96. However, the counter for DeficiencyTypeID will be 1 to 67, skip 68 (at some point this record was deleted), then continue from 69 through 84.

I'm not very good with DAO or ADO, but would prefer a solution that uses DAO, if possible.

If I've left anything out in my explanation, please let me know.

Thanks,
beacon
Nov 16 '11 #1
4 4559
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
This is code written as is, not tested. If this doesn't work for you, and you need more help, it would be nice if could make a blank database, copy the 3 tables into it, and pm me, or attach it.

Anyway here goes (Please backup, before running code :O):
Expand|Select|Wrap|Line Numbers
  1. Dim rsForm as Dao.Recordset
  2. Dim rsDef as Dao.Recordset
  3. Dim rsJoin As Dao.Recordset
  4. dim strFind as string
  5.  
  6. set rsForm=CurrentDB.Openrecordset("tblForms",dbOpenDynaset)
  7. set rsDef=CurrentDB.Openrecordset("tblFormDeficiency",dbOpenDynaset)
  8. set rsJoin=CurrentDB.Openrecordset("tblDeficiencyType",dbOpenDynaset)
  9.  
  10.  
  11. 'Loop through Forms
  12. Do While Not rsForm.Eof
  13.   'Reset rsDef back to start
  14.     rsDef.MoveFirst
  15.  
  16.   'Loop Through defs
  17.     Do While Not rsDef.Eof
  18.       'Check whether combination exists
  19.         strFind="(FormIDFK=" & rsForm!FormID & ") AND (DeficiencyTypeIDFK=" & rsDef!DeficiencyTypeID & ")" )
  20.         rsJoin.FindFirst strFind
  21.         If rsJoin.NoMatch then
  22.           'Combo not found, add it
  23.           rsJoin.AddNew
  24.             rsJoin!DeficiencyTypeIDFK=rsDef!DeficiencyTypeID
  25.             rsJoin!FormIDFK=rsForm!FormID 
  26.           rsJoin.Update
  27.         End If
  28.       rsDef.Movenext
  29.     Loop
  30.  
  31.  
  32.  
  33.   rsForm.MoveNext
  34. loop
  35.  
  36. 'Cleanup
  37. set rsForm=Nothing
  38. set rsDef=Nothing
  39. set rsJoin=Nothing
Nov 16 '11 #2
beacon
579 Contributor
That was perfect Smiley...you have no idea how much that helps.

Thanks again,
beacon
Nov 16 '11 #3
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Im glad that it worked for you. :)
Nov 16 '11 #4
NeoPa
32,557 Recognized Expert Moderator MVP
For this I would suggest an APPEND query in SQL.

If the data is available from [tblForms] and [tblDeficiencyType] then create a subquery with a cartesian product (Both tables in the FROM clause with no JOIN specified) and append the result-set into [tblFormDeficiency]. If you're worried about records failing (as would happen for pre-existing combinations) then simply link that to the existing table and filter for combinations that don't already exist.
Nov 17 '11 #5

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

Similar topics

20
10084
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
3
4861
by: Dave | last post by:
How can i check to see if a link (file) exist while a web page is loading. If the page doesn't exist - i want the hyperlink to go an error page telling the user the link doesn't exist otherwise...
6
27837
by: Roy Gourgi | last post by:
Hi, I am trying to add a row to my table but I get the error message "invalid column name SOBN and BN1" on this statement. Basically, I am trying to add the row into the same table that I am...
6
4495
by: Erik Johnson | last post by:
Maybe I just don't know the right special function, but what I am wanting to do is write something akin to a __getattr__ function so that when you try to call an object method that doesn't exist,...
4
4552
by: M Bourgon | last post by:
I have two SPs, call them Daily and Weekly. Weekly will always call Daily, but Daily can run on its own. I currently use a global temp table because certain things I do with it won't work with a...
1
1067
by: mfaisalwarraich | last post by:
Hi Experts Around the World. I made a table called mainTable, two fields of this table one is SNO and other is Relation are text data types. SNO and Relation can be any string values. Relation stores...
0
7252
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,...
0
7153
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...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7517
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...
0
3230
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
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 ...
0
452
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...

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.