Jenny said:
You're the boss!
LOL! Well, that didn't quite answer my questions, but I think I get your
drift
I'm assuming the following table structures. Please correct me if I'm
wrong, because that might alter the way we do things:
tblCSD:
CSDID - autonumber, primary key
[... other inconsequential fields]
tblSpecialties:
SpecialtyID - autonumber, primary key
SpecialtyName - text, required, no duplicates
tblCSD_Specialties:
CSDFK - long integer
SpecialtiesFK - long integer
[these two fields TOGETHER should form a composite primary key]
First, add some controls to your form:
1. A listbox names lstSpecialties:
RowSource: <leave blank>
RowSourceType: Table/Query
ColumnCount: 2
BoundColumn: 1
ColumnWidths: 0 (this hides the first column)
You could give it an attached label with the caption "Specialties"
2. A combo box named cboAddSpecialty
RowSource: <leave blank>
RowSourceType: Table/Query
ColumnCount: 2
BoundColumn: 1
ColumnWidths: 0 (this hides the first column)
Visible: No
3. Two command buttons: cmdAddSpecialty and cmdDeleteSpecialty
Now, add the following event procedure code to your form module:
Private Sub Form_Current()
'Hide the combo just in case it didn't get hidden already
cboAddSpecialty.Visible = False
'Update the row source for the listbox for the current user
lstSpecialties.RowSource = "Select SpecialtyID, SpecialtyName " _
& "from tblSpecialties inner join tblCSD_Specialties on " _
& "tblSpecialties.SpecialtyID=tblCSD_Specialties.SpecialtiesFK " _
& "where CSDFK=" & Me.CSDID & ";"
End Sub
Private Sub cmdDeleteSpecialty_Click()
'If nothing is selected, give an error message
If lstSpecialties.ListIndex < 0 Then
MsgBox "Please select a specialty to delete!", vbInformation
Exit Sub
End If
' Delete the currently selected specialty from the junction table
CurrentDb.Execute "Delete From tblCSD_Specialties where CSDFK=" _
& Me.CSDID & " and SpecialtiesFK=" & lstSpecialties & ";"
'Requery the list
lstSpecialties.Requery
End Sub
Private Sub cmdAddSpecialty_Click()
'list only those specialties NOT already selected in the combo box
With cboAddSpecialty
.RowSource = "Select SpecialtyID, SpecialtyName From tblSpecialties " _
& "where Not SpecialtyID In (Select SpecialtiesFK from " _
& "tblCSD_Specialties where CDSFK= " & Me.CSDID & ");"
.Value = Null
.Visible = True
.SetFocus
.Dropdown
End With
End Sub
Private Sub cboAddSpecialty_AfterUpdate()
If cboAddSpecialty.ListIndex >= 0 Then
CurrentDb.Execute "Insert Into tblCSD_Specialties (CSDFK, " _
& "SpecialtiesFK) VALUES (" & Me.CSDID & ", " & cboAddSpecialty & ");"
lstSpecialties.Requery
lstSpecialties = cboAddSpecialty
lstSpecialties.SetFocus
End If
End Sub
Private Sub lstSpecialties_GotFocus()
cboAddSpecialty.Visible = False
End Sub
I hope this code is all OK - it's just off the top of my head and probably
full of syntax errors but have fun finding them
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand