Public Function ZDYBH(StrTable As String, StrField As String, StrBH As String, Optional FDay As String = "YYMMDD", Optional ILen As Integer = 3, Optional B As Boolean = False) As String
'==================================================================
'功能: 自定义用户编号,带断号重号检测功能
'格式: "XXX"-"日期格式 "-"0000"
' StrTable 为任意表名称
' StrField 为任意字段名称
' StrBH 编码类型 , 可自义任意格式
' FDay 日期格式 默认日期格式为:YYMMDD",其他符合FORMAT格式的如 YYYYMM,YYYY,YYYYMMDD
' ILen 为数字格式化长度 , 默认长度为3 代表 000 格式
' B 为是否要检测断号 ,默认为FALSE 不检测
'用法: ZDYBH("产量表", "产量ID", "LDH")
' ZDYBH("产量表", "产量ID", Me.Combo1)
' ZDYBH("产量表", "产量ID", Me.Combo1, "YYYY")
' ZDYBH("产量表", "产量ID", Me.Combo1, , 4, True)
'作者: andymark
' QQ : 42503577 , ewang11@163.com
'日期: 2008-4-2
'===============================================================
Dim Rs As New ADODB.Recordset
Dim Conn As New ADODB.Connection
Dim Str As String
Dim Num As Integer
Dim TNum As Integer
Dim StrWhere As String
Dim StrOrderWhere As String
Dim StrOrderWhereDesc As String
Set Conn = CurrentProject.Connection
Str = StrBH & "-" & Format(Now(), "" & FDay & "") & "-"
StrWhere = "select " & StrField & " from " & StrTable & " where " & StrField & " like '" & Str & "%'"
StrOrderWhere = StrWhere & " Order by " & StrField & ""
StrOrderWhereDesc = StrWhere & " Order by " & StrField & " DESC"
Rs.Open StrOrderWhereDesc, Conn, adOpenKeyset, adLockOptimistic
If Rs.EOF Then
Num = 0
Else
Num = Right(Rs.Fields("" & StrField & ""), ILen)
If B = True Then
If CInt(Num) <> Rs.Fields.Count Then
Rs.Close
Rs.Open StrOrderWhere, Conn, adOpenKeyset, adLockOptimistic
Num = 0
Do While Not Rs.EOF
TNum = CInt(Right(Rs.Fields("" & StrField & ""), ILen))
If TNum = Num Then
MsgBox StrBH & "-" & Format(Now(), "" & FDay & "") & "-" & Format(Num, String(ILen, "0")) & "存在重号,请检查数据的"
Exit Function
Else
If TNum - Num = 1 Then
Num = TNum
Else
Exit Do
End If
End If
Rs.MoveNext
Loop
End If
End If
End If
ZDYBH = StrBH & "-" & Format(Now(), "" & FDay & "") & "-" & Format(Num + 1, String(ILen, "0"))
Set Rs = Nothing
Set Conn = Nothing
End Function
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) | Powered by Discuz! X3.3 |