Problem
MS Access application to create Random Password Generator
Solution
i. Description
This application will
a. Generate random password of maximum length 32767 characters
b. Copy password of any length to clipboard
ii. Procedure
1. Create a form "frmMain"
2. Place all controls as shown below
Control Name Caption
----------- --------- -----------
Label lblRandomPasswordGenerator RANDOM PASSWORD GENERATOR
Label lblNoofCharacters NO. OF CHARACTERS
Text Box txtNoCharacter
Command Button cmdReset RESET
Label lblPassword PASSWORD
Text Box txtpword
Command Button cmdPwdGenerator GENERATE PASSWORD
Command Button cmdCopyPword COPY PASSWORD
3. Select RESET button, right-click then select properties, select Event tab , On Click, select [Event Procedure] from dropdown, click ... button
Copy & paste the following code under click event of cmdReset button
Private Sub cmdReset_Click()
txtpword = Null
txtNoCharacter = Null
End Sub
4. Select GENERATE PASSWORD button, right-click then select properties, select Event tab , On Click, select [Event Procedure] from dropdown, click ... button
Copy & paste the following code under click event of cmdPwdGenerator button
Private Sub cmdPwdGenerator_Click()
On Error GoTo Err_Proc
Dim varCharacter As Integer
varCharacter = Nz(txtNoCharacter.Value, 0)
If IsNull(txtNoCharacter) Or txtNoCharacter.Value = "" Then
MsgBox "Please Enter No. of Characters to generate Password.", vbInformation, "Empty No. of Characters"
txtNoCharacter.SetFocus
Else
txtpword.SetFocus
txtpword.Value = PasswordGenerator(varCharacter)
End If
Exit_Proc:
Exit Sub
Err_Proc:
If err.Number = 6 Then
MsgBox "No. of Characters should be less than 32768.", vbInformation, "No. of characters limit exceeds."
ElseIf err.Number = 13 Then
MsgBox " ' " & txtNoCharacter & " ' is not a number, please enter numeric values only from 1 to 32767."
Else
MsgBox err.Number & ": " & err.Description, _
vbOKOnly + vbCritical
End If
Resume Exit_Proc
End Sub
5. Select COPY PASSWORD button, right-click then select properties, select Event tab , On Click, select [Event Procedure] from dropdown, click ... button
Copy & paste the following code under click event of cmdCopyPword button
Private Sub cmdCopyPword_Click()
If IsNull(txtpword) Or txtpword.Value = "" Then
MsgBox "No Password is available to copy.", vbInformation, "No Password"
Else
With Me!txtpword
.SetFocus
.SelStart = 0
.SelLength = Len(.Value)
End With
DoCmd.RunCommand acCmdCopy
MsgBox "Password Copied Successfully.", vbInformation, "Copied"
End If
End Sub
iii.Code
Option Compare Database
Private Sub cmdCopyPword_Click()
If IsNull(txtpword) Or txtpword.Value = "" Then
MsgBox "No Password is available to copy.", vbInformation, "No Password"
Else
With Me!txtpword
.SetFocus
.SelStart = 0
.SelLength = Len(.Value)
End With
DoCmd.RunCommand acCmdCopy
MsgBox "Password Copied Successfully.", vbInformation, "Copied"
End If
End Sub
Private Sub cmdPwdGenerator_Click()
On Error GoTo Err_Proc
Dim varCharacter As Integer
varCharacter = Nz(txtNoCharacter.Value, 0)
If IsNull(txtNoCharacter) Or txtNoCharacter.Value = "" Then
MsgBox "Please Enter No. of Characters to generate Password.", vbInformation, "Empty No. of Characters"
txtNoCharacter.SetFocus
Else
txtpword.SetFocus
txtpword.Value = PasswordGenerator(varCharacter)
End If
Exit_Proc:
Exit Sub
Err_Proc:
If err.Number = 6 Then
MsgBox "No. of Characters should be less than 32768.", vbInformation, "No. of characters limit exceeds."
ElseIf err.Number = 13 Then
MsgBox " ' " & txtNoCharacter & " ' is not a number, please enter numeric values only from 1 to 32767."
Else
MsgBox err.Number & ": " & err.Description, _
vbOKOnly + vbCritical
End If
Resume Exit_Proc
End Sub
Private Sub cmdReset_Click()
txtpword = Null
txtNoCharacter = Null
End Sub
Private Sub Form_Open(Cancel As Integer)
Call ReSizeForm(Me)
DoCmd.Maximize
txtNoCharacter = Null
txtpword = Null
End Sub
iv. Additional Info
a. No. of characters is the length of random password.
b. Generate Password button generates a random password of length mentioned in No. of characters.
c. Reset button clears the textboxes i.e., No. of characters and Password.
d. Copy Password button copies the random password generated in Password textbox.