Solved

do while statement with input box response

Posted on 2001-06-28
11
262 Views
Last Modified: 2006-11-17
I am relatively new to this Access programming game so this question may seem a little basic to you smart guys (and girls) out there.  My problem is this:

I have a database form containing a command button.  it's on click function deletes the contents of redundant fields on that forms, allowing for fresh data to be entered.  This I can code no problem.  What I can't seem to remember how to do from an old VB course I did last year is code using the do while statement.  This is how I wish to use it.

1.User clicks delete button and is prompted for a password (password is entered into an inputbox)
2.If password is correct (password is built into the code itself and is not linked to any table) content of fields are deleted.
3.If password is wrong, a msbox will inform user they have entered an incorrect password and give them the option to either try again or quit. They can only have three attempts at getting the password correct.
4.If they get password wrong 3 times they get a msgbox telling them this.  The application will then close.

I have tried various ways of doing this without success, the help on Access is no use and the reference book I have does not provide adequate instruction.

Can anyone help?

Cheers

PhilE
0
Comment
Question by:PhilipE
11 Comments
 
LVL 6

Accepted Solution

by:
simonbennett earned 195 total points
ID: 6234623
Hi

Code to do this is:

Public Sub CheckPassword()

    Const MAX_TRIES = 3
   
    Dim lngCount As Long
    Dim bValid As Boolean
   
    bValid = False
   
    For lngCount = 1 To MAX_TRIES
   
        If InputBox("Please Enter Password", "Logon", "") = "PASSWORD" Then
            bValid = True
            Exit For
        Else
            If lngCount < MAX_TRIES Then
                If MsgBox("Password incorrect" & vbCrLf & vbCrLf & "Try again?", vbYesNo + vbQuestion, "Password Incorrect") = vbNo Then
                    Exit For
                End If
            Else
                MsgBox "Only 3 Login attempts allowed" & vbCrLf & vbCrLf, vbInformation + vbOKOnly, ""
            End If
        End If
   
    Next

    If bValid Then
   
        MsgBox "You are in"
       
    Else
       
        MsgBox "Access Denied"
   
    End If

End Sub

HTH

SImon
0
 
LVL 7

Expert Comment

by:Jonathan Kelly
ID: 6234625
This might be 1 solution
It uses an inputbox to challenge the user for a password
After 3 failed attempts a msgbox pops up and the code exits.

try it out by placing similar code behind a button on a form.

if u want to hide the Password text as the user is entering it u will probably need to use a form instead of an input box but the principal is the same .

 
    strIn = ""
    x = 0
    Do While strIn <> "Password"
        If x > 3 Then
            MsgBox "2 many attempts"
            Exit Sub
        End If
        strIn = InputBox("Password", "Please Enter Password")
        x = x + 1
    Loop
   
   
hope this helps
Datrias
0
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 6235815
dim x as integer
dim strPassword as string

strPassword = Me![txtPassword]
x = 0
Do While x<4
    If strPassword<>"YourPassword" then
       msgbox "Incorrect password, try again."
       x = x + 1
    Else
       msgbox "login successful."
       exit sub
    End if
Loop
MsgBox "Too many attempts!", vbokonly + vbExclamation
docmd.quit
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 3

Expert Comment

by:SE081398
ID: 6236327
I've only really read Simons' and it seems like it would work.  However, where are you storing the passwords of the users?  if you're storing them in a table (tbl userpasswords) then you'll have to do a dlookup or a recordset on that table to compare the password input into the inbput box and those stored in the table.

if you need help with doing this let me know.

 
0
 

Expert Comment

by:TinoMatthews
ID: 6236371

This is something you should try....
--First you will need a table listing all users and their passwords.
--In this case if the when the user logs in for the first time they will be prompted to change their password because initially the password in the table is the same as the username.

--Find first will find the record you are looking for in the table.

Private sub cmdLogin_click()
dim Ctr as long
let Ctr=0
datLogin.recordset.findfirst "UserId=" & "'" & txtUserId.text & "'"
If datLogin.recordset.nomatch=true then
    Beep
    Msgbox "Unsuccessful Login! Please re-enter",VbOkOnly, "Invalid Attempt"
    Add 1 to Ctr
    If Ctr=3 then
        Msgbox "Too many attmpts, the program will terminate", vbInformation, "Sorry"
        End
    End sub
    txtUserId.setfocus
    exit sub
End if

elseif datLogin.recordset("Password")<> txtPass.text then
      (same if statement as above)
elseif
     txtUserId.text-txtPass.text then
     MsgBox "Password must be changed",vbokonly,"Please change"
     frmPassword.show
     frmLogin.Hide
End sub  
 
0
 
LVL 3

Expert Comment

by:SE081398
ID: 6236463
Tino, it's EE edicate to answer all questions with comments not answers.  this way the question stays open for others to enter into and add comments.  proposing answers closes the question to others.

0
 

Author Comment

by:PhilipE
ID: 6237391
Not in line with EE Exchange etiquite. Looking to liase with SE a little more on the password table thing.
0
 

Author Comment

by:PhilipE
ID: 6237698
and the points go to Simon.  Cheers.  Works a treat.  In the absence of the EE site yeterday for about three hours I had alomst go it myself, only ran out of patience.  Hope to return the favour in the near future - after I have honed my skills a little more!

Regards

PhilE
0
 

Author Comment

by:PhilipE
ID: 6237947
Cheers.  Works a treat.

Regards

PhilE
0
 
LVL 3

Expert Comment

by:SE081398
ID: 6238644
By the sounds of it, it appears that you got things working.  the password thing is really easy.  As a tip,  if you are producing an mde, you can hide the passwordtable from view.  this doesn't mean it can't be accessed just simply not seen.  You can then do a dlookup or use a recordset to find the password in the hidden table.  This way the users won't be able to figure out where the passwords are stored.  this will prevent deviant users from screwing around and getting access to passwords.

0
 

Author Comment

by:PhilipE
ID: 6238670
Thanks for that SE.  I shall look into doing that at a future date.  In the meantime thanks to everyone who had input into my query.

Thanks again

PhilE
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question