G
Guest
On January 21, 2005, John Vinson posted code pertaiing to assigning multiple
selections from a ListBox to a table. I've taken his code, and modified it
for my use on a project I am working on, and it works beautifully (Thank you,
John!!)
The problem I am having is that when I run the code from a Save button, it
creates a blank entry first, and then it will create the record entries that
I need. How do I get rid of the blank entry?
Private Sub btn_Save_Click()
' Modified form original code provided by John Vinson
On Error GoTo PROC_ERR
Dim iItem As Long
Dim lngDP 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_DP_Assignments", dbOpenDynaset)
With Me!lstDeskProcedures
' Loop through all rows in the Listbox
For iItem = 1 To .ListCount - 1
lngDP = .Column(3, iItem)
' Determine whether this PositionID-DPID combination is currently
' in the table
rs.FindFirst "[Position_ID] = " & Me.[Position_ID] & " AND " &
"[DP_ID] = " & lngDP
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.AddNew
'rs!DP_Assignment_ID = Me.DP_Assignment_ID
rs!Position_ID = Me.[Position_ID]
rs!DP_ID = lngDP
'rs!Date_Assigned = Me.Date_Assigned
rs.Update
End If ' if it wasn't selected, ignore it
'Else
'If Not .Selected(iItem) Then
' delete this record if it's been deselected
'rs.Delete
'End If ' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing
Me.Requery
DoCmd.Close , , acSaveNo
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in btn_Save_Click:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
I've set the ListBox to show Headers based upon another post I read, but it
hasn't helped the issue. I'm not sure, but I think that the addtion of the
blank entry (Position_ID shows up, DP_ID does not) might have something to do
with the form opening and closing.
Does anyone have any suggestions? I thought about taking the easy way out,
and coding a delete query to run after the addition of the new records, but
I'd rather resolve the matter the right way.
Any help would be appreciated greatly.
Michael Koenig
selections from a ListBox to a table. I've taken his code, and modified it
for my use on a project I am working on, and it works beautifully (Thank you,
John!!)
The problem I am having is that when I run the code from a Save button, it
creates a blank entry first, and then it will create the record entries that
I need. How do I get rid of the blank entry?
Private Sub btn_Save_Click()
' Modified form original code provided by John Vinson
On Error GoTo PROC_ERR
Dim iItem As Long
Dim lngDP 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_DP_Assignments", dbOpenDynaset)
With Me!lstDeskProcedures
' Loop through all rows in the Listbox
For iItem = 1 To .ListCount - 1
lngDP = .Column(3, iItem)
' Determine whether this PositionID-DPID combination is currently
' in the table
rs.FindFirst "[Position_ID] = " & Me.[Position_ID] & " AND " &
"[DP_ID] = " & lngDP
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.AddNew
'rs!DP_Assignment_ID = Me.DP_Assignment_ID
rs!Position_ID = Me.[Position_ID]
rs!DP_ID = lngDP
'rs!Date_Assigned = Me.Date_Assigned
rs.Update
End If ' if it wasn't selected, ignore it
'Else
'If Not .Selected(iItem) Then
' delete this record if it's been deselected
'rs.Delete
'End If ' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing
Me.Requery
DoCmd.Close , , acSaveNo
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in btn_Save_Click:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
I've set the ListBox to show Headers based upon another post I read, but it
hasn't helped the issue. I'm not sure, but I think that the addtion of the
blank entry (Position_ID shows up, DP_ID does not) might have something to do
with the form opening and closing.
Does anyone have any suggestions? I thought about taking the easy way out,
and coding a delete query to run after the addition of the new records, but
I'd rather resolve the matter the right way.
Any help would be appreciated greatly.
Michael Koenig