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
4 4559
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): - Dim rsForm as Dao.Recordset
-
Dim rsDef as Dao.Recordset
-
Dim rsJoin As Dao.Recordset
-
dim strFind as string
-
-
set rsForm=CurrentDB.Openrecordset("tblForms",dbOpenDynaset)
-
set rsDef=CurrentDB.Openrecordset("tblFormDeficiency",dbOpenDynaset)
-
set rsJoin=CurrentDB.Openrecordset("tblDeficiencyType",dbOpenDynaset)
-
-
-
'Loop through Forms
-
Do While Not rsForm.Eof
-
'Reset rsDef back to start
-
rsDef.MoveFirst
-
-
'Loop Through defs
-
Do While Not rsDef.Eof
-
'Check whether combination exists
-
strFind="(FormIDFK=" & rsForm!FormID & ") AND (DeficiencyTypeIDFK=" & rsDef!DeficiencyTypeID & ")" )
-
rsJoin.FindFirst strFind
-
If rsJoin.NoMatch then
-
'Combo not found, add it
-
rsJoin.AddNew
-
rsJoin!DeficiencyTypeIDFK=rsDef!DeficiencyTypeID
-
rsJoin!FormIDFK=rsForm!FormID
-
rsJoin.Update
-
End If
-
rsDef.Movenext
-
Loop
-
-
-
-
rsForm.MoveNext
-
loop
-
-
'Cleanup
-
set rsForm=Nothing
-
set rsDef=Nothing
-
set rsJoin=Nothing
That was perfect Smiley...you have no idea how much that helps.
Thanks again,
beacon
Im glad that it worked for you. :)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
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...
| | |