Solved

Custom delete of record

Posted on 2000-05-07
7
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

724 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