[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Custom delete of record

Posted on 2000-05-07
7
Medium Priority
?
360 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 400 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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, when working with VBA, learn some techniques for writing readable and easily maintained code.

656 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