Home > database >  Why can't Access update table?
Why can't Access update table?

Time:12-03

Access front end, SQL Server back end.

Simple update query

PARAMETERS ParamTransactionID Long, ParamVoidFlag Short;
UPDATE tblTransaction 
SET tblTransaction.VoidInProgressFlag = [ParamVoidFlag]
WHERE (tblTransaction.TransactionID=[ParamTransactionID]);

using the query here

Set qdef = CurrentDb.QueryDefs("qUPD-tblTransaction_VoidInProgress")
qdef.Parameters![ParamVoidFlag] = VoidFlag
qdef.Parameters![ParamTransactionID] = TransactionID

qdef.Execute dbFailOnError   dbSeeChanges

qdef.Close

gives

[Microsoft][ODBC SQL Server Driver]Query timeout expired
ODBC--update on a linked table 'tblTransaction' failed.

Editing the table directly works.
Opening the query and giving parameters works.
From the app still doesn't.

UPDATE
Deleted the view, no affect.
The old version is now getting the same failure, so it seems like it is not a code issue.
The only thing in common is the table, so it might be a minor change I made there.
I'll check and see if it is just that table or the entire database.
But seems odd that I can make the change by running the query directly, but get different results running it from code.

UPDATE 2 I thought that perhaps it was something to do with the entire database being read-only somehow, and this is just the first place it is getting hit. But no. Other forms can update their tables with no issues.
So it looks related to the specific table. But it still seems odd that I can update perfectly fine by just running the Update Query.

UPDATE 3 To make testing easier, I am running the queryfrom the main menu form, instead of going through all the forms to get to the point where it fails.
Running against the original DB schema worked. Made the same changes again, replacing NTEXT with VARCHAR(MAX), and it still works.
Back to the original table, still works.
Go back through all the forms, fails.
So the problem seems to be related to one of the forms that is open.
I'll go back through that sequence again.
Also, this explains why it works from the query and not from the form.
Sadly, I can't get to the query to run that while the form is open.

CodePudding user response:

Ok, the first question is why/how did you wind up with "Short" data type for the parameter?

It should be:

PARAMETERS ParamTransactionID Long, ParamVoidFlag Bit;
UPDATE tblTransaction 
SET tblTransaction.VoidInProgressFlag = [ParamVoidFlag]
WHERE (tblTransaction.TransactionID=[ParamTransactionID]);

You also state that this query works when you run it from the UI.

So, in code, then you have this:

Make sure that ALL code modules have option Explicit at the start like this:

Option Compare Database
Option Explicit

So, your code should now be:

Dim qdef      as DAO.QueryDef

Set qdef = CurrentDb.QueryDefs("qUPD-tblTransaction_VoidInProgress")
qdef.Parameters![ParamVoidFlag] = VoidFlag
qdef.Parameters![ParamTransactionID] = TransactionID

qdef.Execute dbFailOnError   dbSeeChanges

So correct the data type you have for ParmaVoidFlag.

Also, check the table name in the left nave pane - is it

dbo_tblTransaction

or

tblTransaction.

Also, OPEN UP the linked table in design view - (ignore read only message). Look at the data types. You MUST have a PK defined - so check for a PK.

Next up, on sql server side, true/false fields MUST NOT have a default of null. If they do then updates will fail. So in sql server, make sure the table in question has a default of 0 set.

Last but not least? If the query still errors out, then you need to add a timestamp (not date time) to the sql server table and re-link. This will/is required if any columns are floating point.

After you add the option explicit to the start of the code module, then also from menu do a debug->compile - make sure code compiles.

CodePudding user response:

It turned out that the query failed if a particular form was open. That form queried the same table, but with Snapshot instead of Dynaset. I don't know why that locks the table. It has a proper key and index. But Dynaset fixes it.

  • Related