vba 在 MS Access Maketable 查询中,如何创建自动编号字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2433812/
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-11 11:25:19  来源:igfitidea点击:

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

ms-accessvba

提问by PowerUser

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()"

我在可以使用自动编号字段的 Access 数据库中有一个 Maketable 查询。我找不到适合我的目的的内置函数。我需要自己写吗?理想情况下,我只想在 Access 设计器中创建一个字段为“Autonum:CreateAutoNumber()”

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

编辑:如果不能在查询本身中完成,我也可以在之后运行一个过程。

回答by Fionnuala

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

我认为您需要使用 TableDefs 或 DDL。DDL 查询将在 maketable 查询之后运行,例如:

 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)

回答by tony

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.

让您的生活变得简单,创建一个删除查询,然后是一个追加查询,然后使用宏来运行它们。如果您每次都需要自动编号从 1 开始,请从您的表中创建另一个查询,并使用以下代码 idnew:dcount("[id]","mytable","[id]<"&[id] ) 希望这可以帮助。

回答by MeX

I'd use vba

我会使用 vba

ALTER TABLE tbl_YourTable ADD [ID] AUTOINCREMENT PRIMARY KEY NOT NULL

回答by HenryHansonPhD

  1. Create an empty table containing your schema (use table design or copy existing table without data) and add an Autonumber field. Call this table yourtable_FORMAT
  2. Copy yourtable_FORMAT to yourtable (each time you want to do a make table)
  3. APPEND records to yourtable. (This causes the Autonumber field to always restart at 1.)
  1. 创建一个包含您的架构的空表(使用表设计或复制没有数据的现有表)并添加一个自动编号字段。将此表称为 yourtable_FORMAT
  2. 将yourtable_FORMAT复制到yourtable(每次要做一个make table)
  3. 将记录追加到您的表中。(这会导致自动编号字段始终从 1 重新开始。)

(This process also lets you append LOTS of tables together without using a UNION command.)

(此过程还允许您在不使用 UNION 命令的情况下将许多表附加在一起。)