自动编号也疯狂-Victor Duane
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


自动编号也疯狂

发表时间:2007/12/5 15:39:48 评论(8) 浏览(8862)  评论 | 加入收藏 | 复制
   
摘 要:自动编号也疯狂
正 文:

 自动编号也疯狂

'===============================================================================
'-函数名称:         autoID
'-功能描述:         自动编号 ,
'-输入参数说明:     参数1:tblname 表名
'                   参数2:fldname 字段名
'                   参数3:intlength增长号的长度
'                   参数4:prefix 前缀(可选参数)
'                   参数5:dateformat时间前缀(可选参数)
'-返回参数说明:     可以按时间自增长编号, 如果没有时间就自增长编号
'-使用语法示例:     AutoID("型体编号表", "型体编号", 4, "GL")
'                   AutoID("型体编号表", "型体编号", 4)
'                   AutoID("型体编号表", "型体编号", 4, "GL","yyyymmdd")
'-参考:
'-使用注意:
'-兼容性:           2000,XP,2003
'-作者:             Victor Duane
'-更新日期:        2007-12-5 17:03
'===============================================================================
Function AutoID(tblName As String, fldName As String, intLength As Integer, Optional Prefix As String = "", Optional DateFormat As String = "") As String
'自动编号 , 可以按时间自增长编号, 如果没有时间就自增长编号
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection

Dim intDate As Integer
Dim intPrefix As Integer

Dim strDate As String
Dim strToday As String
Dim OldDate As String

'如果DateFormat为空,当天日期为空,否则为当天日期
If Len(DateFormat) = 0 Then
    strToday = ""
Else
    strToday = Format(Date, DateFormat)
End If

strDate = strToday
intDate = Len(strDate) '取得时间的长度
intPrefix = Len(Prefix) '取得前缀的长度

Set cn = CurrentProject.Connection
Dim strSQL As String
strSQL = "select * from " & tblName & " order by " & fldName
rs.Open tblName, cn, 1, 2

Debug.Print rs.RecordCount
If rs.RecordCount > 0 Then
    rs.MoveLast
    '取得最大值的时间前缀
    OldDate = Nz(Mid$(rs(fldName), intPrefix + 1, intDate), "")
    '判断新增的值是否跨时间
    If strToday = OldDate Then
        AutoID = Prefix & strDate & Format(Val(Right$(rs(fldName), intLength) + 1), String(intLength, "0"))
    Else
        AutoID = Prefix & strDate & String(intLength - 1, "0") & "1"
    End If
Else
    AutoID = Prefix & strDate & String(intLength - 1, "0") & "1"
End If

rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Function


Access软件网交流QQ群(群号:198465573)
 
 相关文章
【Access源码】分组自动编号\筛选数据后自动分组编号的示例\分...  【麥田  2011/7/26】
高效的年月日的自动编号函数  【LiShiping  2012/8/18】
自动编号,每个月都重新从0001号开始编号  【老邹  2012/12/11】
【Access文章】在Access中重置“自动编号”字段值  【缪炜  2013/10/18】
【Access入门】vba代码新增字段为自动编号类型,生成自动编号...  【麥田  2013/11/12】
简单的自动编号  【jia  2013/11/14】
常见问答
技术分类
相关资源
文章搜索
关于作者

Victor Duane

文章分类

文章存档

友情链接