Solved

Custom delete of record

Posted on 2000-05-07
7
344 Views
Last Modified: 2012-06-27
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?

0
Comment
Question by:BozzoCage
  • 5
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
paasky earned 100 total points
ID: 2785547
Hello BozzoCage,

Cancel restores the original record values. This is from Access help:

Cancel:      The setting determines if the BeforeDelConfirm event occurs. Setting the Cancel argument to True cancels the BeforeDelConfirm event and prevents the Delete Confirm dialog box from being displayed. If the event is canceled, the original records are restored, but the AfterDelConfirm event still occurs. If Cancel is set to True, the Response argument is ignored.
If Cancel is set to False (0), which it is by default, the value in the Response argument is used by Microsoft Access to determine the type of response to the delete event.

I'm quite curious what's your idea - here's anyway my suggestion how to make this work like you want:

Private Sub Form_AfterDelConfirm(Status As Integer)
    If MsgBox("Delete?", vbQuestion + vbYesNo) = vbYes And acDeleteCancel Then
        Me!ImportantField = Null
    End If
End Sub

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
    Cancel = 1
End Sub

Regards,
paasky
0
 
LVL 10

Expert Comment

by:paasky
ID: 2785555
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
0
 
LVL 2

Author Comment

by:BozzoCage
ID: 2785688
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.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:paasky
ID: 2786047
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
0
 
LVL 2

Author Comment

by:BozzoCage
ID: 2786235
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!
0
 
LVL 10

Expert Comment

by:paasky
ID: 2786291
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)
0
 
LVL 10

Expert Comment

by:paasky
ID: 2786295
(above code is from mainform's "Delete" button)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now