Instead of the using the auto number in Access (sometimes produces duplicates) I've decided to generate my own numbers.
I am using the data macro Before Change
but I'm not sure on how to run the query SELECT MAX(ID)+1 FROM MyTable
and insert it into the ID field on each Insert
.
I've messed around with the SetField, SetLocalVar, LookUpRecord actions but no luck so far.
EDIT: I've tried using DMAX in the expression as per example: https://www.599cd.com/tips/access/incrementing-your-own-counter/. This works when I add a row manually. However, I add rows from Excel VBA at which point this method stops working, generating the error, the function is not valid for expressions used in data macros
You can only use very limited SQL statements in data macros. You can use queries, though.
Create a query (called QueryA), and enter
SELECT MAX(ID)+1 As Expr1 FROM MyTable
as the SQLThen, you can use a data macro with the following structure:
The AXL is the following:
You shouldn't use VBA functions or domain aggregates such as
DMax
in data macros, nor in the queries data macros are dependent upon. If you do, it can only be triggered from a running Access application, because these are only valid from within Access.Alternatively, you can rewrite your SQL statement to be valid for data macros. This means: no aggregates, no calculations! But you can use ordering to get the maximum value:
The AXL is the following (which makes it easier to understand the limited SQL):
Replaces Access Autonumber LongInt Data Type Using simple Data Macro and one simple VBA function (see images x2)
Data Macro
VBA Code