2010-11-04 45 views
1

我有一個用於生成隨機數的vba。這個數字是在打開表單後生成的,這很好,但是它也需要在創建新記錄後生成一個代碼,這個怎麼做?插入新記錄後訪問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 

回答

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 

而且從形式的插入事件後再次調用它。

Private Sub Form_AfterInsert() 
    UpdateRandNum 
End Sub