Solved

Form_Delete called 2 times when using DEL key on subform

Posted on 2003-11-12
10
409 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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

17 Experts available now in Live!

Get 1:1 Help Now