Command acCmdDeleteRecord on Subform

I

If

Good evening,

I have a form with a subform.
I want to create a button that can delete a highlighted record within the
subform.
I found the code below but it gives me the message : 'The command or action
"DeleteRecord" isn't available now."
How can I removed a recording in this way?

--------------------------------------------
Private Sub cmdDelete_Click()
On Error GoTo Err_cmdDelete_Click

DoCmd.SetWarnings False
If MsgBox("Confirm deletion of the record?", _
vbQuestion + vbYesNo + vbDefaultButton2, _
"Delete?") = vbYes Then
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
End If
Exit_cmdDelete_Click:
DoCmd.SetWarnings True
Exit Sub
Err_cmdDelete_Click:
MsgBox Err.Description
Resume Exit_cmdDelete_Click
End Sub
--------------------------------------------
 
D

Dirk Goldgar

If said:
Good evening,

I have a form with a subform.
I want to create a button that can delete a highlighted record within
the subform.
I found the code below but it gives me the message : 'The command or
action "DeleteRecord" isn't available now."
How can I removed a recording in this way?

--------------------------------------------
Private Sub cmdDelete_Click()
On Error GoTo Err_cmdDelete_Click

DoCmd.SetWarnings False
If MsgBox("Confirm deletion of the record?", _
vbQuestion + vbYesNo + vbDefaultButton2, _
"Delete?") = vbYes Then
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
End If
Exit_cmdDelete_Click:
DoCmd.SetWarnings True
Exit Sub
Err_cmdDelete_Click:
MsgBox Err.Description
Resume Exit_cmdDelete_Click
End Sub
--------------------------------------------

Where is this command button located, on the subform or on the main
form? If it's on the main form, you'll have to set the focus to the
subform first. I don't think you need to explicitly select the record
if you're going to use the acCmdDeleteRecord command, so your logic (if
the command button is on the main form) would be

Me!NameOfYourSubform.SetFocus
DoCmd.RunCommand acCmdDeleteRecord
 
I

If

Oh sorry
My subform is one ListZone


Dirk Goldgar said:
Where is this command button located, on the subform or on the main
form? If it's on the main form, you'll have to set the focus to the
subform first. I don't think you need to explicitly select the record
if you're going to use the acCmdDeleteRecord command, so your logic (if
the command button is on the main form) would be

Me!NameOfYourSubform.SetFocus
DoCmd.RunCommand acCmdDeleteRecord

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
I

If

Oh sorry
My subform is one ListZone


Dirk Goldgar said:
Where is this command button located, on the subform or on the main
form? If it's on the main form, you'll have to set the focus to the
subform first. I don't think you need to explicitly select the record
if you're going to use the acCmdDeleteRecord command, so your logic (if
the command button is on the main form) would be

Me!NameOfYourSubform.SetFocus
DoCmd.RunCommand acCmdDeleteRecord

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
I

If

Oh sorry
My subform is one ListZone


Dirk Goldgar said:
Where is this command button located, on the subform or on the main
form? If it's on the main form, you'll have to set the focus to the
subform first. I don't think you need to explicitly select the record
if you're going to use the acCmdDeleteRecord command, so your logic (if
the command button is on the main form) would be

Me!NameOfYourSubform.SetFocus
DoCmd.RunCommand acCmdDeleteRecord

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

If said:
Oh sorry
My subform is one ListZone

That doesn't really answer my question, I'm afraid. But if your command
button "cmdDelete" is on the main form, and if your subform is named
"ListZone", and if that is actually the name of the subform control on
the main form (and not just the name of the form that is the subform's
Source Object), then I'd expect this event procedure to work:

'------ start of code ------
Private Sub cmdDelete_Click()

On Error GoTo Err_cmdDelete_Click

DoCmd.SetWarnings False

If MsgBox( _
"Confirm deletion of the record?", _
vbQuestion + vbYesNo + vbDefaultButton2, _
"Delete?") _
= vbYes _
Then
Me!ListZone.SetFocus
DoCmd.RunCommand acCmdDeleteRecord
End If

Exit_cmdDelete_Click:
DoCmd.SetWarnings True
Exit Sub

Err_cmdDelete_Click:
MsgBox Err.Description
Resume Exit_cmdDelete_Click

End Sub
'------ end of code ------

On the other hand, if the command button is on the subform itself, I
don't see why your original code wouldn't have worked.
 
I

If

Dirk,
Sorry for my too fast answer.

I wanted to say this:
My Subform is a "list box" control.

Is this possible in this case?
 
D

Dirk Goldgar

If said:
Dirk,
Sorry for my too fast answer.

I wanted to say this:
My Subform is a "list box" control.

Is this possible in this case?

We may be having language problems. A subform cannot be a list box;
they are completely different controls. Do you mean that your subform
contains a list box? Or do you perhaps mean that you are using a list
box to show related records as if it were a subform?

You cannot delete records directly from a list box. You can use code to
delete them from the list box's rowsource table, or to modify them (in
the rowsource table) so that they are no longer included when you
requery the list box.

If you'll explain in more detail, and more accurately, exactly how you
have things set up and what you want to do, we can probably figure out
how to do it.
 

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

Top