Solved

Use VBA to test a record, before deleting it and then abort the delete instruction.

Posted on 2014-01-16
4
793 Views
Last Modified: 2014-01-16
When selecting a record and pressing the delete button, I want the system to do a test on the record and if positive, I want to advise the user of the condition and why he may not delete the record. Then I want the system to abort the delete instruction.

I envisage that that will be done by VBA code in the On Delete action of the applicable form.
Will you help please.
0
Comment
Question by:Fritz Paul
  • 2
  • 2
4 Comments
 
LVL 84
ID: 39785249
What sort of conditions?

If you want to check if a value exists in the record, for example:

If Me!SomeField = 1 Then
  If Msgbox("The value of SomeField is " & Me!SomeField & " Do you want to remove this record?", vbYesNo) = vbYes Then
    DoCmd.RunCommand acCmdDeleteRecord
  End If
End If

Or do you need to check one or more values in the current record against other values in other tables?
0
 

Author Comment

by:Fritz Paul
ID: 39785327
Say form record selectors are set to yes. Then user clicks on record selector and on keyboard presses delete. Then

If Me!SomeField = 1 Then
   Msgbox("The value of SomeField is " & Me!SomeField & " and the record may therefore not be deleted.")

Cancel the delete action   'This is the VBA I am looking for please.

End If
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39785368
You can use the Form's Delete method to handle this:

Private Sub Form_Delete(Cancel As Integer)
    If Me!SomeField = 1 Then
           Msgbox("The value of SomeField is " & Me!SomeField & " and the record may therefore not be deleted.")
        Cancel = True
    End If
End Sub

Open in new window

Setting "Cancel = True" stops the process from continuing.
0
 

Author Closing Comment

by:Fritz Paul
ID: 39785376
Thanks a lot. It works.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

708 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

18 Experts available now in Live!

Get 1:1 Help Now