Public Function autoidd(ByVal prefixion As String, idlength As Integer, tblname As String, fldname As String) As String
'编程的过程中要注意:
'给变量起名一定要简短、有意义、好记。
'发现问题多调试、多思考。
'不能使用系统保留单词,比如autoid就不能作为自定义函数名称使用。
Dim i As Long
'考虑到数据库中可能会有几十万条甚至上百万条记录,所以这里应该用长整型。
Dim formatstring As String
On Error GoTo err_autoidd:
formatstring = String(idlength, "0")
If DCount(fldname, tblname) = 0 Then
autoidd = prefixion & Format(1, formatstring) '空数据从1开始编号。
Else
i = Val(Right(DMax(fldname, tblname), idlength)) + 1
autoidd = prefixion & Format(i, formatstring)
End If
exit_autoidd:
Exit Function
err_autoidd:
autoidd = "#" '出错了给个#。
End Function