Discussion Locked

loop and refresh question

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am working on a login program and I have to areas that I am asking assitance with. I am trying to get a loop to work so the user will have only three tries to login. Second I want the form to refresh, If I enter in false info. The code clears the text box but if I enter in correct info on the second try nothing happens below is my code
thanks Dennis

Private Sub cmdLogin_Click()

Dim intTry As Integer

intTry = intTry + 1

Me.adoLogin.Recordset.Find "UserId='" & txtUserId.Text & "'" 'finds the first record

If intTry > 3 Then
End
End If

If txtPassword.Text = lblPass.Caption Then 'check the password
If txtUserId.Text = "Administrator" Then 'is Administratorentered

frmLogin.Caption = " Welcome to my NIGHTMARE" ' change the form caption
Call Load(frmAdmin) 'load the admin form
frmAdmin.Show 'make the form admin visable
Call Unload(frmLogin) 'unload the login form

ElseIf txtPassword.Text = lblPass.Caption Then 'checks the password

frmLogin.Caption = " Welcome to my NIGHTMARE" 'change the caption
Me.Hide 'hides the login form
Call Load(frmWelcome) 'loads the welcome form
Call Unload(frmLogin)
frmWelcome.Show

Else
frmLogin.Caption = "Sorry Charlie, not today" 'caption for unsuccessful attempt

End If

End If

lblTest.Caption = intTry

Me.txtUserId= " " 'Clears the text box
Me.txtPassword = " " 'Clears the text box

End Sub

5 replies

by bill_h

Two comments...

First, since you are declaring intTry within the cmdLogin_Click subroutine, your limit of three login attempts will never be reached. Each time the 'Login' button is clicked, the variable is redimensioned, and set equal to 1; if the login attempt fails, the subroutine is exited and the variable falls out of scope (and the value is lost). You might want to declare intTry as a form variable; then the value could be retained for subsequent login attempts.

Second, the login failures on second attempts may be due to the way you are clearing the txtUserId and txtPassword fields. If you set them equal to " " (a space), then that space will be appended to the beginning of whatever you enter in those fields, and will most likely *never* match the UserId and Password values expected. Try one of the following:

Set the field(s) equal to "" (an empty string)
-or-
Use the Trim$ function when evaluating the field to delete leading and trailing spaces, i.e.,

If Trim$(txtUserId.Text) = "Administrator"...

I favor the Trim$ function because it also eliminates any extra spaces accidently entered by the user (resulting in lower frustration levels for those who are not so nimble-fingered...)

Questions? You knowwhere to reach me.

Hope this helps...

by awilliams3

Off the bat, the declaration of intTry in your procedure as Dim intTry As Integer will kill you quickly. As your variable will be flushed each time the routine is launched.

I recommend changing the declaration to Static intTry As Integer.

This will maintain intTry value between calls to the cmdLogin_Click event procedure. With this change the rest may fall in place for you without any further assistance.

This discussion is closed to new replies.