MS Access-在创建新记录时分配自定义编号

时间:2019-01-20 15:40:50

标签: ms-access

我想为每个记录分配一个唯一的编号:

当年的后两位数字+一个自动从0001增加到9999的数字

我已经在this thread的帮助下使其工作了,但是我想在创建新记录时自动生成数字。 This suggestion对我不起作用。

如果您创建新记录并单击按钮,它将起作用,但不适用于更新前事件。后面的代码是相同的。

My working example

'generate custom number when you click the button
Private Sub Comando10_Click()

    If Me.NewRecord Then

        Dim ThisYear As String
        ThisYear = Format(Date, "yy")
        Me.campo = ThisYear & Format(Nz(DMax("Right(campo, 4)", "tabella", "Left(campo,2) = '" & ThisYear & "'"), 0) + 1, "0000")

    End If

End Sub

'generate custom number as a new record is created
Private Sub Form_BeforeUpdate(Cancel As Integer)

    If Me.NewRecord Then

        Dim ThisYear As String
        ThisYear = Format(Date, "yy")
        Me.campo = ThisYear & Format(Nz(DMax("Right(campo, 4)", "tabella", "Left(campo,2) = '" & ThisYear & "'"), 0) + 1, "0000")

    End If

End Sub

0 个答案:

没有答案