[WD 15] Find out the next Automatic ID

Startbeitrag von RAUL2 am 19.04.2013 01:10

Hello guys. I can't find how to do this.

I have a PRODUCTS file with an auto id PK field. Most of the products have their own barcode (EAN 13), but a minority of them, haven't. In this 2nd case I want to generate a barcode with some numeric information and I want the last 6 positions to be the same value as the Auto Id field. e.g.
Auto ID: 132
Generated barcode: 999001000132

Because I need to generate the barcode before inserting the new record, I'd like to know which will be the next Auto Id (I don't remember now how, but, in Sql Server it was possible.) I could just look for the greater value for the ID field + 1 and use it. But, if someone have deleted that last record this won't work. Any suggestion??

There's a way, but I'll have to get rid of the auto id feature and I want to use it (less lines of code of course.)

Best regards.


Hello Raul2

Could you use HLockFIle() to lock the file and then run your HReadLast() on the autoid key to get the last record, increment it, generate your barcode, write the record and then HUnlockFile()


von Al - am 19.04.2013 01:37
Hi Raul

what I would do is the following:
- add the record without the barcode
- the autoId is now in the field
- use it to set the barcode
- hmodify to record the barcode

If the barcode is a uniqueID (which is the only logical reason for your " I need to generate the barcode before inserting the new record..."), I would just put a random number in it for the few hundreth of a second needed to do this...

Best regards

von Fabrice Harari - am 19.04.2013 01:56

You can also use query's to present/add/modify data with (hModifyFile) parameter.
With this you don't need to lock/unlock record. You don't need to add/modify record when you still don't know if user will cancel "add" operations, after which you need to delete added record and start many read/write operation on database to insert just new record. If you use read/write directly to tables then you can try to do this with just one table, make query and test results. But, this is just my opinion. You can use whatever you want. Ragards

You can just use :
HreadLast(PRODUCTS, ProductID)
nNextID is int
nNextID = PRODUCTS.ProductID + 1
EDT_barcode = "9930300"+nNextID

von ICI - am 19.04.2013 06:07
Hi Raul2,

you can force the use of a self-generated value:
- Get the last ID +1
- Fill your RecordID with this last ID +1
- Fill your barcode with 999001+[last-ID +1]
- Write your record with HAdd(YourFile,hSetIDAuto)

von Stefan Bentvelsen - am 19.04.2013 07:49
Thank you guys for your replies.

I think I'll choose the random ID generation, in order to write less code. It will work fine anyway.

Kind regards.

von RAUL2 - am 19.04.2013 22:58
Why not add your code to an AFTER TRIGGER? Set and forget!

von Geoff B - am 20.04.2013 00:55
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.