开发者

Detect new record and obtain ID using Access VBA

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 "ne开发者_运维问答w" (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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜