How to generate username based on table lookup? (1 Viewer)

mmai

New member
Local time
Yesterday, 19:21
Joined
Sep 21, 2019
Messages
2
Need a function or code or expression that will generate a username value for that will not conflict with any username value that already exists in Table_MASTER. If the value already exists, then increment the generated value by a number (consecutively) until no conflict.

Example:
Table_MASTER, username field has values: John, John1, John2, John3


TableCalculated, username field has value: john
TableCalculated, NEWusername field = FUNCTION ?? This function would ideally generate the value "John4" since it will not conflict with any existing values in Table_Master, and is the next available number to use after John3.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 03:21
Joined
Feb 19, 2013
Messages
16,607
NEWusername field = FUNCTION ??
google what is frequently called dmax+1
 

mmai

New member
Local time
Yesterday, 19:21
Joined
Sep 21, 2019
Messages
2
I would definitely be interested in using the getmax, but don't know how to make if work in the context I described
 

June7

AWF VIP
Local time
Yesterday, 18:21
Joined
Mar 9, 2014
Messages
5,470
As suggested in other forum, use a combobox.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:21
Joined
May 7, 2009
Messages
19,233
As suggested in other forum, use a combobox.
can't help but noticed on two forums.
but seems invisible as the discussions continues.

this might help you.
you pass the username to the function.
if the username is not in the table, it will return same username.
otherwise it will return username(+number), that is not in the database.


dim usrname as string
usrname="mmai"
usrname = genUser(usrname)


Code:
Public Function genUser(ByVal user As String) As String

    Const table_name As String = "table_master"
    Const field_name As String = "username"
    
    Dim new_user As String
    Dim i As Integer
    
    new_user = user
    While DCount("1", table_name, field_name & "=" & Chr(34) & new_user & Chr(34)) > 0
        i = i + 1
        new_user = user & i
    Wend
    
    genUser = new_user
        
End Function
 

Users who are viewing this thread

Top Bottom