Using a database to store and retrieve User Names
Q: My name is Ward Mitchell. I am currently creating a program that
involves a login into the main part of a VB application that I am
creating. I have searched the internet trying to find the answer, or
some code relevant to the question, but haven't found anything.
My question: How do you program a MS Access database to store and
retreive usernames and passwords from a database already created, for
access into the program, via this login form?
Ward Mitchell
A: what about this (just freewheeling)
you have a database called Application with a table Users with fields
UsreName. Password you have a inlogscreen with two textboxes txtUserName,
txtPassWord and a commandbutton cmdOk
You have NOT a connection to the database (I don't know if you use the
datacontrol so I do everything in code)
Under the cmdOK_Click event you put the next code:
Sub cmdOK_Click
'first check if both entrys are filled
If Len(txtUserName.text) And LEN(txtPassWord.text) Then
If Not CheckAuthorisation(txtUserName.Text,txtPassWord.Text) Then
Goto cmdOK_FAIL
End If
End If
Exit Sub
cmdOK_FAIL:
Unload Me
End Sub
Private Function CheckAuthorisation(Byref strUserName As String _
.Byref strPassWord As String)
Dim Db As Database
Dim rs As Recordset
Dim SQL as String
On Error Goto CheckAuthorisation_FAIL
CheckAuthorisation = False
Set db = OpenDatabase("Appplication")
SQL = " SELECT * FROM Users " + _
" WHERE UserName = '" + strUserName + "'" + _
" AND PassWord = "' + strPassWord + "'"
Set rs = db.OpenRecordSet(SQL)
If Not rs.NoMatch Then
CheckAuthorisation = TRUE
Exit Function
End If
CheckAuthorisation_FAIL:
End Function
I haven't tested it in code but it must be working. Just don't forget to
add the proper reference to your project!!
btw this way your userdatabase can be a different one then the application
database!
Return