简体   繁体   English

保存后,MS Access FE / SQL BE需要在表单上填充自动编号

[英]MS Access FE / SQL BE Need AutoNumber to populate on form after saving

So, I have an Access 2007 FE with an SQL BE. 因此,我有一个带有SQL BE的Access 2007 FE。 I am using a bound form for both adding a new record, or updating an existing record. 我正在使用绑定表来添加新记录或更新现有记录。

My problem is this: Unlike a local Access table, using SQL tables cause the AutoNumber field ("SeqID") to not be available until after the record is saved. 我的问题是:与本地Access表不同,使用SQL表会导致自动编号字段(“ SeqID”)直到保存记录后才可用。 I have a "save" button on the form, which does various validation steps on a new record, then adds a new record to the table upon successful validation. 我在表单上有一个“保存”按钮,它对新记录执行各种验证步骤,然后在成功验证后将新记录添加到表中。 At that point, I'm able to get the AutoNumber for that record in my code, but the problem is that I cannot get the form to update/display it in the SeqID field. 到那时,我可以在代码中获取该记录的自动编号,但是问题是我无法获取表格来在SeqID字段中进行更新/显示。 Because it's a bound form, I cannot simply update the SeqID.Value. 因为它是绑定形式,所以我不能简单地更新SeqID.Value。 I need to have this field updated because it is used for other things later on, such as audit trails for changed fields, etc. 我需要更新此字段,因为稍后将其用于其他用途,例如更改字段的审核跟踪等。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Try something on these lines. 在这些线路上尝试一些操作。 It works for me: 这个对我有用:

Private Sub AText_AfterUpdate()
   Me.Dirty = False
   Me.Recalc
End Sub

If the experiment works, it is just a matter of fitting the steps into the flow of your code. 如果实验可行,则只需将步骤与代码流程相适应即可。

When using SharePoint or SQL server, or in fact most server based databases, the autonumber ID is not generated until record save time. 当使用SharePoint或SQL Server或实际上大多数基于服务器的数据库时,只有在记录保存时间后才会生成自动编号ID。 With a form + sub form, Access will ALWAYS do a automatic record save when focus moves to the sub form, and thus you don't have a problem attaching child records to these parent forms + records. 使用表单+子表单,Access总是会在焦点移到子表单上时自动保存记录,因此将子记录附加到这些父表单+记录上没有问题。

In the case of a bound form, the ONLY way to get the autonumber ID is to force the record save. 对于绑定形式,获取自动编号ID的唯一方法是强制保存记录。 The simple code is this: 简单的代码是这样的:

If isnull(me!ID) = true then
    ' we don't have a record primary key yet, save record
   Me.Dirty = false
End if

At this point, you can then use the PK autonumber 此时,您可以使用PK自动编号

MsgBox "pk = " & me!id

So, a simple me.Dirty = false will save the record, and will force generation of the ID and code that follows is then free to use + grab the PK autonumber at will. 因此,简单的me.Dirty = false将保存记录,并将强制生成ID和随后的代码,然后可以随意使用+随意获取PK自动编号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM