Generate Random Password
In the course of programming you may have cause to generate a password. The following function will generate a password of randomly selected characters up to a maximum of 10 characters (this is easy to increase). The number of characters required is passed as a parameter.
Using this type of procedure as opposed to chosing your own passwords makes for better security.
-
'Form code - frmPasswordGenerate
-
-
Private Declare Function GetTickCount Lib "kernel32" () As Long
-
-
Public Function PassGen(nLen As Integer)
-
Dim range As Collection
-
Dim ivalue, icount, iLen As Long
-
Dim pass As String
-
-
Set range = New Collection
-
range.Add ("0")
-
range.Add ("1")
-
range.Add ("2")
-
range.Add ("3")
-
range.Add ("4")
-
range.Add ("5")
-
range.Add ("6")
-
range.Add ("7")
-
range.Add ("8")
-
range.Add ("9")
-
-
icount = 0
-
ivalue = 0
-
iLen = range.Count
-
-
Do Until icount = nLen
-
Randomize
-
ivalue = CByte(Mid(CStr(Rnd(GetTickCount)), 3, 2))
-
If ivalue > 0 And ivalue <= iLen Then
-
icount = icount + 1
-
pass = pass & range(ivalue)
-
End If
-
Loop
-
-
PassGen = pass
-
End Function
-
-
Private Sub cmdGeneratePassword_Click()
-
MsgBox PassGen(8)
-
End Sub
-