Prevent AutoNumber Reset During Compact/Repair (MS Access) - ms-access

Is it possible, during compact/repair of an Access database, to prevent the AutoNumber ID from resetting?
For example, if I have records 1-10 and I delete record 10, then compact/repair, 10 will be the next record. What I'd like is for 11 to be the next record.
Thanks in advance!

An autonumber is only for identifying the record and should not be meaningful.
If 10 and 11 and so on means something for your application and/or the user, have a separate field to hold these numbers.

Related

How to stop users from editing a single row in MS Access Table?

I have a row in a table which I use for auto-numbering other fields in the same table, if this row is edited it can mess up the sequence. How can I stop users who are making use of this database from editing this one particular row?
You can't. If a user or process creates a new record but regrets the action, the number is lost.
The Autonumber is designed to uniquely identify the individual record, nothing else. If you need a sequence number or similar for any other purpose, roll you own function. There are plenty of examples out there for the browsing.

Can an AutoNumber in MS Access randomly change?

We have a fairly simple multi-user database that we use to make Purchase Orders. The PO Number is generated from an AutoNumber field.
Sometime in the last 4 days something happened that made the numbers go from 8518 to 17474. I know that a user can't change this number. I am just wondering what could have caused this?
There are no records in between 8518 and 17474 to show that 9000(ish) records have been made. I am very confused!!
Autonumber fields in Access are updateable for new records. A user can just write a value to the autonumber column, and Access will continue numbering from there.
For example, a query like this could do it:
INSERT INTO [Purchase Orders]([PO Number]) VALUES (17473)
That will write that value to a new record, and Access will continue on from there.
Also, when copy-pasting, Access will create new autonumbers. The following actions will increment your autonumber seed by the number of records in the table:
When in table view:
Ctrl + A (Select all)
Ctrl + C (Copy)
Move to new record, select entire row
Ctrl + V (Paste all records)
Escape (oops, didn't mean to copy table)
No new records would've been added since the action was canceled, but only after already generating autonumbers for all those new records.

Autonum changed when insert a register in middle of table

I've detected a change in the way autonums are being managed in access. May be due to de access verions.
Till Access 2003, when I insert a record in the middle of a table that uses autonum key to create new record, the autonum counter keeps its original value. I mean:
The ID field is an autonun, which actual value is 1000
I insert a new record (using a query) with an ID value of 400
This is what happens:
If I insert this record in one computer running access 2003 the ID autonum stays in 1000, so next record that is added recieves an ID of 1001. Good for me!
If I insert this record in one computer running access 2010 the autonum IS SET to the last manually assigned value, so next record that is added recieves an ID of 401. Bad for me!
Am I wrong or this is due to the Access version? Is anything related to MDAC? Can I do something to avoid the autonum reset when inserting?
Thank you

Calculate a field's value based on multiple records in another table in Access DB

I'm trying to create a database in Access 2010, and have run into a bit of a problem. I currently have two tables, EarlyStageListResults & ESDailyTotals. Each of these tables has a field named Records, with ESDailyTotals being a summary of multiple entries in EarlyStageListResults. What I need to do is have the Records field in ESDailyTotals be the sum of multiple Records fields in EarlyStageListResults. For example, given the following records in EarlyStageListResults:
Date Records
4/22/16 2000
4/22/16 3000
4/22/16 1500
4/21/16 1200
4/21/16 2700
the records in ESDailyTotals should be:
Date Records
4/22/16 6500
4/21/16 3900
I know this can be calculated later through VBA and a form event, but optimally I'd like to be able to have it update as soon as any of the Records fields in EarlyStageListResults changes. It looks like there may be a way to do this using the Access Macro Editor (not sure of the name, but the tool where you can create a macro through a series of combo boxes rather than through VBA), but I've never gotten an understanding of using that tool, so have always relied on Forms and VBA instead. Basically if there's an event that triggers when a field is updated, and a way to enter VBA code into that event handler like you can with Access Forms, then I can do it with either DLookup or an SQL statement I think, but I don't know how to grab that event handler.
This does all need to be done within Access itself, I can't use an external program to update the records in an Access database file. This is for work, and (officially at least) any custom programs are a big no-no. :)
Thanks in advance everyone!
You're making this way too complicated. You just need one table and a database view (called a query in Access, I think) that is defined as
select Date, sum(Records) as Records from EarlyStageListResults
group by Date
Name the query ESDailyTotals.

How to restart counting from 1 after erasing table in MS Access?

I have table in MS Access that has an AutoNumber type in field ID
After inserting some rows, the ID has become 200
Then, I have deleted the records in the table. However, when I tried to insert a new row, I see that the ID starts with 201
How can I force the ID to restart with 1, without having to drop the table and make new a new one?
In Access 2010 or newer, go to Database Tools and click Compact and Repair Database, and it will automatically reset the ID.
You can use:
CurrentDb.Execute "ALTER TABLE yourTable ALTER COLUMN myID COUNTER(1,1)"
I hope you have no relationships that use this table, I hope it is empty, and I hope you understand that all you can (mostly) rely on an autonumber to be is unique. You can get gaps, jumps, very large or even negative numbers, depending on the circumstances. If your autonumber means something, you have a major problem waiting to happen.
In addition to all the concerns expressed about why you give a rat's ass what the ID value is (all are correct that you shouldn't), let me add this to the mix:
If you've deleted all the records from the table, compacting the database will reset the seed value back to its original value.
For a table where there are still records, and you've inserted a value into the Autonumber field that is lower than the highest value, you have to use #Remou's method to reset the seed value. This also applies if you want to reset to the Max+1 in a table where records have been deleted, e.g., 300 records, last ID of 300, delete 201-300, compact won't reset the counter (you have to use #Remou's method -- this was not the case in earlier versions of Jet, and, indeed, in early versions of Jet 4, the first Jet version that allowed manipulating the seed value programatically).
I am going to Necro this topic.
Starting around ms-access-2016, you can execute Data Definition Queries (DDQ) through Macro's
Data Definition Query
ALTER TABLE <Table> ALTER COLUMN <ID_Field> COUNTER(1,1);
Save the DDQ, with your values
Create a Macro with the appropriate logic either before this or after.
To execute this DDQ:
Add an Open Query action
Define the name of the DDQ in the Query Name field; View and Data Mode settings are not relevant and can leave the default values
WARNINGS!!!!
This will reset the AutoNumber Counter to 1
Any Referential Integrity will be summarily destroyed
Advice
Use this for Staging tables
these are tables that are never intended to persist the data they temporarily contain.
The data contained is only there until additional cleaning actions have been performed and stored in the appropriate table(s).
Once cleaning operations have been performed and the data is no longer needed, these tables are summarily purged of any data contained.
Import Tables
These are very similar to Staging Tables but tend to only have two columns: ID and RowValue
Since these are typically used to import RAW data from a general file format (TXT, RTF, CSV, XML, etc.), the data contained does not persist past the processing lifecycle
I think the only ways to do this is outlined in this article.
The article explains several methods. Here is one example:
To do this in Microsoft Office Access 2007, follow these steps:
Delete the AutoNumber field from the main table.
Make note of the AutoNumber field name.
Click the Create tab, and then click Query Design in the Other group.
In the Show Table dialog box, select the main table. Click Add, and then click Close.
Double-click the required fields in the table view of the main table to select the fields.
Select the required Sort order.
On the Design tab, click Make Table in the Query Type group. Type the new table name in the Table Name box, and then click OK.
On the Design tab, click Run in the Results group.
The following message appears:
You are about to paste # row(s) into a new table.
Click Yes to insert the rows.
Close the query.
Right-click the new table, and then click Design View.
In the Design view for the table, add an AutoNumber field that has the same field name that you deleted in step 1. Add this AutoNumber
field to the new table, and then save the table.
Close the Design view window.
Rename the main table name. Rename the new table name to the main table name.
I always use below approach. I've created one table in database as Table1 with only one column i.e. Row_Id Number (Long Integer) and its value is 0
INSERT INTO <TABLE_NAME_TO_RESET>
SELECT Row_Id AS <COLUMN_NAME_TO_RESET>
FROM Table1;
This will insert one row with 0 value in AutoNumber column, later delete that row.