Did a search...still need help

  • Thread starter Thread starter Angi
  • Start date Start date
A

Angi

Before everyone attacks me, I did do a search on this topic and found
thousands of postings. I read a couple hundred without finding my
answer, so I'm taking the chance and asking.

<meekly> Why does my form (cts form view) cmdDelRec button work when
opened alone but not as a subform??

Private Sub cmdDelRec_Click()

If MsgBox("Are you sure you want to delete this contact?",
vbOKCancel + vbCritical, "WARNING!") = vbOK Then
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
Me.Requery
DoCmd.SetWarnings True
End If

End Sub

TIA!
 
i've also used this:

Me.ContactSubForm.Form.RecordsetClone.delete
Me.ContactSubForm.Requery
 
It may be that your subform recordset isnt updateable when it is a subform
either because of the settings of the subform or the relationship that you
are linking mainform/subform is many to many. Idealy you should have a one
to many relationship defined for any mainform/subform relationship in tools
relationship if you follow.
rgds
Stephen
 
Angi said:
Before everyone attacks me, I did do a search on this topic and found
thousands of postings. I read a couple hundred without finding my
answer, so I'm taking the chance and asking.

<meekly> Why does my form (cts form view) cmdDelRec button work when
opened alone but not as a subform??

Private Sub cmdDelRec_Click()

If MsgBox("Are you sure you want to delete this contact?",
vbOKCancel + vbCritical, "WARNING!") = vbOK Then
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
Me.Requery
DoCmd.SetWarnings True
End If

End Sub

You have to select the record so DoCmd can figure out which
record you want to delete. I suspect the button is not in
the detail section so clicking it doesn't put the focus on a
record.

This kind of issue is the primary reason that I avoid using
DoCmd to operate on records (or some objects). It's just
never clear what DoCmd is supposed to operate on.

See your followup post for my preferred approach.
 
Angi said:
i've also used this:

Me.ContactSubForm.Form.RecordsetClone.delete
Me.ContactSubForm.Requery


You still have to make sure you're going to delete the
correct record. In this case, you do not know (or didn't
explain) which record is the current record in the clone
recordset (it's probably not the same record as the form's
current record).

If Me.RecordsetClone.RecordCount > 0 Then
Me.RecordsetClone.Bookmark = Me.Bookmark
Me.RecordsetClone.Delete
Me.Requery
End If
 
Thanks guys for the replies!

The delete button is in the detail section of the subform. That was
the only way it was working with cts forms. The relationship is a
1-many.

Marsh: Your code works but I don't understand the difference between
what I had and yours since the btn is in the detail section.

Thanks all!!
 
Angi said:
The delete button is in the detail section of the subform. That was
the only way it was working with cts forms. The relationship is a
1-many.

Marsh: Your code works but I don't understand the difference between
what I had and yours since the btn is in the detail section.


The trick is to make sure that you're trying to delete the
appropriate record. Syncing the current record in the
form's recordset clone to the form's current record using
Bookmarks is guaranteed to work.

The DoCmd method you tried to use should at least include a
DoCmd.RunCommand acCmdSelectRecord in an attempt to identify
the record to delete. Even with that, it's not 100%
guaranteed because you can not specify which form/record to
select. Even if you get that right, there is a (small?)
chance the focus could shift to a different form/record
between the time you select the record and the time the
acCmdDeleteRecord action executes. The DoCmd methods
(especially the RunCommand actions) are just simulating a
user mouse click on a menu bar item and not appropriate in
VBA procedures. Just try to stay away from DoCmd actions
that do not allow you to explicitly specify the object of
the action (an example that does is DoCmd.Close acForm,
"formname", but even then a frm.Close would be the "right"
method if the object model allowed it).
 
Marsh,
Thank you for taking the time to explain it to me. It was a pleasant
surprise!

Best regards,
Ang
 
Back
Top