Forum Discussion
AndyBentley-7653
Aug 25, 2022Copper Contributor
Deleting records from a form in datasheet view when a sort has been applied to a combo-box.
I believe that I have found a bug in Access. I am currently running Microsoft® Access® for Microsoft 365 MSO (Version 2207 Build 16.0.15427.20182) 64-bit . To reproduce this bug do the following: ...
arnel_gp
Aug 28, 2022Steel Contributor
it's a Dangerous approach on having 2 tables with Same PK values.
Access will not distinguish which PK value belongs to which table.
therefore it will delete both.
also this is not a Normal table design.
there can only be PK and the Other field on the other table should be made FK.
there is no such things as "1=1" like this one, since you can just have 1 table
in this situation.
Access will not distinguish which PK value belongs to which table.
therefore it will delete both.
also this is not a Normal table design.
there can only be PK and the Other field on the other table should be made FK.
there is no such things as "1=1" like this one, since you can just have 1 table
in this situation.
George_Hepworth
Aug 28, 2022Silver Contributor
"Access will not distinguish which PK value belongs to which table.
therefore it will delete both."
I don't think that's entirely correct, but it does express the problem correctly, I believe.
In an Access query, this is acceptable syntax and it will delete records ONLY from table2, as required.
DELETE Table2.*
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2;
In an Access query, this is not acceptable syntax for a delete query and won't execute, precisely because it doesn't specify which table to delete the records from.
DELETE *
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2;
I am sure that Ben's explanation is correct with regard to the implicit join created by sorting the values in the combo box. That results in a query that does include both tables (i.e. the recordsource for the form, table2, and the rowsource for the combobox, table1) and therefore deletes all records in that query.
The result must be something like this, although it's not materialized explicitly. Remember sorting the values forces Access to bring in the rowsource (table1) to support that sort
DELETE Table1.* ,Table2.*
FROM Table1 INNER JOIN Table2 ON Table1.ID1 = Table2.ID2;
And that query will delete all records from both tables because they are now specified (incorrectly, no doubt) as the form's recordsource.
To me this does qualify as a bug in the way the form's built-in Delete function operates.
Also, I wholeheartedly agree the table design here is not appropriate and is itself part of the problem. In fact, it may well be that this particular flaw hasn't been commonly encountered is precisely because the join on two primary key fields is so unusual.
- isladogsAug 29, 2022MVP
I agree that the database design isn't appropriate & that this isn't (or at least shouldn't be) an approach that is used in a real life situation
However, I disagree with arnel_gp about this 'cascade delete' being normal behaviour
Deleting records from one table should have no effect on the records from another table UNLESS a relationship has been set with referential integrity and cascade delete. See my article: Relationships1 (isladogs.co.uk)
As the OP stated, deleting records via the form does NOT normally delete records from the other table. That is correct behaviour as there is no relationship between the tables.
Applying a sort causes the records in the unrelated table to also be deleted as he reported.
That is definitely unwanted behaviour and is reasonable to consider it as a bug.
More accurately it is an unwanted side effect that isn't normally an issue in most circumstances
It should be noted that there is still no relationship between the tables
Deleting records from Table2 directly does not affect Table1
The problem is the combo on the form which has a row source to Table1 and the Inherit Value List property is set to Yes (default value). Change it to No as in the screenshot.
Now sort the list and delete records in the form. The records in Table1 are no longer deleted
A similar issue is observed in my Combobox Zombies Puzzle (isladogs.co.uk) example app
Hope that helps
- arnel_gpAug 31, 2022Steel Contributoryou can disagree if you like.
if you build a query joing the two table on their PK fields.
and you delete from 1 table, what do you think will happen.
records from both tables will get deleted! Why?
Access will not distinguished which Table you want to delete from since the 2 tables
have exactly same Primary Key.
You can confirm it by making same table as with the OP and linking Table2 to table1 to show the Desc field.- isladogsAug 31, 2022MVP
You are describing a completely different scenario to the issue outlined in this thread.
In the OP's 'simplified database', there was no link between the 2 tables in the form used for demonstration purposes
What I described was correct. Deleting a record from the combo should have no effect on the other table. The fact that it does is a bug. The workround of setting the combo InheritValueList property to No prevents that unwanted behaviour.
Next I'll discuss your exact scenario.
Create a table and add several records. Clone the table & join by the PK fields as you described.
a) Create a delete query to delete a record from one of the tables. The other table isn't affected UNLESS you do all the following: create a relationship, set referential integrity & set Cascade delete related records to True.
In other words, Access ensures data in related tables isn't deleted unless you specifically tell it to do so
b) Create a select query between the 2 tables and display all the fields from one table. Now manually delete a record from that table. In that specific scenario, Access will delate the corresponding record from the other table whether or not there is a relationship between the tables. That is expected behaviour and not a bug.
That is one of the reasons why end users should NEVER be allowed direct access to queries. Nor should deleting records manually be allowed in forms where tables are joined in the manner described
Hope that clarifies matters
- George_HepworthAug 29, 2022Silver Contributor
Thanks, Colin.
I had used Jet Show Plan, as Mike Wolfe suggested, without spotting anything useful yet.
Too many irons in the fire to spend a lot of time on this one.
With regard to Inherit Value List, this statement is in the documentation, emphasis added:"The InheritValueList property is effective only when the combo box is bound to a Lookup field and the RowSourceType property is set to Value List."
The fact that is impacts this Lookup using a table does suggest a bug, since it should only effect a value list. That confuses me, anyway.
Nonetheless, I think Ben's hypothesis is on the right track. Because the form is implicitly joining the two recordsets to accomplish the sort, the delete then impacts both. But, knowing that the InheritValueList property avoids the bug is very helpful.
- isladogsAug 29, 2022MVPThat's a good point regarding Inherit Value List.
As you say, it SHOULD only affect Value List row sources so it shouldn't apply in this example.
The fact that it does so, confirms it is a bug....though a somewhat obscure bug due to the unusual usage.