Hi Stefano
I am the author of that sample database, so maybe I can help out here
I assume you understand the principles of setting up a many-to-many
relationship using a junction table, so I won't explain that.
Even though a many-to-many relationship is usually symmetrical, from the
point of view of one of the sides it is not. For example, in the sample DB
there is a form based on the "People" table which lists the Clubs that
person belongs to (traditionally this would be done with a subform). In this
example I call the People table the "Master table" and Clubs the "Lookup
table".
In another form, based on Clubs, the relationship is reversed.
To use the MtoMListHandler, you just need to create on your form five
controls:
one listbox to list the related items from the "lookup table"
one combo box to select new items to add to the list
two command buttons: one to add new items and one to delete items from
the list
one rectangle - any size, any position (explained below**)
Now, you just need some simple code in your form. This is from the frmClubs
form, where the control lists the members of the given club:
========== start code ==============
Private MemberList As MtoMListHandler
Private Sub Form_Load()
Set MemberList = New MtoMListHandler
With MemberList
' this is the name of the listbox control
Set .OptionList = lstMembers
' this is the name of the "Add" button
Set .AddButton = cmdAddMember
' this is the name of the "Delete" button
Set .DeleteButton = cmdDeleteMember
' this is the name of the combo box
Set .AddCombo = cboAddMember
' this is the name of the rectangle
Set .ComboMask = boxMaskMember
' set the name of the lookup table
.LookupTable = "People"
' set the name of the junction table
.JunctionTable = "People_Clubs"
' set the name of the PK of the "master table"
.MasterPK = "ClubID"
' set the name of the PK of the "lookup table"
.LookupPK = "PersonID"
' set the name of the field in the JT that is related to the master table
.MasterFK = "ClubFK"
' set the name of the field in the JT that is related to the lookup table
.LookupFK = "PersonFK"
' this is the name of a field in the lookup table that contains text to be
displayed in the list and combo boxes
' in this case, it is an expression comprising two fields:
.LookupText = "FirstName & ' ' & LastName"
End With
End Sub
============= end code ======================
This is all the code you need. Everything else is taken care of by the
MtoMListHandler class.
You say in your case that one or both of the "one side" keys is not the PK,
but another field allowing no duplicates. I don't see any reason why that
should not work. Just be sure to specify that field, not the PK, to the
MasterPK and/or LookupPK properties when you set up the class.
If you are still having trouble, post back with more information about the
structure of your three tables - field names and data types, and how the
relationships are set up.
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
Many thanks Doug for your help, now I found them!
I have one more question to ask for.
I'd like to use the sample database called "Easy Maintenance of "Many-
To-Many" Data with a Form", but I'm having some problems to make it
works on my db.
I imported and copied all the neccesary from the sample to my db but
it doesn't seem work properly.
What I have to set about tables relationships on my db?
On my db, I have all relationships on a fields that are NOT primary
keys but only field indexed with no duplicate. Could be that the
problems?
In other words I haven't use the autonumber field to make the
relationships, but I preferred to use fields with random numbers.
Hope to receive an hand to someone,
Bye,
Stefano.