Solved

Form_Delete called 2 times when using DEL key on subform

Posted on 2003-11-12
10
412 Views
Last Modified: 2009-10-31
I am at a complete loss on this one...  I have a subform (continuous), inside a form, and I've got a custom-built Form_Delete section that works well.  The problem is, if the user selects delete (for 1 or multiple records) from the menu, the routine functions normally, and I am asked if I want to delete, or not delete, or cancel the remaining questions for each of the selected records.  If the user presses the DEL key, the process happens 2 times (all the selected items are listed for deletion, and, if the user selects "No, don't delete", once the last of the selected items has been asked, the entire process repeats itself (asking all the selected items that the user selected "No" for again.

Again, it works fine from the menu, but repeats 2 times when pressing the DEL key.
The form is a subform of another form, (when I use the form itself, not as a subform, the DEL key works fine)
I'm using Access 2003, and this feels like a bug, but any help would be greatly appreciated.
0
Comment
Question by:oronk
10 Comments
 
LVL 32

Expert Comment

by:jadedata
ID: 9732956
Hey oronk!

  If you are using the OnDelete Event to Delete a record, you are multiplying its effect.
  This is not a bug.

  Show the code for you Delete related events on the affected form(s) and specify which form the code rides on.

regards
Jack
0
 

Author Comment

by:oronk
ID: 9733077
This is the only event that is called (for either the menu delete, or the DEL key).  As I stated before, if the Menu Delete command is used, this routine is run 1 time for each record that is to be deleted (weather or not the record is deleted)... If the DEL key is pressed (not using the menu), this routine is run 1 time for each record, then another time for any of the records that were not to be deleted.  
You state that I am multiplying the effect... why and how would it multiply when pressing the DEL key, but not when selecting the Delete Records from the menu?

Private Sub Form_Delete(Cancel As Integer)
   Static Test As VbMsgBoxResult
   Static Records As Integer
   
   If Test = vbCancel And Records > 0 Then
      Cancel = True
      Records = Records - 1
   Else
      Dim rst As Recordset
      Dim strSearchName As String
      Set rst = Me.RecordsetClone
      strSearchName = Str(Me!LContactsID)
      rst.FindFirst "LContactsID = " & strSearchName
      If Not rst.NoMatch Then Records = rst.AbsolutePosition
      rst.Close
   
      Records = Me.SelHeight + (Me.SelTop - 1) - Records
      Test = MsgBox("Are you sure you want to delete: " & Chr$(13) & Chr$(10) & _
                    """" & ShortNameText & ": " & NameListingText.Value & """?", vbYesNoCancel, "Delete Contact")
      If Test = vbYes Then
         MsgBox "Deleting Current Record: " & NameListingText.Value & ": " & NameListingText.OldValue
      Else
         Records = Records - 1
         Cancel = True
      End If
   End If
End Sub
0
 

Author Comment

by:oronk
ID: 9733095
the main form does not include a OnDelete event, this resides in the subform.  Also, like I stated, if I run the subform as a stand-alone form, the DEL key only calls the function 1 time per record, not 2 times.
0
Technology Partners: 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 32

Expert Comment

by:jadedata
ID: 9733748
Move that code onto a separate button and out of the form_delete event.  
Delete the target record directly from the recordset it is found in.
Requery the form after a delete.

I bet the problem goes away
0
 

Author Comment

by:oronk
ID: 9734155
jadedata,
Your solution has crossed my mind... but I really want to be able to delete multiple records at the same time (which is why I use the form_delete event).  Using the button, from what I can tell, even if I had multiple selections, when I clicked on the button, only 1 record would be deleted.
Maybe I'm going about this wrong... is there anyway to tell that the DEL key was pressed... I don't think it's in the normal keypress information...  If I could tell from where the delete command came from, I could know what to expect (like duplicate requests to delete)
I would even be willing to use the Form_BeforeDelConfirm function (but I MySQL doesn't do transactions... once Access deletes a record, it can't be undeleted... as far as I can tell, which is why I ask in the Form_Delete routine)
0
 

Author Comment

by:oronk
ID: 9734503
I figured it out... The problem was on the form, I had Allow Deletes enabled, but I had Allow Edits disabled, for some reason, this combination of options caused the Form_Delete to run twice when the form was a subform of another form.
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9736560
well, wasn't that a fun and educational ride!!?
0
 
LVL 39

Expert Comment

by:stevbe
ID: 10025373
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

stevbe
EE Cleanup Volunteer
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 10025468
Make a Flag

Dim DelFlag as Boolean


Formload DelFlag = False

In your Delete Sub

Make the Flag True  and then false and then the Click event only call the Delete Procedure if the flag is true
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 10069645
PAQed, with points refunded (250)

SpazMODic
EE Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

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