When entering a new product i
It is important to understand the problem. This is a sequence number that needs to be generated in an append query which is different from the examples you all posted which work for select queries or in the BeforeUpdate event of a form. Please Sameth, correct me if I am wrong. Are you looking for a way to do this in an append query??????? Because if you don't care if the sequence number changes when records are deleted, then just generate the sequence number on the fly in the select query.
The entire problem with generating the sequence number revolves around the request to do it in an append query. Doing it in a select query or in a form where you add a new record are both trivial. I asked if the BE was SQL Server because T-SQL has a feature - Row-Number which can probably do this but that doesn't help if the BE is Jet or ACE since they do NOT have that functionality.
I know that Query can do it easily but don't know how.
It cannot be done in a query. Not sure why you think this. Here's an example of how it would work...
You can create a sequence number in a query for EXISTING records if there is a UNIQUEID on which to base it. You can do it with a join that counts the number of rows in the table with an ID less than the current one. It is more complicated with a three column uniqueID but you can still do it.
The problem with "new" records is you can't use the join technique, you need to use the dMax() technique which is what we use in forms when we are adding new records. That doesn't work because the "previous" records are not actually saved until the end of the query so the dMax() always returns the same value. Remember Action queries are encapsulated in a transaction and the transaction is committed in its entirety or not as the last action.
So, if you use in an APPEND query:
Nz(dmax("SeqNum", "YourTable", "YourYear = YourYear and YourMonth = YourMonth"), 0) +1 As Updated Seq
You will generate the same sequence number for all rows that match year and month. So if the max existing seq is 395 and you add 10 new records to that group, all will get the number 396 10 times because the dMax() operates against the SAVED data, not the pending data. Using a function may solve the problem if you can keep a static counter but I wasn't successful.
Here is a sample which you can use to generate exactly the sequence number you want EXCEPT, it works in a form, one record at a time.
I asked if you were converting existing data because that you would do in a DAO code loop using the same technique as the form except that the sequence number is based on an internal counter rather than a dMax()