Solved

Custom delete of record

Posted on 2000-05-07
7
348 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

679 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