delete a selected record from a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey,

There is probably a very simple answer to this but i'm damned if i know it!!

I have a name table attached to a drop list. now i have a seperate form to
add names to the list (no problem), but i also need a second form to delete
from the list.

I have created a drop down with the lists of names and i want to be able to
select the appropriate name to be deleted from the table, and then hit a
delete button to delete it. All my attempts have ended in it just deleting
the next record in the table.

thanks for any help,
Myles
 
can i also add that although i try, i'm not amazing with Access n get a bit
bamboozald with code althoguh MAcro's aren't a problem!!

cheers for any help you can give!!

Myles
 
Try first capturing the value from the list and then using DAO (or ADO)
to delete the record from the table.

i.e.:

dim db as database
dim rec as recordset

set db = currentdb
set rec = db.openrecordset("Your Table Name")

do while rec.eof = false
if rec("Your Field Name") = Me!cboYourComboBox then
rec.delete
rec.update
endif
rec.movenext
loop
 
It's almost always better to use a query rather than a recordset:

Dim strSQL As String

strSQL = "DELETE FROM [Your Table Name] WHERE [Your Field Name] = " &
Me!cboYourComboBox

CurrentDb.Execute strSQL, dbFailOnError


Of course, if [Your Field Name] is a text field, that would need to be:

strSQL = "DELETE FROM [Your Table Name] WHERE [Your Field Name] = " & +
Chr$(34) & Me!cboYourComboBox & Chr$(34)
 
Oops, that last line should be, of course

strSQL = "DELETE FROM [Your Table Name] WHERE [Your Field Name] = " & _
Chr$(34) & Me!cboYourComboBox & Chr$(34)


--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Douglas J. Steele said:
It's almost always better to use a query rather than a recordset:

Dim strSQL As String

strSQL = "DELETE FROM [Your Table Name] WHERE [Your Field Name] = " &
Me!cboYourComboBox

CurrentDb.Execute strSQL, dbFailOnError


Of course, if [Your Field Name] is a text field, that would need to be:

strSQL = "DELETE FROM [Your Table Name] WHERE [Your Field Name] = " & +
Chr$(34) & Me!cboYourComboBox & Chr$(34)

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


ManningFan said:
Try first capturing the value from the list and then using DAO (or ADO)
to delete the record from the table.

i.e.:

dim db as database
dim rec as recordset

set db = currentdb
set rec = db.openrecordset("Your Table Name")

do while rec.eof = false
if rec("Your Field Name") = Me!cboYourComboBox then
rec.delete
rec.update
endif
rec.movenext
loop
 
Back
Top