database 我可以在 Access 中编辑自动编号列吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3113915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:46:12  来源:igfitidea点击:

Can I edit AutoNumber Column in Access?

databasems-accessdatabase-designautonumber

提问by Pece

I've lost my data in Access base, and I've manage to bring them back but when I copy the values in the table with the AutoNumber Column it increments the numbers. Is there Any way to change it to int and then bring it back to AutoNumber?

我在 Access base 中丢失了数据,我设法将它们带回来,但是当我使用 AutoNumber 列复制表中的值时,它会增加数字。有什么方法可以将其更改为 int 然后将其带回 AutoNumber?

采纳答案by Pece

I've manage to insert the AutoNumber fields by code from c#. I take all the data I need and just inserted in an empty table.

我已经设法通过 c# 中的代码插入自动编号字段。我把我需要的所有数据都插入到一个空表中。

回答by Kip

Here is how I managed to do this in Access 2010:

这是我在 Access 2010 中设法做到这一点的方法:

  1. Make a backup of your database. (Just to be safe.)
  2. Right-click on the table in the tables list, and select Export->Excel. Accept all defaults.
  3. Open the table in Excel and make the desired change to the autonumber field.
  4. Open the table and delete all rows
  5. Right-click on table in the tables list, and select Import->Excel
    • In the options, choose "Append to table" and select the table. Accept defaults for all other options
  1. 备份您的数据库。(只是为了安全。)
  2. 右键单击表列表中的表,然后选择“导出”->“Excel”。接受所有默认值。
  3. 在 Excel 中打开表格并对自动编号字段进行所需的更改。
  4. 打开表格并删除所有行
  5. 右键单击表格列表中的表格,然后选择导入-> Excel
    • 在选项中,选择“附加到表格”并选择表格。接受所有其他选项的默认值

This might not be a viable solution for a large table. I don't think Excel can handle more than around 65K rows.

对于大表,这可能不是一个可行的解决方案。我不认为 Excel 可以处理超过 65K 行。

回答by David-W-Fenton

Don't copy the data with the user interface, but append it with a query. Because an Autonumber field is just a long integer with a special default value, you can append to it values that already exist. That doesn't work in the UI, but only in SQL.

不要用用户界面复制数据,而是用查询附加它。因为自动编号字段只是一个带有特殊默认值的长整数,所以您可以向它附加已经存在的值。这在 UI 中不起作用,而仅在 SQL 中起作用。

An Autonumber field has a few other properties that are different from a normal Long Integer field, but in terms of appending data, those are not relevant. One of those properties is that it is not editable once it's populated, and another is that you can have only one in each table.

自动编号字段还有一些其他属性与普通长整型字段不同,但就附加数据而言,这些属性并不相关。这些属性之一是一旦填充就不可编辑,另一个是每个表中只能有一个。

回答by Fionnuala

How are you bringing the data back? It should be possible to append the data from your table and to keep the existing numbers.

你怎么把数据带回来?应该可以附加表中的数据并保留现有数字。

It is necessary however, that you paste from an integer field to the autonumber field. You cannot change a field to autonumber from integer once there is data in the field, but you can change to integer from autonumber.

但是,您必须从整数字段粘贴到自动编号字段。一旦字段中有数据,就不能将字段从整数更改为自动编号,但可以从自动编号更改为整数。

回答by user2520142

Make backup of your data table. Delete all data form original table and then do compact & repair your database. By doing this, auto number field will be reset at 1. You may now append your data from backup table.

备份您的数据表。删除原始表中的所有数据,然后进行压缩和修复数据库。通过这样做,自动编号字段将重置为 1。您现在可以从备份表中附加数据。

回答by charlesdeb

SQL code like

SQL 代码如

   insert into <tablename> 
   (<column 1>, <column2>, ...) 
   values
   ( <value 1>, <value 2>, ...);

will do the trick if you include the autonumber column in your query. It's pretty tedious, but works. You can switch to SQL mode for any old query to enter this text (usually after preparing it in a text editor), or as @Dominic P points out, you can bring up a VBA immediate window and run DoCmd.RunSQL "INSERT INTO ..."which will give you a better editor experience within Access.

如果您在查询中包含自动编号列,就会起作用。这很乏味,但有效。您可以为任何旧查询切换到 SQL 模式以输入此文本(通常在文本编辑器中准备好之后),或者正如@Dominic P 指出的那样,您可以调出一个 VBA 立即窗口并运行DoCmd.RunSQL "INSERT INTO ...",这将为您提供更好的编辑器Access 中的体验。