Working with Login Userforms

255 pts.
Tags:
Excel 2003
Microsoft Excel
VBA
I've been doing research on Login Userforms and have come up with a code variation similar to coding trend.

However, I am having an issue clearing the previous information, allowing the user to reenter their password before the loop continues. (i.e. the user inputs "password", receives an invalid password message, then reenters their password.)

As the code is written now, the user recieves three succesive messages and the application closes, before they ever get a chance to reenter the password. My code is below...hope my issue is clear. If not, let me know.  THANKS!

 

Private Sub CommandButton1_Click()

UserName = ComboBox1.Value Password = TextBox2.Value Dim intLogonAttempts As Integer

For intLogonAttempts = 0 To 3          If IsNull(UserName) Or UserName = "" Then         MsgBox ("User Name is a required field.")         ComboBox1.SetFocus         Exit Sub     End If           'Check to see if data is entered into the password box          If IsNull(Password) Or Password = "" Then         MsgBox ("Password is a required field.")         TextBox2.SetFocus         Exit Sub     End If           'Check value of password in tblEmployees to see if this matches value chosen in combo box          If UserName = "Administrator" And Password = "password" Then         ...Do This...         ElseIf UserName = "McNitro" And Password =  "password" Then             ...Do That...

            Else             MsgBox ("Password Invalid.  Please Try Again")             TextBox2.SetFocus     End If           'If User Enters incorrect password 3 times database will shutdown             If intLogonAttempts < 3 Then         TextBox.Value = ""

        Else: NoAccessQ = MsgBox("You do not have access to this database.  Please contact your system administrator.", vbCritical, "Access to Access is Restricted!")               Application.Quit               Exit Sub     End If         Next intLogonAttempts

End Sub

ASKED: August 18, 2009  8:52 PM
UPDATED: August 21, 2009  10:14 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

How about instead of using a MsgBox for the Password Invalid line, use an InputBox and have the user put their next attempt at the password in through that?

I think you would use something like

Password = Inputbox(“Password Invalid. Type password here:”)

Then what they type into the InputBox would become the new value of the Password string.

By the way, I think the loop from 0 to 3 will let the user have 4 attempts, instead of 3.

Discuss This Question: 6  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • McNitro
    Code isn't very clear in my question. Thought I would clean it up for you guys/gals. Private Sub CommandButton1_Click() UserName = ComboBox1.Value Password = TextBox2.Value Dim intLogonAttempts As Integer For intLogonAttempts = 0 To 3 'Check to see if data is entered into the username box If IsNull(UserName) Or UserName = "" Then MsgBox ("User Name is a required field.") ComboBox1.SetFocus Exit Sub End If 'Check to see if data is entered into the password box If IsNull(Password) Or Password = "" Then MsgBox ("Password is a required field.") TextBox2.SetFocus Exit Sub End If 'Check to see if UserName and Password entered match the created variables If UserName = "Administrator" And Password = "password" Then ...Do This... ElseIf UserName = "McNitro" And Password = "password" Then ...Do That... Else: MsgBox ("Password Invalid. Please Try Again") TextBox2.SetFocus End If 'If User Enters incorrect password 3 times database will shutdown If intLogonAttempts < 3 Then TextBox.Value = "" Else: NoAccessQ = MsgBox("You do not have access to this database. Please contact your system _ administrator.", vbCritical, "Access is Restricted!") Application.Quit Exit Sub End If Next intLogonAttempts End Sub
    255 pointsBadges:
    report
  • McNitro
    I have thought about the InputBox solution, but in my opinion it's an "ugly" solution to the problem. There are plenty of login screens out there that allow the user to re-enter the password into the same TextBox they previously used. Functionality is great, but can't functionality meet aesthetics??? As far as the number of loops, the code works fine for 3 attempts. The user is allowed to attempt acces when intLogonAttempts is less than 3 (0, 1 or 2), and exits when the variable equals three.
    255 pointsBadges:
    report
  • tlsanders1
    OK, I think the underlying problem is that when you set the focus back to that TextBox2, there's nothing to make the program stop running and wait for a new entry. You could try adding a Stop statement right after the TextBox2.SetFocus. Then in TextBox2.OnExit set password = TextBox2.Value again, so that after the new password is typed in and something is done for the TextBox2 to be exited (Enter or Tab), the program can pick back up in the For ... Next loop with the new password.
    1,340 pointsBadges:
    report
  • McNitro
    I played around with the STOP statment idea, but I couldn't get that to work either. The .OnExit statement you refer to doesn't seem to be available in Excel 2003 for use with a TextBox. What I have ended up doing is quite possibly a worse idea than going the InputBox route. I created two new UserForm and wrote the code to cycle through all three as needed. This helps with the aethetics of my login form, but is also an "ugly" solution! If anyone can help me to figure this out, I would appreciate it! But for now, the modules are at least working the way I would like them to.
    255 pointsBadges:
    report
  • tlsanders1
    I've been throwing out ideas without testing them. This time I tested first. I think you need to get rid of the For ... Next Loop. It's just spinning right through it without stopping. In Private Sub UserForm_Activate(), I initilize the password counter to 0. In Private Sub TextBox1_Exit, I set password = TextBox1.Value. In Private Sub CommandButton1_Click(), first I increment the password counter i = i + 1. Then in the password test IF...THEN, if the password is not correct, check the counter to see if it has reached 3, and if not then clean out the textbox, TextBox1.Value = "" and set the focus back with TextBox1.SetFocus. Then the program can fall through to the end of the CommandButton routine and wait for a new value in TextBox1 and a new click on the CommandButton.
    1,340 pointsBadges:
    report
  • McNitro
    PERFECT! That works great! Thank you for your help. I didn't consider trying to use the event procedures for the other controls in the userform. Now I can get rid of the extra forms, and the result is not only functional, but aesthetically pleasing as well. Consider this SOLVED! Thanks again!
    255 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following