Deleting Records in subform

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

Guest

I am trying to create a button on a form which deletes the current record but
ALSO the records linked to it the froms subform. If some one could suggest
some code to do this that would be great.

TIA

Rico
 
Try this code

' run a delete query to delete the related records from the subform
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName =" & Me.[KeyFieldThatJoinBothTables]

' Delete current record on main form
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
=============================================
If the key field is text field then change the delete sql to
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName ='" & Me.[KeyFieldThatJoinBothTables] & "'"
 
Tankyou Ofer, worked perfectly!

Few related questions. D'you know if its possible to suppress the "are you
sure you want to delete records" messages.

Also if you repsond no to the yes/no prompts, i get a 'Menu item action
cancelled' message, even with error handling code.

Any ideas on how to suppress both of these?

TIA

Rico

Ofer said:
Try this code

' run a delete query to delete the related records from the subform
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName =" & Me.[KeyFieldThatJoinBothTables]

' Delete current record on main form
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
=============================================
If the key field is text field then change the delete sql to
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName ='" & Me.[KeyFieldThatJoinBothTables] & "'"

--
I hope that helped
Good luck


rico said:
I am trying to create a button on a form which deletes the current record but
ALSO the records linked to it the froms subform. If some one could suggest
some code to do this that would be great.

TIA

Rico
 
Use the setwarnings to false

Docmd.SetWarnings False
Run query here
Docmd.SetWanings True

' Don't forget to set it back to true
--
I hope that helped
Good luck


rico said:
Tankyou Ofer, worked perfectly!

Few related questions. D'you know if its possible to suppress the "are you
sure you want to delete records" messages.

Also if you repsond no to the yes/no prompts, i get a 'Menu item action
cancelled' message, even with error handling code.

Any ideas on how to suppress both of these?

TIA

Rico

Ofer said:
Try this code

' run a delete query to delete the related records from the subform
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName =" & Me.[KeyFieldThatJoinBothTables]

' Delete current record on main form
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
=============================================
If the key field is text field then change the delete sql to
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName ='" & Me.[KeyFieldThatJoinBothTables] & "'"

--
I hope that helped
Good luck


rico said:
I am trying to create a button on a form which deletes the current record but
ALSO the records linked to it the froms subform. If some one could suggest
some code to do this that would be great.

TIA

Rico
 
thanks for all your help, all works fine.

Ofer said:
Use the setwarnings to false

Docmd.SetWarnings False
Run query here
Docmd.SetWanings True

' Don't forget to set it back to true
--
I hope that helped
Good luck


rico said:
Tankyou Ofer, worked perfectly!

Few related questions. D'you know if its possible to suppress the "are you
sure you want to delete records" messages.

Also if you repsond no to the yes/no prompts, i get a 'Menu item action
cancelled' message, even with error handling code.

Any ideas on how to suppress both of these?

TIA

Rico

Ofer said:
Try this code

' run a delete query to delete the related records from the subform
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName =" & Me.[KeyFieldThatJoinBothTables]

' Delete current record on main form
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
=============================================
If the key field is text field then change the delete sql to
DoCmd.RunSQL "DELETE TableName.* FROM TableName Where
TableName.FieldName ='" & Me.[KeyFieldThatJoinBothTables] & "'"

--
I hope that helped
Good luck


:

I am trying to create a button on a form which deletes the current record but
ALSO the records linked to it the froms subform. If some one could suggest
some code to do this that would be great.

TIA

Rico
 

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

Back
Top