自动编号也疯狂
时 间:2007-12-05 15:39:48
作 者:victorduane ID:70 城市:闵行
摘 要:自动编号也疯狂
正 文:
自动编号也疯狂
'===============================================================================
'-函数名称: 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群 (群号:483923997) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access小作品】简单的待...(04.30)
- 【Access窗体导出PDF】...(04.08)
- 【Access窗体导出PDF】...(04.07)
- Access两种方式实现即时更...(03.01)
- Access隐藏与显示lacc...(01.12)
- 【Access高效办公】将每个...(12.23)
- Access21点游戏源代码(12.13)
- 【Access窗体导出Exce...(11.15)
- 【Access开发】Acces...(11.14)
学习心得
最新文章
- 【Access小作品】简单的待办任...(04.30)
- 从另一个ACCESS数据库批量导入...(04.29)
- Access日期格式的数据导出Ex...(04.28)
- ACCESS精华集锦资料.CHM(04.25)
- Access VBA语句If Me...(04.24)
- 【Access修改记录示例】编辑选...(04.22)
- 【Access表名称命名建议】将A...(04.20)
- Access学习笔记--用Acce...(04.19)
- 【Access重复项查询示例】将A...(04.17)
- Access快速开发平台企业版--...(04.16)