Help- VBA Code for passwording a form - Retries

Ar3on

New member
Local time
Today, 11:45
Joined
Sep 9, 2010
Messages
2
Hey can you help me simplify this code to a for loop counter in VBA please

Private Sub Form_Open(Cancel As Integer)
Dim stDocName As String
Dim stLinkCriteria As String
Dim PassWordtxt
Dim BadResult

PassWordtxt = InputBox("Please Type in password you have a maxium of 3 tries")
If PassWordtxt = "password" Then
stDocName = "Add/Edit frm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
MsgBox "The password provided was incorrect - you do not have permission"
PassWordtxt = InputBox("Please Type in password you have a 2 tries")
If PassWordtxt = "password" Then
stDocName = "Add/Edit frm"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Else
MsgBox "The password provided was incorrect - you do not have permission"
PassWordtxt = InputBox("Please Type in password you have a 1 try")
If PassWordtxt = "password" Then
stDocName = "Add/Edit frm"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
BadResult = MsgBox("This application will closedown due to 3 wrong entries")
Application.Quit
End If
End If
End If
End Sub

The code works fine as it is but would like it to be compact and take less load time to run.
 
Please use the code tags which makes it easier to read your code.

This should give you an idea on how to do it...

Code:
Option Compare Database
Option Explicit

Dim bCounter As Byte

Code:
bCounter = bCounter + 1

If UCase$(Me.tbPassword.Value) <> UCase$(gcPassword) Then
    If bCounter < 3 Then
        Me.tbPassword.Value = ""
        Me.tbPassword.SetFocus
        Exit Sub
    Else
        Application.Quit acQuitSaveNone
    End If
Else
    DoCmd.Close acForm, "fMainMenu"
End If
 
Please use the code tags which makes it easier to read your code.

This should give you an idea on how to do it...

Code:
Option Compare Database
Option Explicit

Dim bCounter As Byte

Code:
bCounter = bCounter + 1

If UCase$(Me.tbPassword.Value) <> UCase$(gcPassword) Then
    If bCounter < 3 Then
        Me.tbPassword.Value = ""
        Me.tbPassword.SetFocus
        Exit Sub
    Else
        Application.Quit acQuitSaveNone
    End If
Else
    DoCmd.Close acForm, "fMainMenu"
End If
For the code you provided for me what is it ment for Macro builder, Expression builder or Code builder?? As i am a Noivce i attempted to copy and past the code over the existing code and it sprung up a error (expression onopen error). As my intentions for this event is for the user to simple open a form and a input box will pop up and give them three tries to enter a passworf and either lets them in or kicks them out.
 

Users who are viewing this thread

Back
Top Bottom