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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a hardcoded procedure to a universal library function?

Status
Not open for further replies.

dmtelf

Programmer
Apr 16, 2003
3
0
0
PH
I'm an Access newbie, and would like to make a password entry/validation private subroutine into a re-usable library function but am unsure of the best way to do it.

I have two buttons on a menu form.

btnMaintenance (default state: visible,disabled)
btnEnableMaintenance (default state: visible, enabled)

When the user clicks on btnEnableMaintenance, a form is opened and the user is prompted to enter a password.

If the password is valid, then btnMaintenance is enabled.

After three failed attempts, the password entry form window will close and btnMaintenance remains disabled.

At the moment, the name of the controls and valid password are hardcoded into Password_AfterUpdate()

Ideally, I'd use the new password function with three parameters, like this:

EnableButton(ButtonNameToEnable,ButtonEnable,Password)

where:

1) ButtonNameToEnable is the name of the button to enable (initial state is disabled)
2) ButtonEnable is the name of the button which has to be clicked on to enable ButtonNameToEnable
3) Password is the valid password required to enable ButtonNameToEnable

Any suggestions or constructive criticism gratefully welcomed! :)

Thanks!

DMTelf

<code>
Option Explicit

Dim gOkToClose As Boolean
Dim gintNumberOfPasswordAttempts As Integer

Private Sub Form_Load()

gOkToClose = False
' number of tries
gintNumberOfPasswordAttempts = 1

End Sub

Private Sub Form_Unload(Cancel As Integer)

If Not gOkToClose Then
Cancel = True
End If

End Sub

Private Sub Password_AfterUpdate()

' txtPassword is the name of the text control where the password is input by the user.
If Me![txtPassword] = &quot;p&quot; Then
gOkToClose = True
Forms!frmMenu!btnMaintenance.Enabled = True
Forms!frmMenu!btnMaintenance.SetFocus
Forms!frmMenu!btnEnableMaintenance.Enabled = False

DoCmd.Close A_FORM, &quot;frmPassword&quot;
Else
' give them three shots at getting it right
Select Case gintNumberOfPasswordAttempts
Case 1 To 2
DoCmd.Beep
MsgBox &quot;Incorrect password&quot;, 16, &quot;Password&quot;
gintNumberOfPasswordAttempts = gintNumberOfPasswordAttempts + 1
Case Else
DoCmd.Beep
gOkToClose = True
DoCmd.Close A_FORM, &quot;frmPassword&quot;
End Select
End If

End Sub
</code>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top