Results 1 to 6 of 6

Thread: Create Unique Field

  1. #1

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Create Unique Field

    I've created a 'username' field that will be auto-completed based on first and last name in my form text fields, so it starts as Me.firstname + Me.lastname

    What I'd like to do, is verify that no one else has that username first. If they do, I want to loop through their first name, adding a character to the username until it's unique.

    For example, lets say I enter:

    John Smith... his username would be jsmith - check the database - no values so he gets jsmith

    now I go to enter Jane Smith, but when the 'unique' check happens, she can't be jsmith, because there already is one... so it appends the second character to her name, she would be: jasmith.
    if I entered June Smith, she would be jusmith, because jsmith is taken, but jusmith is not.

    Now, if I wanted to enter Janice Smith, she would end up being jansmith, because jsmith is taken, it tries jasmith, thats taken, so it moves to jansmith.

    Does this make sense?

    How would I make this type of "recursive" check?


    -Matthew-

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,695

    Re: Create Unique Field

    E.g.
    vb.net Code:
    1. Function CreateUserName(givenName As String, surname As String) As String
    2.     Dim substringLength = 1
    3.     Dim userName As String
    4.  
    5.     Do
    6.         userName = givenName.Substring(0, substringLength) & surname
    7.         substringLength += 1
    8.     Loop Until IsUniqueUserName(userName)
    9.  
    10.     Return userName
    11. End Function
    That handles what you've asked for but it will crash if it gets to the end of the given name without finding a unique user name. You haven't told us what you want to do in that case so I haven't included it. Hopefully it's obvious that you need to implement the IsUniqueUserName method yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Re: Create Unique Field

    thanks JM... most of this code works... one thing I didn't mention is that i'm doing this in ligthswitch - but VB.net within ligthswitch (silverlight application).

    Everything seems to work except for the Loop Until isUniqueUsername
    Perhaps this will help further things along.

    To answer your previous question - I'm going to implement a counter and if it gets to the end and there still isn't a unique value, I will append the counter number to the end.

    Table is called Users and the field it is checking again is called *****ername. The form has FirstName and LastName fields, so heres the code I have so far, based on what you gave me.

    Code:
                Dim substringLength = 1
                Dim userName As String
    
                Do
                    userName = Me.FirstName.Substring(0, substringLength) & Me.LastName
                    substringLength += 1


    -Matthew-

  4. #4

    Thread Starter
    Fanatic Member mateo107's Avatar
    Join Date
    Jan 2005
    Posts
    547

    Re: Create Unique Field

    and i'm not entirely sure how to iterate through the "Users".username table.fieldname - so i think that might be partially why this code isn't working.

    So essentially, using your code and some psuedo-code, it should be if userName = FirstRecordinUsersTable.username - iterate the substringlength, start the search again, repeat until there is no match and return that value. if no unique values hit after iterating through all substring lengths, make the username = userName & substringlength


    -Matthew-

  5. #5
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,168

    Re: Create Unique Field

    You may need it moved to a silverlight/switch forum section:
    http://www.vbforums.com/forumdisplay.php?88-Silverlight

    Don't duplicate I will PM a Mod. to have it moved. They should be able to help a little easier there...

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,392

    Re: Create Unique Field

    Thread moved to Silverlight forum. If there is a better place, just report the thread and I'll move it again (or somebody else will, whoever sees it first).
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width