Hi,
This is realy a general VBA programming question, but as it is for an Access database, I have decided to post here. I hope nobody objects.
Anyway, I am creating a Database/application, that will be used by our 1st line Helpdesk to create new users. (Strange I know, but orders are orders!).
I have a button on the form that generates the username based on the first and last name (first name, plus first letter of last name). I have a function that goes and queries Active Directory to see if the generated username already exists. If it does already exist, then I need to increment 'n' in "left(lastname.value,n)" from 1 to 2.
I have suceeded in doing this using simple If statements, but I'd like to do it so that it works in a loop statement so that it will keep incrementing 'n' until the username is unique. (I know there are failings in this plan, like two people could have the same first and lastname.)
As far as I can see, my code (below) is getting stuck in the loop as I have to crash out of Access. How do I get it to exit?
Here is the code
Any help would be greatly apreciated.
Many thanks
Woter
This is realy a general VBA programming question, but as it is for an Access database, I have decided to post here. I hope nobody objects.
Anyway, I am creating a Database/application, that will be used by our 1st line Helpdesk to create new users. (Strange I know, but orders are orders!).
I have a button on the form that generates the username based on the first and last name (first name, plus first letter of last name). I have a function that goes and queries Active Directory to see if the generated username already exists. If it does already exist, then I need to increment 'n' in "left(lastname.value,n)" from 1 to 2.
I have suceeded in doing this using simple If statements, but I'd like to do it so that it works in a loop statement so that it will keep incrementing 'n' until the username is unique. (I know there are failings in this plan, like two people could have the same first and lastname.)
As far as I can see, my code (below) is getting stuck in the loop as I have to crash out of Access. How do I get it to exit?
Here is the code
Code:
Function generateUsername()
fName = FirstName.Value
lName = Left(LastName.Value, 1)
uName = LCase(fName & lName)
Username.Value = uName
If Username.Value = queryAD(Username.Value) Then
MsgBox "Username already exists!"
n = 1
Do Until uName <> queryAD(uName)
n = n + 1
lName = Left(LastName.Value, n)
Loop
uName = LCase(fName & lName)
Username.Value = uName
Else
uName = LCase(fName & lName)
Username.Value = uName
End If
End Function
Any help would be greatly apreciated.
Many thanks
Woter