开发者

access vba execute after inserting new record

I have a vba for generating a random number. This number is generated after opening the form, this is good, but it also needs to generate a code after a new record is made, how can this be done?

Private Sub Form_Open(Cancel As Integer)
Dim strPin As String
Dim i As Integer

strPin = "JobNr: "

'Set seed
Call Randomize

For i = 1开发者_JAVA技巧 To 5
   strPin = strPin & Int(10 * Rnd)
Next

Me.random_nr = strPin
End Sub


If that code is doing what you want, you can move it to a separate procedure in the form's module.

Private Sub UpdateRandNum()
    Dim strPin As String
    Dim i As Integer

    strPin = "JobNr: "

    'Set seed '
    Call Randomize

    For i = 1 To 5
       strPin = strPin & Int(10 * Rnd)
    Next

    Me.random_nr = strPin
End Sub

Then change Form Open:

Private Sub Form_Open(Cancel As Integer)
    UpdateRandNum
End Sub

And call it again from the form's After Insert event.

Private Sub Form_AfterInsert()
    UpdateRandNum
End Sub
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜