pr2-eugin
Super Moderator
- Local time
- Today, 08:56
- Joined
- Nov 30, 2011
- Messages
- 8,487
This piece of code will cause an infinite loop to occur..
The following code, will run to provide you with unique number like..
However this not only defies the main objective of creating a unique ID with First Initial and parts of Last name.. BUT also with the greatest disadvantage of running into an infinite loop, say after 9999 similar records.. The code will break.. Time to consider alternatives?
Code:
While DCount("*", "tblusers", "UserID = '" & retStr & "'") > 0
retStr = "DUPUSKM" & Mid(retStr, 5)
Wend
Code:
USKMJHASSLEH
DUPUSKM1JHAS
DUPUSKM2SKM1
DUPUSKM3SKM2
Code:
Public Function getSpecialID(firstStr As String, middleStr As Variant, lastStr As String) As String
'-------------------------------------------------------------------------
' A Function that will take in First Name, Middle Name and Last Name
' returns a Specially designed UserID.
'
' Input : First Name, Middle Name and Last Name
' Output : A UserID
' Example :
' ? getSpecialID("Mary", "Q", "Shortname")
' USKMMQSHORTN
' ? getSpecialID("John", "", "Hasareallyreallylonglastname")
' USKMJHASAREA
' ? getSpecialID("John", "", "Hasanotherlonglastname")
' USKM1JHASARE
'--------------------------------------------------------------------------
Dim retStr As String, ctr As Integer, incCtr As Integer
On Error GoTo Error_handler
retStr = "uskm" & Left(firstStr, 1)
If Len(middleStr & vbNullString) <> 0 Then retStr = retStr & Left(middleStr, 1)
retStr = retStr & Left(lastStr, 12 - Len(retStr))
retStr = UCase(retStr)
ctr = DCount("*", "tblusers", "UserID = '" & retStr & "'")
While ctr > 0
incCtr = incCtr + 1
retStr = Left("DUPUSKM" & incCtr & Mid(retStr, 5), 12)
ctr = DCount("*", "tblusers", "UserID = '" & retStr & "'")
Wend
getSpecialID = Left(retStr, 12)
exitHandler:
Exit Function
Error_handler:
MsgBox Err.Number & ": " & Err.Description
Resume exitHandler
End Function