Password redundant through use of cancel
You haven't "included the necessary code" since you didn't include the
code in which the user inputs the password.
Just guessing, therefore, but one way might be:
Const sMSG = "Password Incorrect." & vbNewLine & vbNewLine & _
"Please check caps lock is off. Click Retry to try again."
Dim vResponse As Variant
Dim nButton As Long
Application.ScreenUpdating = False
With Workbooks("Training Log.xls").Worksheets("Menu")
Do
vResponse = Application.InputBox( _
Prompt:="Input Password:", _
Title:="Password", _
Default:="", _
Type:=2)
If vResponse = False Then Exit Sub 'User cancelled
On Error Resume Next
.Unprotect Password:=vResponse
On Error GoTo 0
If .ProtectContents Then
nButton = MsgBox(sMSG, Buttons:=vbRetryCancel)
If nButton = vbCancel Then Exit Sub
End If
Loop Until .ProtectContents = False
.Visible = True
Application.Goto .Range("A1"), Scroll:=True
With .Parent.Sheets("AddTraining")
.Visible = True
Application.Goto .Range("C26"), Scroll:=False
End With
End With
Application.ScreenUpdating = True
In article ,
"Blokeyfella" wrote:
I have written an application that uses passwords to prevent unauthorised
users from changing data. This functions in the normal way i.e. by displaying
an input box for the password with OK and Cancel buttons.
When no password or an incorrect password is entered, an error message is
displayed on screen (msgbox "Incorrect. Please check caps lock is off and try
again"). However when Cancel is selected, rather than show this error
message, the macro ticks through as though the correct password has been
entered.
This is doubly frustrating as another application I have written does all of
the above but when cancel is pressed, pulls up the error message (as
required).
I have included the necessary code below, the version is MS Excel 2000.
PLEASE help me to understand why one of my applications is operating
differently (and wrongly) to the other. Thanks.
The code:
Windows("Training Log.xls").Activate
Application.ScreenUpdating = False
Sheets("Menu").Select
On Error GoTo TPassword_Err
ActiveSheet.Unprotect
Range("A1").Select
Sheets("AddTraining").Visible = True
Sheets("Menu").Visible = False
Sheets("AddTraining").Select
Application.ScreenUpdating = True
Range("C26").Select
Exit Sub
TPassword_Err:
MsgBox "Password Incorrect." & vbCrLf & vbCrLf _
& "Please check caps lock is off. Click on the Continue button to try
again"
Application.ScreenUpdating = True
end sub
|