简体   繁体   中英

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

So, I have an Access 2007 FE with an SQL BE. 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. 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. Because it's a bound form, I cannot simply update the 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. 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.

In the case of a bound form, the ONLY way to get the autonumber ID is to force the record save. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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