Generate a Random Password - November 1999

A recent news group posting asked if anyone had written code to generate a password. So I sent some code I developed in 1996. After thinking about it, I decided to update the code, put it in a class module and create a demo program. So this becomes the tip-of-the-month.

2009/11/07 - I converted the code to support VB.Net VS2005. Click here to view that tip.

The Demo Program

I wrote a VB6 demo program to show how the class module works. The demo program:

Generate Password Demo

Using the Demo

Pick From - The password is generated using the 'pick from' string. No character is repeated - the password contains a character once and only once - no duplicates. The class module removes duplicate characters and eliminates spaces. The 'pick from' list needs to be longer than the password length. You can pass a 'pick from' list to the generator or use the default shown above.

Password Length - The length of the password to be generated - eight characters in this example. Maximum is the length of the 'pick from' string. If you enter a value larger than the 'pick from' string's length the value is reduced to the length of the 'pick from' string.

Password - The generated password is shown in a combo box. For example:

Generate a Password - Click this button to generate a password.

Include in Your Code

Include class module 'clsGeneratePassword.cls' in your application. Sample code for creating a password:

Private Sub DoGeneratePassword()
  Dim objGeneratePassword As New clsGenPassword
  With objGeneratePassword
    If IsNumeric(Text2.Text) Then
      .lLength = Text2.Text
    End If
    If Len(Trim$(Text1(0).Text)) > 0 Then
      .sPickFrom = Trim$(Text1(0).Text)
    End If
    Combo1.AddItem .GeneratePassword
    Combo1.ListIndex = Combo1.ListCount - 1
    Text1(0).Text = .sPickFrom 'used this to create the password
    Text2.Text = .lMaxLength   'used this
  End With
  Set objGeneratePassword = Nothing
End Sub

The Demo Uses modConnectToWebSite

The demo program uses modConnectToWebSite.bas to display this page. For details on the use of that module click here.

Download VB Code

Click here to download the VB 6 Source Code [7k].

Previous Tip   Tip Index   Next Tip


Updated 2009/11/07