G
Guest
I am trying to insert multiple records into a table if more than one employee
has attended a training class via a multi-select list box.
Table: tbl_Main_Table
List box: lst_Emp
The code behind the Save button on the form is as follows:
Private Sub Cmd_Save_Click()
On Error GoTo Err_Cmd_Save_Click
Dim iItem As Integer
Dim lngEmpTraining As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
' save the current record if it's not saved
If Me.Dirty = True Then
Me.Dirty = False
End If
Set db = CurrentDb
' Open a Recordset based on the table
Set rs = db.OpenRecordset("tbl_Main_Table", dbOpenDynaset)
With Me!lst_Emp
' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
lngEmpTraining = .Column(0, iItem)
' Determine whether this EmployeeID-TrainingID combination is in
the table
rs.FindFirst "[Class_ID] = " & Me.Class_ID & " AND [Employee_ID]
= " & lngEmpTraining & " AND [Date] = " & Me.Date
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.AddNew
rs!Class_ID = Me.Class_ID
rs!Comment = Me.Comment
rs!Date = Me.Date
rs!Hours = Me.Hours
rs!Instructor_ID = Me.Instructor_ID
rs!Employee_ID = lngEmpTraining
rs.Update
End If
' if it wasn't selected, ignore it
Else
If Not .Selected(iItem) Then
rs.Delete ' delete this record if it's been deselected
End If
' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing
Exit_Cmd_Save_Click:
Exit Sub
Err_Cmd_Save_Click:
MsgBox Err.Description
Resume Exit_Cmd_Save_Click
End Sub
After many long hours, I finally got the code to insert records into the
table, but the logic is not deleting a record that has been de-selected (if
the user selected a name accidentally). Can anyone see what is not working?
Thanks,
Melanie
has attended a training class via a multi-select list box.
Table: tbl_Main_Table
List box: lst_Emp
The code behind the Save button on the form is as follows:
Private Sub Cmd_Save_Click()
On Error GoTo Err_Cmd_Save_Click
Dim iItem As Integer
Dim lngEmpTraining As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
' save the current record if it's not saved
If Me.Dirty = True Then
Me.Dirty = False
End If
Set db = CurrentDb
' Open a Recordset based on the table
Set rs = db.OpenRecordset("tbl_Main_Table", dbOpenDynaset)
With Me!lst_Emp
' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
lngEmpTraining = .Column(0, iItem)
' Determine whether this EmployeeID-TrainingID combination is in
the table
rs.FindFirst "[Class_ID] = " & Me.Class_ID & " AND [Employee_ID]
= " & lngEmpTraining & " AND [Date] = " & Me.Date
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.AddNew
rs!Class_ID = Me.Class_ID
rs!Comment = Me.Comment
rs!Date = Me.Date
rs!Hours = Me.Hours
rs!Instructor_ID = Me.Instructor_ID
rs!Employee_ID = lngEmpTraining
rs.Update
End If
' if it wasn't selected, ignore it
Else
If Not .Selected(iItem) Then
rs.Delete ' delete this record if it's been deselected
End If
' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing
Exit_Cmd_Save_Click:
Exit Sub
Err_Cmd_Save_Click:
MsgBox Err.Description
Resume Exit_Cmd_Save_Click
End Sub
After many long hours, I finally got the code to insert records into the
table, but the logic is not deleting a record that has been de-selected (if
the user selected a name accidentally). Can anyone see what is not working?
Thanks,
Melanie