Detect new record and obtain ID using Access VBA

…衆ロ難τιáo~ 提交于 2019-12-24 02:14:19

问题


Related to this question, how do I detect that a form user is adding a record? It would be ideal if I knew this as soon as the form's "current record" became "new" (navigating beyond the last existing record, or clicking the "new" navigation button), but knowing as soon as the first piece of data begins to be entered would be almost as good. I'm guessing that this would be testing something in Form_Current(), but what (or is there a better way)?

Further related, what is the earliest that I can find the (Autonumber) ID for the new record? That requires a save, does it not? Is it automatically populated into the form's recordset? (I know this is not the case for code-driven saves .AddNew ... .Update.) The reason I'm asking for this is to begin populating the child table behind the subform (the data is such that I can auto-create a "default" data set that the user can confirm or modify).


回答1:


I think you want :

Private Sub Form_Current()
    If Me.NewRecord Then

End Sub

The autonumber should not be used with a child table until the record has been saved, or you will (should) run into integrity problems. When the subform gets focus, the autonumber of the main table will have been saved. Do not forget that you can do a lot with link child, link master fields on a subform.



来源:https://stackoverflow.com/questions/4403987/detect-new-record-and-obtain-id-using-access-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!