Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Form_Delete called 2 times when using DEL key on subform

Posted on 2003-11-12
10
Medium Priority
?
416 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
[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
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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