INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Do While Loops

Do While Loops

(OP)
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

RE: Do While Loops

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: Microsoft: Access Modules (VBA Coding)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close