DoCmd.RunCommand acCmdDeleteRecord is deleting a record on a different form! (1 Viewer)

bignose2

Registered User.
Local time
Today, 15:36
Joined
May 2, 2010
Messages
219
Hi,

Very strange problem, using the code below, I have a button that delete's the current record, sometimes it works as expected, sometimes does not delete the record but more oddly it deletes a record on a different form, the form is a main menu A, is open but not even the calling form so when this single form C is closed it should revert to B anyway.

A Menu
B List form tabular
C individual single form

Had this form and code for years, never had a problem before, tinkered with it a little but even rolling back to quite old versions the problem still remains.

Does not seem to be a pattern when it fails, usually when I try to delete the records that failed at first, it works the second time.

Almost like a bug, hence importing old version of that form, compact & repair, even decompile.

As I type, thinking I had added a combo box, based on a different table but this was a while ago and had been fine in between but he most significant change.
I will delete & test some more but sort of would like to know why its doing this even if a bug.

Code:
    DoCmd.SetWarnings False  ' stop the cascading message, which also remove the option not to delete but don't really want by product choice anyway here
    DoCmd.RunCommand acCmdDeleteRecord     ' July 19 insetad of above
    DoCmd.SetWarnings True
   
'   MsgBox "deleted"
   
    DoCmd.Close acForm, "Dogs Alter Fm 1"

After closing is should be on for B & that is the one that is visible but if go to the Menu form A, the record has been deleted, its just a single header type record.
 
Local time
Today, 16:36
Joined
Feb 27, 2023
Messages
43
Avoid DoCmd as it depends on actual focus. You can set focus with Docmd.SelectObject, but there are better alternatives.
Ruby:
Me.RecordSet.Delete
is referencing the Form-Instance Recordset explicit!

btw. DoCmd.SetWarnings is used to suppress deletion warning? Better use Application.SetOptions to only disable specific warnings
 

bignose2

Registered User.
Local time
Today, 15:36
Joined
May 2, 2010
Messages
219
Hi,

Thanks for very fast reponse
In all my years never used that method, I will go down that route but would sort of like to know why & if simply a access bug

Below does seem to be a real anomaly

4 out of 5 times, "deleted OK"

1 time, debug.print is correct "SMITH/JOHN"
but Msgbox StillHere is STILL "SMITH/JOHN"

Active control is the Button that calls the Delete, even when it failed.

Code:
    SaveID = Me!ID
    Debug.Print Nz(DLookup("Key", "Dogs", "ID = " & SaveID), "Null"), Screen.ActiveControl.Name
   
    'DoCmd.SetWarnings False 
    DoCmd.RunCommand acCmdDeleteRecord
    'DoCmd.SetWarnings True
   
    StillHere = Nz(DLookup("Key", "Dogs", "ID = " & SaveID), "Deleted OK")
    MsgBox StillHere
 
Local time
Today, 16:36
Joined
Feb 27, 2023
Messages
43
remove DoCmd.SetWarnings False and use DoCmd.SetWarnings True on top of code (as it might be turned off by you) to get an error message
 

bignose2

Registered User.
Local time
Today, 15:36
Joined
May 2, 2010
Messages
219
I had tested, did not get an error message, it was deleting a record OK jut the WRONG on from a different form

I am now using Me.RecordSet.Delete & cannot replicate the problem
I would have liked to know why it somehow the focus goes to this form, even stepping through the code, does not show how it leaps
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:36
Joined
Feb 19, 2002
Messages
43,275
We would have to see the entire database to determine why the focus is not where you think it is. As you mentioned, this "seems" to have run correctly for years. Maybe it hasn't been running correctly and you only just noticed. It is extremely dangerous as has been pointed out to rely on something you don't actually control when doing something as important as deleting a record. In my apps, I never allow more than one form to be visible at one time so, this wouldn't ever happen to me. But this situation is exactly why I always hide formA when it opens formB and I open formB as a dialog so it is absolutely the only form that can obtain the focus.

Perhaps you have a Timer event in a different form that is interfering.
 

Users who are viewing this thread

Top Bottom