Delete selected records from a table

branston

Registered User.
Local time
Today, 04:22
Joined
Apr 29, 2009
Messages
372
I have a table where 1 of the columns (ColumnA) is selected as a, b, c or d.
I want to have a button on a form which deletes all of the records where ColumnA is set to b.
Im not completely sure how to do this, but I was thinking of putting the code behind the button as something like

DELETE * FROM NameOfTable WHERE ColumnA = 'b'

Would that work?

Is there a better way of doing it?

Thank you in advance
 
That would work, just you have to tell access that it is SQL that it needs to execute...
Currentdb.Execute "Delete bla bla"
Docmd.runsql "Delete bla bla"

Those will both do it with a small difference that Execute will not show the "are you sure" boxes while runSQL will...

Note:
You can also use variables
mySQL = "Delete bla bla"
Currentdb.Execute mySQL

Note 2:
If you use variables and big SQL statements I find it best to do so like this:
mySQL = ""
mySQL = mySQL & " Delete * "
mySQL = mySQL & " from something "
mySQL = mySQL & " where bla "
Currentdb.execute mySQL

This keeps the SQL more readable and maintable, also note the spaces at the start and end of each line to make sure you dont get "from somethingwhere" which will obviously generate an error.

Hope that helps
 
Great, thank you. Lots of useful bits in there. (Esp the difference between execute & runsql)
Just tried it out and it works great. Thank you!
 

Users who are viewing this thread

Back
Top Bottom