?
Solved

do while statement with input box response

Posted on 2001-06-28
11
Medium Priority
?
273 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 780 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

839 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