help for password code VBA

  • Thread starter Thread starter Patrick Bastard
  • Start date Start date
P

Patrick Bastard

Hi,

I try to have 3 different passwords for 3 different users, to allow them 3
different functions.

I'd be happy if someone here could tell me why this code doesn't work, and
how to correct it.

Thanks in advance.

Patrick.

Sub TestPass()
Dim okPswd As Boolean
Dim pass(3) As String
pass(0) = "tata"
pass(1) = "titi"
pass(2) = "toto"
okPswd = GetPassword(pass(3), 3)

' MsgBox okPswd

If okPswd ="tata" Then

'**********************************************************************
'TEST CODE 1
Range("C2:H2").Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
'****************************************
elsIf okPswd ="titi"Then

'**********************************************************************
'TEST CODE 2
Range("C5:H5").Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
'****************************************
elsIf okPswd ="toto"Then

'**********************************************************************
'TEST CODE 3
Range("C8:H8").Select
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
'****************************************

Else
'do something else that you want!
End If

End Sub

Function GetPassword(CorrectPswd As String, MaxTimes As Long) As Boolean
'Dave Peterson, mpep
Dim Pswd As String
Dim essais As Long

essais = 1
GetPassword = False

Do
Pswd = InputBox("Essai n°" & essais & " sur " & MaxTimes _
& ". Mot de passe?", _
"Saisie du mot de passe")

If Pswd = "" Then
Exit Function
End If

If Pswd <> CorrectPswd Then
essais = essais + 1
If essais <= MaxTimes Then
MsgBox "Erreur !"
End If
Else
GetPassword = True
'MsgBox "Mot de passe correct !"
Exit Function
End If
Loop Until essais > MaxTimes

End Function
 
Well, since you didn't say what "doesn't work" means, it's hard to know
what you expect, but just looking at it...

1) You pass the variable pass(3) to GetPassword's CorrectPswd argument.
Since you didn't assign any value to pass(3), CorrectPswd will be
assigned a null string. None of your passwords are null strings, so
GetPassword will always return False.

2) okPswd is declared as a boolean variable, but you perform a string
comparison on it:

If okPswd = "tata"

That will give you a Type Mismatch error.

3) You've misspelled "ElseIf" - that should have given you a syntax
error.
 
I'm not sure how to correct it to make it do what you want, but this does
something!

First,
okPswd is a boolean variable. It returns True or False. True if the user got
the password correct and false if they failed.

So you can't test "If okPswd ="tata" Then". But you can test if they got it
with this:

if okpswd = true then
...

And you sent pass(3) to the function.
Dim Pass(3) as string
means you have 4 elements:
pass(0), Pass(1), Pass(2) and Pass(3)

And since you never changed pass(3) from it's initial value (an empty string
""), then this line in the function:

If Pswd = "" Then
Exit Function
End If

Would always just return a False back to your calling routine.

I wasn't sure how you know which password to test, so I created another variable
(WhichPwd) that I could use in this line:

okPswd = GetPassword(pass(WhichPwd), 3)

And if the password were matched correctly, I could test it later:

Select Case WhichPwd

I changed your if/then/elseif/else statement to "select case" (personal
preference only).

I find when I stack too many things in if/then/elseif/elseif/elseif/else, I get
confused. The "select case" is easier to read later (well, I think so).



Option Explicit
Sub TestPass()
Dim WhichPwd As Long
Dim okPswd As Boolean
Dim pass(3) As String
'pass(3) means you have 4 elements 0, 1, 2, 3
'did you mean to add the fourth or make it
'dim pass(2) as string
pass(0) = "tata"
pass(1) = "titi"
pass(2) = "toto"
pass(3) = "tutu"

WhichPwd = 2
okPswd = GetPassword(pass(WhichPwd), 3)

If okPswd = True Then
'MsgBox "User got it ok"
Else
MsgBox "Try again when you have the correct password"
Exit Sub '<-- don't let them continue
End If

With ActiveSheet
Select Case WhichPwd
Case Is = 0
With .Range("C2:H2").Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Case Is = 1
With .Range("C5:H5").Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Case Is = 2
With .Range("C8:H8").Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Case Else
'do something else that you want!
End Select
End With

End Sub

Function GetPassword(CorrectPswd As String, MaxTimes As Long) As Boolean

Dim Pswd As String
Dim essais As Long

essais = 1
GetPassword = False

Do
Pswd = InputBox("Essai n°" & essais & " sur " & MaxTimes _
& ". Mot de passe?", _
"Saisie du mot de passe")

If Pswd = "" Then
Exit Function
End If

If Pswd <> CorrectPswd Then
essais = essais + 1
If essais <= MaxTimes Then
MsgBox "Erreur !"
End If
Else
GetPassword = True
'MsgBox "Mot de passe correct !"
Exit Function
End If
Loop Until essais > MaxTimes

End Function
 
Hi, *JE McGimpsey*.

Thank you for these explanations.
I begin in VBA, and try to understand and accomodate.

Best regards.
P. Bastard.
 
Hi, Dave

Many thanks for all these explanations, Dave.
I am a learner in VBA, and I try to understand and accomodate existing code.
With your comments, it'is clearer for me.

Best regards.
P. Bastard.
 
Back
Top