I use the ‘updateable query’ feature of ADO, being able to select a dataset from several tables and criteria and display it in a grid or some other UI for the user to browse and edit.
However, and I’m surprised I’ve not hit this problem before now, when one of the joined tables doesn’t have a record for the (master) key, and the user tries to edit a field in that table, ADO gives the famous “Row cannot be located..” error on post.
As far as I understand this error, the ADO driver is trying to locate the record in order to update its fields – and, of course, in this instance, there is no record to find. What I was expecting in these circumstances would be that the ADO driver would issue the equivalent of an UPDATE query for the main table, but an INSERT query for the subsidiary table.
Has anyone else come across this problem and found a workround?
The ADO driver used is the Jet 4.0 OLE DB Provider connecting to an Access (mdb) database.
I have ensured that the primary keys fields for both tables are available in the query dataset for the driver to utilise.
Here is a basic version of the SQL I have using:
SELECT
Table1.CustomerNo, Table1.Field1, Table1.Fieldn,
Table2.CustomerNo, Table2.Field1, Table2.Fieldn
FROM
Table1
LEFT JOIN Table2
ON Table1.CustomerNo = Table2.CustomerNo
WHERE
Table1.CustomerNo = Newcode;
As an experiment, I tried the same thing in MS Access 2007, and that worked, so there maybe a solution within ADO (but then Access is probably using a different driver).
In the end, I went for the ‘hack’ solution. The updateable ADO query over 2 tables would only work if
the customer’s reocrds existed in both tables. So, I had to check that, and if the relevant record was missing from table2, had to insert it first, before calling the main query. Here is the code:
It’s not efficient, and also creates records (in Table2) that may not be needed. But it seems the only solution.