Can't delete a record with this slightly modified code: Access 03

G

Guest

Private Sub cmdDeleteServer_Click()
On Error GoTo Err_cmdDeleteServer_Click
If MsgBox("Delete this server?", vbYesNo, "Delete") = vbYes Then
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
End If
Exit_cmdDeleteServer_Click:
Exit Sub
Err_cmdDeleteServer_Click:
MsgBox Err.Description
Resume Exit_cmdDeleteServer_Click
End Sub
 
G

Guest

It's a list box that I'm displaying on a form. Does that have something to
do with it?
 
J

John Spencer

What is a listbox?

Do you want to delete a record based on the value in a listbox on the form?

Does the listbox have a column or columns that specifically identify which
record to delete?

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
G

Guest

If you look on the tool box in form designer you'll see a list box control.
I am trying to delete a record based on a select from that control. It
doesn't seem to matter if I include the primary key, all of the fields, or
anything else.

--
 
J

John Spencer

Sorry, my question wasn't clear. I know what a listbox is. Your code has
NO reference to the listbox.

Guessing that the listbox has the primary key as its bound column then you
would need code to get the correct record and delete it. I would use a
delete query.

Private Sub cmdDeleteServer_Click()
Dim strSQL as String
On Error GoTo Err_cmdDeleteServer_Click

If MsgBox("Delete this server?", vbYesNo, "Delete") = vbYes Then
strSQL = "DELETE * FROM [YourTableNameHere] " & _
" WHERE [NameOfThePrimaryKeyField Here] = " & me.ListBoxName

'If the primary key field is not a number field then you need to change the
line to
' " WHERE [NameOfThePrimaryKeyField Here] = """ & me.ListBoxName & """"

Currentdb().Execute strSQL, dbFailonerror


End If

Exit_cmdDeleteServer_Click:
Exit Sub
Err_cmdDeleteServer_Click:
MsgBox Err.Description
Resume Exit_cmdDeleteServer_Click
End Sub

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top