A Simple Function To Generate A Random Password Of A Given Set Of Characters
Random Password Generator
' /* In A Code Module Add This: */
Public Function Gen_Rand_Password(PassLength As Integer) As String
' /* Dimension A Variable To Contain The Random Password */
Dim RetVal As String
' /* Dimension Variables To Contain The Upper and Lower Random Boundaries */
Dim Max As Integer
Dim Min As Integer
' /* Set The Limits Of Min and Max To Character Codes Relevant To The Keyboard */
Max = 126
Min = 48
' /* Set The Random Number Generator, Using Timer as the Seed */
' /* This Makes The Psuedo-Random Number A Little More Random */
Randomize Timer
' /* You Can Change This To The Desired Length, 8 is Pretty Much A */
' /* Standard Password Length */
If PassLength < 8 Then
' /* Return From Function With An Error */
Gen_Rand_Password = "error: invalid length"
End If
' /* Loop Until We Reach The Desired Length Of The Password */
For I = 1 To PassLength
' /* Concantenate (append) RetVal with Random Keyboard Character */
RetVal = RetVal & Chr(Int((Max - Min + 1) * Rnd + Min))
Next I
' /* Return That Value To The Calling Procedure */
Gen_Rand_Password = RetVal
End Function
' /* In Your Form (or In Another Procedure) [Call It Like This] */
' /* The Value Passed To The Function (8, In This Case) */
' /* Is The Number Characters That Will Be Returned */
RandomPassword = Gen_Rand_Password(8)
willdove 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.