Delete records/rows in subform (1 Viewer)

Wysy

Registered User.
Local time
Today, 14:51
Joined
Jul 5, 2015
Messages
291
Hi,
I want to delete a record in a subform by clicking a command button on the mainform.
I made a public function in subform
Perl:
Public function DelRec()
    docmd.runcommand accmdDeleteRecords
end function
Then onClick of the button
Code:
Public sub CMD1_Click()
    Forms!frmMain.fsub.form.DelRec
end sub

but it does not work. Any idea?
thanks
 

Wysy

Registered User.
Local time
Today, 14:51
Joined
Jul 5, 2015
Messages
291
seems like syntax error
accmdDeleteRecord.....
sorry about that, it works now without the 's'
 

Ranman256

Well-known member
Local time
Today, 17:51
Joined
Apr 9, 2015
Messages
4,102
cant you just click the DELETE button on the keyboard?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:51
Joined
Feb 19, 2002
Messages
36,352
What you are doing is extremely dangerous. It's like playing pin the tail on the donkey. Do you KNOW with absolute certainty which record in the subform is the Current record because that is the record that will be deleted using this method.

A safer option is to add the button to the subform so the user knows with absolute certainty which record will be deleted.

A different option that lets you keep the button on the main form (which I do not like) is to create a hidden textbox on the main form. Then in the Current event of the subform copy the ID of the subform record to that control.

Me.Parent.txtHoldSubformID = Me.SomePKID

Then in the button code, run a delete query that deletes that specific record.

Delete * From YourTable Where SomeID = Forms!yourparentform!txtHoldSubformID

Then requery the subform otherwise you'll see #deleted# in all the columns of the record that was deleted.
 

Wysy

Registered User.
Local time
Today, 14:51
Joined
Jul 5, 2015
Messages
291
Thank you for the detailed input. I am going to build in a message box with conformation message with default cancel button.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:51
Joined
Feb 19, 2002
Messages
36,352
That isn't going to be any safer. Users blow by messages all the time. If the user can't see the record he's deleting (the current record is not always visible), How will he be able to confirm? YOU have to ensure that the record being deleted is the record that is selected. I offered two suggestions. There are probably other options.
 

Wysy

Registered User.
Local time
Today, 14:51
Joined
Jul 5, 2015
Messages
291
I am the only one using that DB. But i am testing your solutions. Thank you so much!
 

Users who are viewing this thread

Top Bottom