Solved

Form_Delete called 2 times when using DEL key on subform

Posted on 2003-11-12
10
410 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
 
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now