DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: VB to check MS Access table,password/use

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    VB to check MS Access table,password/use

    [Originally posted by SAM]

    Hi friends,
    I made a table in access for user logins, where I stored usernames and passwords. And in my project from login form I want to write the code with which only the username and password that is in the access table should be able to enter.
    and the username and password that is not there in the table shouldn't be able to enter the project.
    Please help friends, I am at the early stage of learning VB
    Thanx

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:VB to check MS Access table,password/use

    [Originally posted by Don Gerdts]

    You may want to try something like this:
    set a variable to equal the users password that they give via whatever method you choose, such as an input dialog. Then use that variable in the code that will open the database. It might look something like this:

    Dim sUsePass As String
    Dim dbPath as String

    dbPath = "" 'path to your database
    sUsePass = InputBox("What is your password?", "Password")

    sUsePass = "MS Access;PWD=" + sUsePass

    Set db = OpenDatabase(dbPath, False, False, sUsePass)

    then do whatever needs to be done.

  3. #3
    Join Date
    Aug 2004
    Posts
    43,023

    Re:VB to check MS Access table,password/use

    [Originally posted by Don Gerdts]

    Or maybe this is more to your liking:

    Public Function CheckPassword(dbPath As String, sFieldName as String, sPasswordTable as String) as Boolean
    'you'll need to provide the dbPath and sFieldname and sPasswordTable
    Dim sCompare1 as String
    Dim sCompare2 as String
    On Error Resume Next

    Set db = OpenDatabase(dbPath, False, False, "MS Access;PWD=(a password here if needed)")
    sSql = "SELECT * FROM " & sPasswordTable & ";"
    Set rs = db.OpenRecordset(sSql, dbOpenForwardOnly)

    If rs Is Nothing Then Exit Sub

    sCompare2 = "Start"
    With rs
    Do While Not .EOF 'while not "end of field"
    sCompare1 = rs(sFieldName)
    If CStr(sCompare1) = CStr(sCompare2) Then
    CheckPassword = True
    End If
    sCompare2 = sCompare1
    .MoveNext
    Loop
    End With

    rs.Close
    db.Close
    Set rs = Nothing
    Set db = Nothing

    On Error GoTo 0
    End Function

    If the function returns "True" then they can access the database.

    Enjoy


  4. #4
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Re:VB to check MS Access table,password/use

    [Originally posted by Don Gerdts]

    Oops! Forgot to include the user-entered password. I changed it too this:

    Public Function CheckPassword(dbPath As String, sFieldName as String, sPasswordTable as String, sUserPass as String) as Boolean
    'you'll need to provide the dbPath, sFieldname, sPasswordTable and sUserPass
    'the sUserPass can be gotten from an input box as an example
    Dim sCompare as String
    On Error Resume Next

    Set db = OpenDatabase(dbPath, False, False, "MS Access;PWD=(a password here if needed)")
    sSql = "SELECT * FROM " & sPasswordTable & ";"
    Set rs = db.OpenRecordset(sSql, dbOpenForwardOnly)

    If rs Is Nothing Then Exit Sub

    With rs
    Do While Not .EOF 'while not "end of field"
    sCompare = rs(sFieldName)
    If CStr(sCompare) = CStr(sUserPass) Then
    CheckPassword = True
    End If
    .MoveNext
    Loop
    End With

    rs.Close
    db.Close
    Set rs = Nothing
    Set db = Nothing

    On Error GoTo 0
    End Function


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center