Generating random string

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I need to generate a string of six random characters to act as the first
password. Is there an example of how to do this somewhere?

Thanks

Regards
 
John said:
Hi

I need to generate a string of six random characters to act as the
first password. Is there an example of how to do this somewhere?

Here's a quickie function I just threw together. I haven't tested it
thouroughly, but it seems to work.

'----- start of code -----
Function RandomString(LengthNeeded As Integer) As String

Static blnRandomized As Boolean

Dim strOut As String
Dim I As Integer
Dim intChar As Integer

Const conSourceChars As String = _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"0123456789"

If Not blnRandomized Then
Randomize
blnRandomized = True
End If

For I = 1 To LengthNeeded
strOut = strOut & _
Mid(conSourceChars, _
1 + Int((Rnd() * Len(conSourceChars))), _
1)
Next I

RandomString = strOut

End Function
'----- end of code -----

You'd call it like this:

strNewPassword = RandomString(6)

Notice that I've assumed that all characters must be drawn from the set
of letters or digits. You can add to that list by modifying the
constant conSourceChars.
 
Many thanks. What do you know, it works! :)

Dirk Goldgar said:
Here's a quickie function I just threw together. I haven't tested it
thouroughly, but it seems to work.

'----- start of code -----
Function RandomString(LengthNeeded As Integer) As String

Static blnRandomized As Boolean

Dim strOut As String
Dim I As Integer
Dim intChar As Integer

Const conSourceChars As String = _
"abcdefghijklmnopqrstuvwxyz" & _
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"0123456789"

If Not blnRandomized Then
Randomize
blnRandomized = True
End If

For I = 1 To LengthNeeded
strOut = strOut & _
Mid(conSourceChars, _
1 + Int((Rnd() * Len(conSourceChars))), _
1)
Next I

RandomString = strOut

End Function
'----- end of code -----

You'd call it like this:

strNewPassword = RandomString(6)

Notice that I've assumed that all characters must be drawn from the set
of letters or digits. You can add to that list by modifying the
constant conSourceChars.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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

Back
Top