Sub AutomaticNumber()
Call Connection
Set Rs_User = New ADODB.Recordset
Rs_User.Open "SELECT * FROM TBL_USER WHERE id_user IN (SELECT
MAX(id_user)FROM TBL_USER)ORDER BY id_user DESC", Conn
Rs_User.Requery
Dim x As String * 15
Dim count As Long
With Rs_User
If .EOF Then
x = "USR" + Format(Date, "yyyymmdd") + "001"
NewNumber = x
Else
If Left(!id_user, 8) <> Format(Date, "yyyymmdd") Then
x = "USR" + Format(Date, "yyyymmdd") + "001"
Else
Count = Right(!id_user, 3) + 1
x = "USR" + Format(Date, "yyyymmdd") + Right("00" &
Count, 2)
End If
End If
NewNumber = x
End With
End Sub
此代码可能会导致产生USR20180805001,但是当我尝试向数据库中添加另一条记录时,由于该代码无法正确计数/增加右边的最后3位数字,因此无法添加。因此显示错误重复条目。谢谢您的关注。