Generate a random 6 character output

S

Stephen sjw_ost

I am looking for a way to randomly generate a 6 character output of letters
and numbers like abc123 or 258qwe etc...
Can anyone show me how to do this or point me to some examples?

Thanks for any help.
 
S

Stephen sjw_ost

Well I have built my own answer.
1 table with 2 fields. LTR and NUM.
LTR has 10 letters and NUM numbers the letters 0-9.
The following is the code I built from the help file.

Function RNDPassword()
Dim db As DAO.Database
Dim char1, char2, char3, char4, char5, char6
Dim ltr1 As Recordset, ltr2 As Recordset, ltr3 As Recordset
Set db = CurrentDb
Randomize
char1 = Int((6 * Rnd) + 1)
char2 = Int((6 * Rnd) + 1)
char3 = Int((6 * Rnd) + 1)
Set ltr1 = db.OpenRecordset("SELECT * FROM t_RND WHERE num='" & char1 &
"'")
Set ltr2 = db.OpenRecordset("SELECT * FROM t_RND WHERE num='" & char2 &
"'")
Set ltr3 = db.OpenRecordset("SELECT * FROM t_RND WHERE num='" & char3 &
"'")
char4 = Int((6 * Rnd) + 1)
char5 = Int((6 * Rnd) + 1)
char6 = Int((6 * Rnd) + 1)

Form_f_Test.txtrslt.Value = ltr1("LTR") & ltr2("LTR") & ltr3("LTR") &
char4 & char5 & char6

End Function
Of course you would want to move the output to where ever work for you.
I hope this helps someone.
 
J

John W. Vinson

Well I have built my own answer.
1 table with 2 fields. LTR and NUM.
LTR has 10 letters and NUM numbers the letters 0-9.
The following is the code I built from the help file.

Function RNDPassword()
Dim db As DAO.Database
Dim char1, char2, char3, char4, char5, char6
Dim ltr1 As Recordset, ltr2 As Recordset, ltr3 As Recordset
Set db = CurrentDb
Randomize
char1 = Int((6 * Rnd) + 1)
char2 = Int((6 * Rnd) + 1)
char3 = Int((6 * Rnd) + 1)
Set ltr1 = db.OpenRecordset("SELECT * FROM t_RND WHERE num='" & char1 &
"'")
Set ltr2 = db.OpenRecordset("SELECT * FROM t_RND WHERE num='" & char2 &
"'")
Set ltr3 = db.OpenRecordset("SELECT * FROM t_RND WHERE num='" & char3 &
"'")
char4 = Int((6 * Rnd) + 1)
char5 = Int((6 * Rnd) + 1)
char6 = Int((6 * Rnd) + 1)

Form_f_Test.txtrslt.Value = ltr1("LTR") & ltr2("LTR") & ltr3("LTR") &
char4 & char5 & char6

End Function
Of course you would want to move the output to where ever work for you.
I hope this helps someone.

You can actually make this a bit simpler, more flexible, and selfcontained:

Function RndPassword(iSize as Integer) As String
Dim strChr As String
Dim iLen As Integer
Dim iPos As Integer
strChr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
' use whatever set of characters you like - mixed case, add punctuation such
' as !#$%^_, etc.
iLen = Len(strChr)
RndPassword = ""
Randomize ' initialize the random number generator
For iPos = 1 to iSize ' generate the requested size password
RndPassword = RndPassword & Mid(strChr, Rnd() * iLen + 1, 1)
Next iPos
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top