|
在软件开发中,经常会碰到录入基础资料编码的功能,比如供应商编码或助记码、客户及物料的编码或助记码.而用户大多希望在录入一个编码保存后,系统能自动在新增记录上显示规则的编码,比如输入:A0001能在新记录上显示A0002;KS09新增后自动显示下一记录编码KS10.
以下我从软件中提取自动编码函数,可实现大多数自动编码的功能:
Public Function AUTOINCREE_CODE(CONTENT As Variant) As String
On Error GoTo err
Dim i, b, itext
Dim STRCONT As String, ADDTIONZERO As String
Dim num As Long
num = 1
itext = CONTENT
Do
i = Mid(itext, Len(itext) - num + 1, 1)
If Len(i) = 0 Or IsNull(i) Then Exit Do
b = Asc(i)
If b >= 48 And b <= 56 Then
STRCONT = Mid(itext, 1, Len(itext) - num) & (i + 1) & ADDTIONZERO
STRCONT = Trim(STRCONT)
Exit Do
ElseIf b = 57 Then
ADDTIONZERO = ADDTIONZERO & "0"
num = num + 1
Else
If num = 1 Then
STRCONT = itext & "1"
Else
STRCONT = Mid(itext, 1, Len(itext) - (num - 1)) & 1 & Trim(ADDTIONZERO)
End If
Exit Do
End If
Loop
AUTOINCREE_CODE = STRCONT
Exit Function
err:
MsgBox err.Description, vbInformation, GLOTITLE
Exit Function
End Function |
|