By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,663 Members | 1,867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,663 IT Pros & Developers. It's quick & easy.

creating a password, with three attempts allowed.

P: 49
Hello All,

I am creating a system and would like to place a password when the system is open on the main form. i would like to be a code in vba that could be entered and should only allow three attempts before closing the form.

Any ideas?

Much Appreciated
Jan 26 '08 #1
Share this Question
Share on Google+
18 Replies


P: 32
in your form place a filed with visibilty set to no with a default of 0 call it whateve in this case fldAttempt. in your onclick event for the password check if the password is incorrect then

Expand|Select|Wrap|Line Numbers
  1. if me.fldAttempt > 2 then
  2.    docmd.quit
  3. else
  4.    me.fldAttempt = me.fldAttempt +1
  5. end if
you could also do it with a global varible.

Hello All,

I am creating a system and would like to place a password when the system is open on the main form. i would like to be a code in vba that could be entered and should only allow three attempts before closing the form.

Any ideas?

Much Appreciated
Jan 26 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
Here's a routine for doing this.

Table name
tblUsers

Table Fields
User_Name (Text datatype)
User_Password (Text datatype)

Form name
LogInForm

Form Controls
txtUserName
txtPassword
cmdSubmitButton

Expand|Select|Wrap|Line Numbers
  1. Private Static Sub cmdSubmitButton_Click()
  2.   Dim Attempts As Integer
  3.  
  4.  If Me.txtPassword.Value = DLookup("[User_Password]", "tblUser", "[User_Name]='" & Me.txtUserName.Value & "'") Then
  5.    'Continue to open database
  6.  Else
  7.   MsgBox "User Name and or Password Are Incorrect!", vbCritical, "Warning"
  8.   Attempts = Attempts + 1
  9.    Me.txtUserName.SetFocus
  10.     If Attempts = 3 Then
  11.       DoCmd.Close acForm, "LogInForm"
  12.     End If
  13.  End If
  14. End Sub
  15.  
Welcome to TheScripts!

Linq ;0)>
Jan 26 '08 #3

P: 49
Here's a routine for doing this.

Table name
tblUsers

Table Fields
User_Name (Text datatype)
User_Password (Text datatype)

Form name
LogInForm

Form Controls
txtUserName
txtPassword
cmdSubmitButton

Expand|Select|Wrap|Line Numbers
  1. Private Static Sub cmdSubmitButton_Click()
  2.   Dim Attempts As Integer
  3.  
  4.  If Me.txtPassword.Value = DLookup("[User_Password]", "tblUser", "[User_Name]='" & Me.txtUserName.Value & "'") Then
  5.    'Continue to open database
  6.  Else
  7.   MsgBox "User Name and or Password Are Incorrect!", vbCritical, "Warning"
  8.   Attempts = Attempts + 1
  9.    Me.txtUserName.SetFocus
  10.     If Attempts = 3 Then
  11.       DoCmd.Close acForm, "LogInForm"
  12.     End If
  13.  End If
  14. End Sub
  15.  
Welcome to TheScripts!

Linq ;0)>
Thats, this works, but after 3 attempts the form does not close itself???
would you have any idea
Jan 26 '08 #4

P: 49
Here's a routine for doing this.

Table name
tblUsers

Table Fields
User_Name (Text datatype)
User_Password (Text datatype)

Form name
LogInForm

Form Controls
txtUserName
txtPassword
cmdSubmitButton

Expand|Select|Wrap|Line Numbers
  1. Private Static Sub cmdSubmitButton_Click()
  2.   Dim Attempts As Integer
  3.  
  4.  If Me.txtPassword.Value = DLookup("[User_Password]", "tblUser", "[User_Name]='" & Me.txtUserName.Value & "'") Then
  5.    'Continue to open database
  6.  Else
  7.   MsgBox "User Name and or Password Are Incorrect!", vbCritical, "Warning"
  8.   Attempts = Attempts + 1
  9.    Me.txtUserName.SetFocus
  10.     If Attempts = 3 Then
  11.       DoCmd.Close acForm, "LogInForm"
  12.     End If
  13.  End If
  14. End Sub
  15.  
Welcome to TheScripts!

Linq ;0)>
Thats great!!! it works a beauty!!!
Jan 26 '08 #5

missinglinq
Expert 2.5K+
P: 3,532
Glad we could help!

Linq ;0)>
Jan 27 '08 #6

P: 49
Glad we could help!

Linq ;0)>
is there a way to turn the letters entered into stars so it is not possible to see what is being entered into the system? ****
Jan 28 '08 #7

jaxjagfan
Expert 100+
P: 254
is there a way to turn the letters entered into stars so it is not possible to see what is being entered into the system? ****
Set the Input mask to the field as PASSWORD.
Jan 28 '08 #8

P: 49
Set the Input mask to the field as PASSWORD.
thanks works a treat!!!
Jan 28 '08 #9

NeoPa
Expert Mod 15k+
P: 31,712
Thats, this works, but after 3 attempts the form does not close itself???
would you have any idea
Thats great!!! it works a beauty!!!
Curious as to what happened in-between.

My reading of the code is that the Dim line should be changed to Static if the count is to persist between calls (clicks of the Command Button). An alternative would be to define it outside of the procedure (as Private).
Feb 2 '08 #10

missinglinq
Expert 2.5K+
P: 3,532
I hadn't used Static in ages, and had to go to help for a refresher. I was thinking it was part of the Dim statement, too, but that's not true, at least not in ACC2003. Declaring the sub Static persists the value of any variables declared within the sub, as long as the form is open.

As to what happened between # 4 and # 5, I call it the NIDYID effect! (No It Doesn't, Yes It Does!) When I know the code is valid, I just wait. The OP usually gets it sussed out. Probably a typo!

Linq ;0)>
Feb 2 '08 #11

NeoPa
Expert Mod 15k+
P: 31,712
Nice one Link ;)
I didn't even know (or I suppose I must have known at some time - having been through help for such things a few times - but certainly didn't remember even when reminded) that a procedure could also be defined as static, and what effect that had on the internally DIMmed variables. I think I may even prefer your solution :)

PS. I've just reread your posts and saw a possible reason why I missed that part of the first line. Yellow on white is hardly the best contrast factor available - hence I never use [ CODE = VB ] in my posts (it's optional of course). You lose more than you gain for me.
Feb 2 '08 #12

missinglinq
Expert 2.5K+
P: 3,532
I always assumed "Please use code tags" meant "Please use appropriate code tags!" I agree a wiser choice of colors could have been made! The colors aren't bad on my laptop screen, but on my secondary monitor, an elderly HP M90, they're somewhat faded. And, of course, sad to say, I'm forced to use my secondary monitor most of the time.

Linq ;0)>
Feb 2 '08 #13

NeoPa
Expert Mod 15k+
P: 31,712
No, no.
We don't choose to enforce the basic ones as some love what the targetted ones can do. My personal preference is for the basic ones, but I regret losing the benefit of the highlighting of particular sections. Bear in mind too, that the parsing is not perfect and some items are incorrectly coloured.
Feb 2 '08 #14

P: 49
Hello All,

The code above works fine, Is there a way to attach a button incase the employee was to forget their password,

I am currently trying to have a button on my home form saying 'forgot password'
When this is pressed a list of questions appear within a combo box and when an answer has been entered, they are logged into the menu form?

Regards
Feb 15 '08 #15

NeoPa
Expert Mod 15k+
P: 31,712
Yes there would be.
Try to do some of this on your own before coming back for someone to do it for you though.
It's easier if we now how far you've got - and simply asking for it all to be done for you is not going to help you at all (which is what this site is about).

We can help if you get into difficulties of course.
Feb 15 '08 #16

P: 10
Yes there would be.
Try to do some of this on your own before coming back for someone to do it for you though.
It's easier if we now how far you've got - and simply asking for it all to be done for you is not going to help you at all (which is what this site is about).

We can help if you get into difficulties of course.

I am trying to use this piece of code in my own database, but I am a bit confused!! The code runs fine, but the login form I have created automatically defaults to a user name and displays the password (masked with ****). How do I get it so that the form opens blank and allows users to enter their names and passwords each time?

If I then delete the pre entered data, it deletes it from the undelying table tblUsers.

Sorry if I am being thick, but I have been messing with this for a couple of hours and it's doing my head in!!!
Feb 15 '08 #17

P: 10
Ignore me, I was being thick!! Have solved it - I had forced my password box to be referenced to the underlying table, so no wonder it kept deleting it.
Feb 15 '08 #18

missinglinq
Expert 2.5K+
P: 3,532
I agree with NeoPa, goldstar! You've already been shown how to use the DLookup() function to retrieve data from your table and compare it to data on your form. Place fields in the tblUser table to hold the User_Question and corresponding User_Answer, then compare the User_Name, User_Question and User_Answer from the table to that entered on the form. If they match, open the form. If they don't match, close the form, just like before.

If you have problems with any of the bits and pieces, then post the code you've tried and we'll help.

Linq ;0)>
Feb 15 '08 #19

Post your reply

Sign in to post your reply or Sign up for a free account.