Tuesday, June 24, 2008

Generate Password

This is a simple digit only password generator that I use for a number of purposes. My recent post about managing automation objects uses this function and I forgot to include it there - I have updated that post but though I would also post it separately. I adapted this from some code I got somewhere, my apologies for not acknowledging the source.


Public Function GeneratePassword( _
ByVal intLength As Integer) As String

' Generates a random string of digits of the requested length

' In:
' intLength - number of digits to be returned (max 9)
' Out:
' Return Value - a random string of digits
' Example:
' GetPassword(3) = "927"

Dim lngHighNumber As Long
Dim lngLowNumber As Long
Dim lngRndNumber As Long

' Check we don't exceed our maximum range
If intLength > 9 Or intLength < 1 Then
Err.Raise 5, "GetPassword", _
"Invalid string length - must be between 1 and 9"
Else
' Work out the numbers
lngLowNumber = 10 ^ (intLength - 1)
lngHighNumber = (10 ^ intLength) - 1
' Generate a new seed and a new random number
Randomize
lngRndNumber = Int((lngHighNumber - lngLowNumber + 1) * Rnd) + lngLowNumber
' Format the result as string
GeneratePassword = Format$(lngRndNumber, String$(intLength, "0"))
End If
End Function

1 comment:

Unknown said...

Hi Wazza,
heres my "password generator". It generates a string composed of characters 0-9, a-z or A-Z.
Enjoy :-)

'******************************************************************************
'* Procedure : GenerateRandomString (Function)
'* Author : Peder Schmedling
'* Purpose :
'* This function returns a string containing a random set of the characters
'* [0-9], [A-Z] or [a-z] with the length specified by the input parameter. If
'* the desired length is > 1 the function calls itself recursivly.
'* Parameters:
'* [In] lngLength, desired length of the Ascii string
'* Return :
'* String, the generated random string
'******************************************************************************
Public Function GenerateRandomString(ByVal lngLength As Long) As String
On Error GoTo ErrorHandler

'Declaration: number in range 0–255
Dim byteRandVal As Byte
Dim strChar As String

'Validate input
lngLength = Abs(lngLength)
If lngLength = 0 Then GoTo ExitProc

'Function is volatile if used as UDF
Application.Volatile

'Initialize the random-number generator
Randomize: DoEvents

'Generate a new random number in the range 0-61
byteRandVal = Int(Rnd * 61)

'Call the function recursivly if we are generating more than one character
If lngLength > 1 Then
strChar = GenerateRandomString(lngLength - 1)
End If

'Three groups of Ascii charaters generated, 0-9, a-z and A-Z
Select Case byteRandVal
Case 0 To 9
strChar = strChar & Chr(byteRandVal + 48)
Case 10 To 35
strChar = strChar & Chr(byteRandVal + 55)
Case 36 To 61
strChar = strChar & Chr(byteRandVal + 61)
End Select

'Return the string
GenerateRandomString = strChar
ExitProc:
Exit Function
ErrorHandler:
GenerateRandomString = ""
Resume ExitProc
End Function