我有一个用于生成随机数的vba。这个数字是在打开表单后生成的,这很好,但它还需要在创建新记录后生成代码,如何才能完成?
Private Sub Form_Open(Cancel As Integer)
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
答案 0 :(得分:3)
如果该代码正在执行您想要的操作,您可以将其移动到表单模块中的单独过程。
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
然后更改表单打开:
Private Sub Form_Open(Cancel As Integer)
UpdateRandNum
End Sub
然后从表单的After Insert事件再次调用它。
Private Sub Form_AfterInsert()
UpdateRandNum
End Sub