Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do While Loops

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
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
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
 
Why not just add a number? Whether or not this works will depend on what queryAD returns.

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
    TestName=uName
    Do Until TestName <> queryAD(TestName)
        TestName = uName & n
        n = n + 1
    Loop
        
    uName = TestName
    Username.Value = uName
Else
     uName = LCase(fName & lName)
     Username.Value = uName
End If


End Function

The forum for this is forum705
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top