Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an MS Access Maketable query, how to create an Autonumber field?

Tags:

vba

ms-access

I have a Maketable query in an Access db that could use an Autonumber field. I can't find a built-in function for my purpose. Do i need to write my own? Ideally, I'd just like to create a field in the Access designer as "Autonum: CreateAutoNumber()"

Edit: If it can't be done in the query itself, I can also run a procedure afterward.

like image 756
PowerUser Avatar asked Oct 27 '25 23:10

PowerUser


2 Answers

I reckon you either need to use TableDefs or DDL. The DDL query would run after the maketable query, for example:

 ALTER TABLE NewTable ADD COLUMN AutoField COUNTER

EDIT Additional note

If you wish to make the new column the primary key, you can run something like:

ALTER TABLE NewTable ADD PRIMARY KEY (AutoField)
like image 104
Fionnuala Avatar answered Oct 31 '25 01:10

Fionnuala


make your life simple, create a delete query followed by an append query, then use a macro to run them. if you need the autonumbering to start from 1 every time, create another query from your table with an extra column with the following code idnew:dcount("[id]","mytable","[id]<"&[id]) hope this helps.

like image 35
tony Avatar answered Oct 31 '25 00:10

tony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!