Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Password Generator

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
US
I'm looking for a password generator I can use in access so I can click a button and it will generate a password in the length I specify for upper/lower case, numer and special characters like []{}, etc..

I found one that will do this without the special characters. Can anyone help me to add the special characters. my code is below:

Code:
Public Function PasswordGenerator(ByVal lngLength As Long) _
  As String

' Description: Generate a random password of 'user input' length
' Parameters : lngLength - the length of the password to be
                'generated
' Returns    : String    - Randomly generated password
  
On Error GoTo Err_Proc
  
 Dim iChr As Integer
 Dim c As Long
 Dim strResult As String
 Dim iAsc As String
 
 Randomize Timer

 For c = 1 To lngLength
   
   ' Randomly decide what set of ASCII chars we will use
   iAsc = Int(3 * Rnd + 1)
   
    'Randomly pick a char from the random set
   Select Case iAsc
     Case 1
       iChr = Int((Asc("Z") - Asc("A") + 1) * Rnd + Asc("A"))
     Case 2
       iChr = Int((Asc("z") - Asc("a") + 1) * Rnd + Asc("a"))
     Case 3
       iChr = Int((Asc("9") - Asc("0") + 1) * Rnd + Asc("0"))
     Case Else
       Err.Raise 20000, , "PasswordGenerator has a problem."
   End Select
   
   strResult = strResult & Chr(iChr)
 
 Next c
 
 PasswordGenerator = strResult
 
Exit_Proc:
 Exit Function
 
Err_Proc:
 MsgBox Err.Number & ": " & Err.DESCRIPTION, _
    vbOKOnly + vbCritical
 PasswordGenerator = vbNullString
 Resume Exit_Proc
 
End Function
------------------------------------------------------------
Private Sub cmdpwdupdate_Click()
txtpword.SetFocus
txtpword.Text = PasswordGenerator(8)
End Sub
 
Looks like all you need to do is change the 3 to a 4 in this line:
iAsc = Int(3 * Rnd + 1)


then add another Case statement for the 4.


Then just use the same line of code as the others, but put in the lower and upper bounds of the character numbers you want from the character set.

This is from MS re: RND function:
Code:
To produce random integers in a given range, use this formula:

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

In MS Access help, search on the CHR function and pick OTHER TOPICS and look at the Character Sets.


Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
great..

Also, how could I copy the generated password to the clipboard.
 
Add two lines to the button code:

txtpword.SetFocus
txtpword.Text = PasswordGenerator(8)
txtpword.SelLength = Len(txtpword)
DoCmd.RunCommand acCmdCopy



Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top