Hi,
I have a form with a list box that shows the contents of a table and
managed to create an add record button with the wizard.
(DoCmd.GoToRecord , , acNewRec)
Now i want to make a delete record button based on what was selected
from the list box.
I have the basic code:
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
but how do i pass the value of what i want to delete to the delete
button? (ie me.mylist.value)
Thanks
Wayne Morgan - 12 Jan 2006 16:17 GMT
Since you are making selections from a listbox, I assume you want to be able
to delete multiple records if multiple items were selected in the listbox.
This can be done using a Delete query with the values from the listbox
concatenated in. The trick is to make sure that the values you are getting
from the listbox are the ones you want. If they aren't unique IDs for the
records to be deleted, be aware that any record that matches the selection
will be deleted.
Example:
Dim strSelection As String, varItem As Variant
Dim strSQL As String
For Each varItem in Me.lstMyListbox.ItemsSelected
'This assumes numbers as the values from the listbox
strSelection = strSelection & Me.lstMyListbox.ItemDate(varItem) & ","
'If the values are strings, use the following
'strSelection = strSelection & """" & Me.lstMyListbox.ItemDate(varItem)
& ""","
Next
'Remove the last ","
strSelection = Left(strSelection, Len(strSelection) -1)
strSQL = "DELETE * FROM MyTable WHERE MyTable.FieldName IN (" & strSelection
& ");"
CurrentDb.Execute strSQL, dbFailOnError
Of course, if you have spaces in the table or field names, place brackets
[ ] around them.

Signature
Wayne Morgan
MS Access MVP
> Hi,
> I have a form with a list box that shows the contents of a table and
[quoted text clipped - 13 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
jim Bob - 13 Jan 2006 03:04 GMT
Thanks Wayne,
problem solved.