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!

Log-In Screen Code Won't Work 1

Status
Not open for further replies.

MsBecca

Programmer
Oct 21, 2003
7
0
0
US
Can someone review my code and see why my Log-In userform code will not work properly? I can't get the Log-In form to redisplay each time the user inputs the wrong USERNAME and PASSWORD. After the inital log-in attempt, code loops two more time, but never re-displays the log-in form for the second and third attempts.

I'm sure it's something very simple I'm missing, but I can't figure it out. Please help.

frmLogIn - Name of the form that loads when the workbook opens.
frmEmployee - Form that is displayed after correct log-in information is entered.


Private Sub cmdLogIn_Click()

Dim intCounter As Integer
Dim x As Integer
x = 1
intCounter = 3

Do Until x > intCounter
If txtUserName <> &quot;Admin&quot; And txtPassword <> &quot;admin&quot; Then
MsgBox &quot;Invalid UserName or Password. Please enter valid log-in information&quot;, _
vbCritical, &quot;Invalid Log-In: Attempt #&quot; & x
x = x + 1
If x > intCounter Then
MsgBox &quot;Three Unsuccessful Log-In Attempts.&quot; + vbNewLine + _
&quot;Contact System Adminstrator for Password Reset&quot;, vbCritical, &quot;Invalid Log-In&quot;
Unload Me
Exit Sub
Else
Me.txtUserName = &quot;&quot;
Me.txtPassword = &quot;&quot;
Me.txtUserName.SetFocus
End If
Else
MsgBox &quot;Log-In Successful&quot;, , &quot;Successful Log-In&quot;
Unload Me
frmEmployee.Show
End If
Loop

End Sub

Tried putting the statement EXIT SUB after the me.txtUserName.SetFocus in the nested If Statement, and it still does not work.
 
I'm not entirely sure why it doesn't work, but I'd get rid of the loop, instead keeping track of the number of attempts in a module-level variable, e.g.

dim Attempts as integer

Private Sub cmdLogIn_Click()
Attempts=Attempts+1
If txtUserName <> &quot;Admin&quot; And txtPassword <> &quot;admin&quot; Then
MsgBox &quot;Invalid UserName or Password. Please enter valid log-in information&quot;, _
vbCritical, &quot;Invalid Log-In: Attempt #&quot; & Attempts
If Attempts = 3 Then
MsgBox &quot;Three Unsuccessful Log-In Attempts.&quot; + vbNewLine + _
&quot;Contact System Adminstrator for Password Reset&quot;, vbCritical, &quot;Invalid Log-In&quot;
Unload Me
Else
Me.txtUserName = &quot;&quot;
Me.txtPassword = &quot;&quot;
Me.txtUserName.SetFocus
End If
Else
MsgBox &quot;Log-In Successful&quot;, , &quot;Successful Log-In&quot;
Unload Me
frmEmployee.Show
End If
Loop
End Sub

sub Userform_Initialize
Attempts=0
end sub


Rob
[flowerface]
 
MsBecca:

If the userform disappears when you press the 'Login' button then you have an 'unload me' or 'me.hide' somewhere in the code that checks for a valid username and password. You'll need to use the show method to make the form visible again.

One thing I would recommend would be to refrain from 'unloading' a form while your still running code from it. When you use the 'unload me' statement the object's terminate event is called and the object is removed from memory (At least it's reference is released.) and that includes the code associated with that object. There's no guarantee that it will be there when you get back from the employee form.

Ron

 
Thanks RobBroekhuis and TheBitDoctor for responding and helping me resolve my coding problem.
Your help was much appreciated. STARS to both of you.
MsBecca
 
Hello MsBecca,

I have created a similar login form like Rob. But, I also would like to allow the user to change the password by creating a new form for changing password.

The new form is called &quot;ChangePassword&quot; with three textboxes: txtoldpsswd; txtnewpsswd; and txtconfirmnewpsswd, and a button call &quot;Change&quot;.

I would like to replace the existing password in &quot;Login&quot; form with the new password value in the &quot;txtnewpsswd&quot; in the &quot;ChangePassword&quot; form. For example, instead of entering &quot;admin&quot; the next time I open up the &quot;Login&quot; form I would enter something like &quot;newpsswd&quot;.

I included the codes for your review.

Private Sub Change_Click()
If Login.txtpsswd.text=ChangePassword.txtoldpsswd then
If txtnewpsswd.text=txtconfirmnewpsswd.text then
ChangePassword.txtnewpssd.text=Login.txtpsswd.text
Else
msgbox(&quot;Please check your new password&quot;)
End if
Else
msgbox(&quot;Please enter the correct password&quot;)
End If
End Sub

However, the new password did not replace the existing password after I clicked the change button. Can you please tell me what I did wrong? Thank you for your time and help in advance.

DTiff
 
Sorry for getting Becca and Rob names confused. I look forward to hear from one or both of you. Thank you in advance for your help.

DDTiff
 
MsBecca, IMHO your validation code is wrong:
If txtUserName <> &quot;Admin&quot; And txtPassword <> &quot;admin&quot; Then
instead of:
If txtUserName <> &quot;Admin&quot; Or txtPassword <> &quot;admin&quot; Then



Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top