Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking password in a table 2

Status
Not open for further replies.

BMKanun

Technical User
Sep 27, 2010
20
MK
Hi,
I'm trying to make code for password check in Access2007.
The user type and password are stored in table (id,name,psw)(Defined in other database)
A form opens with 2 fields, Listbox User (to select username-type) and Textbox Password. On password enter modul runs. It stucks somewhere in the loop, and the Access closes.
The modul:

Public Function Pass()

Dim dbsmain As DAO.Database
Dim rstRecord As DAO.Recordset
Dim strSQL, strUserR, strUserE, strPassE, strPassR As String


On Error GoTo ErrorHandler

strUserE = Forms!Password!User
strPassE = Forms!Password!Password
strUserR = ""
strPassR = ""

Set dbsmain = CurrentDb

'Open a recordset on all records from the tblLogin table
strSQL = "SELECT * FROM tblLogin"
Set rstRecord = dbsmain.OpenRecordset(strSQL, dbOpenDynaset)

With rstRecord
Do Until .EOF
strUserR = rstRecord![Name]

If strUserR = strUserE Then
strPassR = rstRecord![Psw]
End If
Loop
End With

rstRecord.Close
dbsmain.Close

Set rstRecord = Nothing
Set dbsmain = Nothing


Exit Function

ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function


Afterwards, regarding to strUserE value, it will open different menue to continue.

I cant find where it stucks,
Thanks
 

Try putting a break at the beginning and step through the code. I think you'll find that you look at every record, set the strPassR variable, and move to the next record. Nothing in your code indicates that you compare the password to the one entered by your user. Also, you should exit the function once you've found the user.

In addition, I think you need to change this:
Dim strSQL, strUserR, strUserE, strPassE, strPassR As String
to this:
Dim strSQL As String
Dim strUserR As String
Dim strUserE As String
Dim strPassE As String
Dim strPassR As String




Randy
 
Hi Rendy,
I thought to find out the record with the username same as the user selected in the form. If the string match?s:
strUserR = strUserE
then I want to set that particular text from rstRecord![Psw] to the strPassR.

That was the idea. The table with data has only 3 records, (name: administrator, user and visitor)so I thought it will be quick. And I thought to pick up the password only if the user string matches.

The part where i compare the passwords is not shown yet. Firs I want to find If I have the correct password from the rstRecord to compare with. But it seems it is a endless loop.
Is there more elegant way to do this anyway?

thanks
 
But it seems it is a endless loop.
Sure, it is !
Code:
 With rstRecord
    Do Until .EOF
        strUserR = rstRecord![Name]
        If strUserR = strUserE Then
            strPassR = rstRecord![Psw]
            [!]Exit Do[/!]
        End If
        [!].MoveNext[/!]
    Loop
End With

Is there more elegant way to do this anyway?
A simple DLookUp should suffice:
Code:
strPassR = DLookUp("Psw", "tblLogin", "Name='" & strUserE & "'")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How are ya BMKanun . . .

There are several things wrong with your [blue]Pass[/blue] function:
[ol][li]You search by name and if found you set the password. You need to verify both [blue]name[/blue] and [blue]password![/blue][/li]
[li]Even though you set the password above with [tt][blue]strPassR = rstRecord![Psw][/blue][/tt], you never set the function [blue]Pass[/blue] to indicate anything was found. So the same empty variant is always returned.[/li][/ol]
Bear in mind that the logical idea of [blue]Pass()[/blue] is to tell you if [blue]name & password[/blue] are verified! This saids your pass function should be returning a boolean [blue]true/false[/blue] data type. With this in mind I've modified your function to the following:
Code:
[blue]Public Function PassOK() As Boolean
   Dim db As DAO.Database, rst As DAO.Recordset
   Dim frm As Form, SQL As String
   
   Set db = CurrentDb
   SQL = "SELECT Name, Psw " & _
         "FROM tblLogin"
   Set rst = db.OpenRecordset(SQL, dbOpenDynaset)
   Set frm = Forms!Password
   
   Do Until rst.EOF
      If frm!User = rst!Name Then
         If frm!Password = rst!Psw Then
            [purple][b]PassOK = True[/b][/purple]
            Exit Do
         End If
      End If
      
      rst.MoveNext
   Loop
      
   Set frm = Nothing
   Set rst = Nothing
   Set db = Nothing

End Function[/blue]
Code to call [blue]PassOK[/blue] would look like:
Code:
[blue]   If PassOK() Then
      [green]'Name & Password Verified![/green]
   Else
      [green]'Failed Message[/green]
   End If[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi everybody!

@Randy700: thanks, I have applied your suggestions.

@PHV: thanks to you, too, how foolish of me not to see .MoveNext is missing.:)After adding the 2 lines everything worked as I expected.

@theAceMan1: Thanks to you, also! Good to learn (I really need to learn a lot). In general, the case I am on, needs more like: select user level (admin, operator, visitor) from list Box and check simple password. The result should be opening different menu with extra options for the admin. Not much protection. :) or feedback from the code. Thanks anyway!

B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top