office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access自定义自动编号 月份重新开始

2018-09-05 11:05:00
tmtony8
原创
8095

在论坛中,我们可以轻松搜索到自定义的自动编号函数。可以直接调用使用,如下:


access创建自定义递增自动编号(不固定位数)

 一个用于自定义自动编号的通用函数(支持数字型、文本型编号,支持断号重续)

高效的年月日的自动编号函数


但是有时需要设置每月都从头开始,即每月都重新开始编号,

如前面四位前缀,二位年二位月+三位档案号。即后面这三位档案号只要到下个月自动断号从001再开始。


月份自动重新编号详细函数:

Public Function AutoNumber(Prefixal As String, Digit As Integer, FieldName As String, TableName As String) As String
    
    
    Dim strMaxID As Long
    Dim strNumberFormat As String
    Dim i As Integer
    

    strMaxID = Nz(DMax("Right( " & FieldName & "," & Digit & ")", TableName, "Mid(" & FieldName & ", Len('" & Prefixal & "') + 1, 4)=" & Format(Date, "yymm") & "")) + 1
    
    
    For i = 1 To Digit
        strNumberFormat = strNumberFormat & "0"
    Next
    AutoNumber = "" & Prefixal & Format(Date, "yymm") & Format(strMaxID, strNumberFormat) & ""
    

End Function



调用代码:在窗体中添加一个按钮,其中,“bbbb”是前缀,“3”是后缀位数,“aa”为字段名,“tbldte”是表名

    CurrentDb.Execute "insert into tbldte(aa) values ('" & AutoNumber("bbbb", 3, "aa", "tbldte") & "')"

    分享