This simple VBA statement does not work as expected:
strSQl = "UPDATE Inventory SET NumberOfBlocks = BlocksReserved, LastUser = 'Me' WHERE InventoryID = 1234;"
CurrentDb.Execute strSQl, dbFailOnError + dbSeeChanges
LastUser is updated, but NumberOfBlocks remains unchanged, no error.
If I run this statement in SSMS or as an Access query, it works.
If I use a variable in the VBA statement ..."SET NumberOfBlocks = " & intBlocksReserved & ",..., it works.
Constant works: ..."SET NumberOfBlocks = 555"...
And this one works, too: NumberOfBlocks = (BlocksReserved * 1)
NumberOfBlocks and BlocksReserved are both smallint and not null; the record has a timestamp/rowversion field.
Environment: Access 2016 with SQL 2016 back-end.
Any ideas why my initial statement fails silently? Thanks!
More testing confirms my previous findings:
Created a table in SQL Server:
[ID] [int] IDENTITY(1,1) NOT NULL, [NumberOfBlocks] [smallint] NULL, [BlocksReserved] [smallint] NULL, [LastUser] nvarchar NULL, [RV] [timestamp] NOT NULL
Set ID as Primary Key, linked SQL table, entered test data in both.
Run exactly the same code on both tables (only changed the table name):
Dim strSQl As String
strSQl = "UPDATE Inventory SET NumberOfBlocks = BlocksReserved, LastUser = 'Me';" CurrentDb.Execute strSQl, dbFailOnError + dbSeeChanges
Result:
Local Access table: NumberOfBlocks = BlocksReserved, LastUser = 'Me'
Linked SQL table: NumberOfBlocks unchanged, LastUser = 'Me'
More notes:
However, explicitly converting the field worked:
...SET NumberOfBlocks = CInt(BlocksReserved)...
just as
...SET NumberOfBlocks = (BlocksReserved * 1)...
I guess, that turns my post from a question to a heads-up...
Further tests confirmed that this is a bug in the Execute command under the following conditions:
The same SQL statement will work fine
Tested work-arounds: