Delete Records based on DoCmd.RunSQL

  • Thread starter Thread starter davea
  • Start date Start date
D

davea

Hi

I have a command button that when clicked deletes certain records from
a subform based on an application number being equal. It deletes all
records with the same applciation number as currently shown on the
main form, see code below:

DoCmd.RunSQL ("DELETE FROM tblAdultDetails WHERE
[frmAdultDetails2].Form![ApplicationNum]=" +
Str(Me.ApplicationNumber))

However I would like it to only delete records if the fields
[MainApplicantFlag] =1 or [PartnerFlag] =1 as well as ApplicationNum
being equal. These are both number fields btw.

Can someone tell me what I should add to my code in order to do this?
I'm not sure of the syntax.

Thanks
 
Try this:

Dim strSql As String
strSql = "DELETE FROM tblAdultDetails WHERE ((tblAdultDetails.[Field1] = " &
Me.ApplicationNumber & ") AND ((tblAdultDetails.[MainApplicantFlag] =1) OR
(tblAdultDetails.[PartnerFlag] = 1)));
dbEngine(0)(0).Execute strSql, dbFailOnError

Replace "Field1" with the name of the field in the table that matches the
application number.

Using the Execute method, you can know whether the deletion succeeded or
not. For more about that, see:
Action queries: suppressing dialogs, while knowing results
at:
http://allenbrowne.com/ser-60.html
 
Back
Top