Link to home
Start Free TrialLog in
Avatar of BozzoCage
BozzoCage

asked on

Custom delete of record

I want to change one field of record instead of deleting whole record with standard delete action. My current solution:

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
    If MsgBox("Delete?", vbQuestion + vbYesNo) = vbYes Then
        Cancel = True
        ImportantField.Value = Null
    Else
        Cancel = True
    End If
End Sub

ImportantField is name of field on that form that is criteria in query. My delete should only set this field to Null instead of deleting whole record - record would disappear from shown datasheet, since it would no longer match my criteria (..WHERE ImportantField Is Not Null)

Previously written code sets field to Null, but when event completes (BeforeDelConfirm), it gets back old value - it discards my changes. How to prevent that?

ASKER CERTIFIED SOLUTION
Avatar of paasky
paasky
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
AfterDelConfirm event is triggered even in BeforeDelConfirm Cancel parameter is set True -> Status indicates in AfterDelConfirm, that Cancel has been set true in VBA.

Here are possible status codes in AfterDelConfirm event:

acDeleteOK      
Indicates the deletion was successful.

acDeleteCancel      
Indicates the deletion was canceled in Visual Basic. (we catch this!)

acDeleteUserCancel      
Indicates the deletion was canceled by the user.

Hope this helps,
Paasky
Avatar of BozzoCage
BozzoCage

ASKER

paasky, thanks!
It works as I wanted.

My idea:
Let's say I'm managing subscriptions and when I unsubscibe someone, I don't want to erase users data (name,...). Because there is only one property that says someone is subscribed (date), I put it in same table with all other users data. I don't need another table that would say who is subscribed, since that one would be related to main table only 1:1. It would be easier to unsubscribe users, but I think current way is better.
I have subform with list of subscribed users, from where I simply delete someone to unsubscribe it.

Glad I could help you BozzoCage.

Interesting approach ...
and the reason why you're doing importantfield-nulling in OnBef./Aft.Delete event is that built-in toolbar or menu is used for unsubscribing the user, not button where you could just set null to "importantfield", right?

' unsubscribe user with button
Private Sub B_Delete_Click()
    If MsgBox("Delete?", vbQuestion + vbYesNo) = vbYes then Me!ImportantField = Null
End Sub

Regards,
Paasky
paasky, Hello!

I have main form with some common propertis with a subform, in which I have list (with some details) of subscribers, matching some criteria. Main form has button that opens new form for adding (actually selecting from existing users) subscribers. Changes (additions) are immediately shown in subform. To delete (unsubscribe) them, I "have to" use standard method (delete key on selected row).
It would be great if I could put some buttons to main form (Delete, Properties of person...), but I didn't found a way to access subforms data from main form - to access single records. Subform is shown as datasheet, so I cannot put those buttons onto subform.
But that would be for another question - and my time is ticking away...

Thanks again!
Just set focus to your subform and use DoCmd.Runcommand statement to delete record:

Private Sub B_DeleteRecord_Click()

    ' this prevents error messages
    On Error Resume Next

    ' set focus to subform
    Me.Subform1.SetFocus

    ' delete active record
    DoCmd.RunCommand acCmdDeleteRecord
End Sub

:o)
(above code is from mainform's "Delete" button)