Many to Many implementation question for use in a form (1 Viewer)

darren726

New member
Local time
Today, 11:11
Joined
Feb 25, 2020
Messages
2
Hello all,

I have a scenario where a many to many table is needed but I can't it to work in the way that I need. I currently have to add 10 columns to my main table then use a union query to stack them with the rows primary key to recreate the many to many table I want to use. The 10 columns pull accessories from another table which have different categories. The 10 columns I add to the main table are for each category which pull all the available accessories for that category via a query.

When I add a new primary record with all it's info on the mainform, I want to use a subform within the main form with 10 combo boxes for the user to select the desired accessory for each category and have it add all 10 of those selections with the primary records key as 10 individual rows to the many to many table. I feel like I'm overlooking something obvious but it's just not coming to me. The 10 extra columns in main table work fine for what I'm doing but I'd much rather setup the relationship properly with a many to many table. If I'm mistaken in this thinking, please let me know and thanks for any help/insight you can provide.

Here is a very simplified version of the data scenario I'm trying to accomplish. I'm only using 3 categories/columns for these examples:

Door_Locations - Main form for adding new record
PK_DoorDoor IdentifierRoomDoor
Part No
X CordY CordCloser
Required
Holdback RequiredLock Required
11-1ERoom 162B-2156.2546.125YESYESYES
21-2CRoom 145A-665422.625NOYESNO
33-1BRoom 392M-7426.185-146YESNOYES
42-1CRoom 284A-123.625-82.75YESNONO

Detail_List - The table that Door_Locations pulls from to get the accessories by the 10 categories I mentioned
PK_DetailDetailDetail ForDescription
1N/ANot UsedN/A
21ACloserDetailDoor Closer 1
31BCloserDetailDoor Closer 2
41CCloserDetailDoor Closer 3
52AHoldbackDetailHoldback 1
62BHoldbackDetailHoldback 2
72CHoldbackDetailHoldback 3
83ALockDetailLock 1
93BLockDetailLock 2
103CLockDetailLock 3
114AInstallationDetailInstall 1
124BInstallationDetailInstall 2
134CInstallationDetailInstall 3

This is what the Many To Many Table would look like
PK_DetailRequiredFK_DoorFK_Detail
114
215
3110
4111
521
626
721
8212
932
1031
1139
12313
1344
1441
1541
16412

This is how I have to setup the main table then use a union query to recreate the Many To Many table I want
PK_DoorDoor IdentifierRoomDoor
Part No
X CordY CordCloser
Required
Closer DetailHoldback RequiredHoldback DetailLock RequiredLock DetailInstallation Detail
11-1ERoom 162B-2156.2546.125YES1CYES2AYES3C4A
21-2CRoom 145A-665422.625NON/AYES2BNON/A4B
33-1BRoom 392M-7426.185-146YES1ANON/AYES3B4C
42-1CRoom 284A-123.625-82.75YES1CNON/ANON/A4B

Here is how I want to use the form with above data and the relationship setup between the tables.
Example Door_List Form.JPG

Database Relationship Example.JPG


Thanks again for any help you can provide.
 

June7

AWF VIP
Local time
Today, 08:11
Joined
Mar 9, 2014
Messages
5,466
Assembly/manufacturing type db is common topic of threads. I am sure a search in forums will present a selection for study.

I believe it is one of the most difficult type to build and consider myself lucky I never have.
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:11
Joined
Aug 11, 2003
Messages
11,695
Common solution is to insert a junction table to resolve the N:M problem.

N:1 and 1:M as a result you have an extra layer to add information to that is pertinent to the particular relationship as well
 

darren726

New member
Local time
Today, 11:11
Joined
Feb 25, 2020
Messages
2
Well I got the insertion method to work with my desired setup. I used the following public function to restrict the recordcount on the subform to the desired qty of dropdown categories I needed. Because of this, my dropdowns are no longer filtered by each individual category. I'm wondering if there is a way I can use the Where clause with this function to set a filter on the combo box for each record count of the subform?

Would I do something like this, use a case statement or something else?
If .recordcount = 1 Then
Select Detail_List.PK_Details, Detail_List.Detail AS Closer_Detail
Where Detail_List.DetailFor = "Closer"
Else If
If .recordcount = 2 Then
Select Detail_List.PK_Details, Detail_List.Detail AS Holdback_Detail
Where Detail_List.DetailFor = "Holdback"
Else If
etc.... for each additional combo

Here's the function I mentioned:

Module
--------------------------
Public Function LimitRecords( _
frm As Access.Form, _
Optional RecLimit As Integer = 1)

' Limit the number of records in the form passed as
' to no more than the number specified by .

With frm.RecordsetClone
If .RecordCount <> 0 Then .MoveLast
frm.AllowAdditions = (.RecordCount < RecLimit)
End With

End Function
-------------------------
Private Sub Form_Current()

LimitRecords Me.subfrmAddDoor2.Form, 4

End Sub
---------------------------

This is how design view of my form looks. As you can see, subform is continuous form.
con_frm.JPG


This is working form now but you can see how the combo boxes now list all details when I wish to separate them by category to align with the text boxes. It's not a big deal in this sample database but the full database has 10 categories and well over 500 details.
formview.JPG
 
Last edited:

Users who are viewing this thread

Top Bottom