Access Authorization (VB)

yan19454

Freshman
Joined
May 7, 2003
Messages
29
Access Authorization

I need to develop an application which let user key in the user name and password( which store the user name and password.) The application would check the authorization to let the user use the application. It is easy in VB. I open the recordset and compare that with the text box entry. I need hints to do taht in the VB.net. Thanks.:(
 
RE

look at oledbdataadapter to connect to a database in the help files. There is an example below.
Once you have a valid connection to the database that stores the user name and password you could compare what the user input into a textbox with the database fields like so

Code:
 Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\DatabaseName.mdb")
        MyConnection.Open()
        Dim MyCommand As New OleDbCommand("SELECT * FROM NAME WHERE ('" & txtUserName & "') = USERNAME, MyConnection)
        Dim MyReader As OleDbDataReader = 
        Dim str_Password as String

MyCommand.ExecuteReader()
     While MyReader.Read()
     str_password =  myreader("USERNAME")

       End While 
        MyConnection.Close()
        MyReader.Close()
        MyCommand.Dispose()

The above code selects all of the fields from the database where the text in txtUserName matches that of the field USERNAME in the database and stores it in str_password
then all you have to do is compare the strings.

You'll want to modify this code in the event two persons have the same username, this code will return the last password in the database and not all of them. But it's a good place to start
 
How about Not find

how about user name not find. It will not enter the While loop. but if can't find the user , I need to give message to user . Password is not correct. I need to give message to the uesr ,too. Right ? Where I should put the message. In VB6, you can recordcount=0, no user name, recordcount =0. Password is not correct. Should I use two while . One for user name and one for password. But I still do not know how to solve the user name not find message. Thanks.
It is my first project on VB.net.
 
Last edited:
how about catching it like this?

Code:
' In a class or model declare as public
Public str_Password as String

Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\DatabaseName.mdb")
        MyConnection.Open()
        Dim MyCommand As New OleDbCommand("SELECT * FROM NAME WHERE ('" & txtUserName & "') = USERNAME, MyConnection)
        Dim MyReader As OleDbDataReader = 
        str_Password as String

MyCommand.ExecuteReader()
     While MyReader.Read()
     str_password =  myreader("USERNAME")
     

       End While 
'catch it here
if str_password = "" then msgbox"name not registerd"
        MyConnection.Close()
        MyReader.Close()
        MyCommand.Dispose()
' pass the value of str_password to a new sub 
     passwordSub(str_password)

I would use str_password as a Public in a class or model
then create a Sub to handle comparing the password to user input and call the sub at the end of the connection routine
 
Nice and easy way

Visual Basic:
        Dim CheckConnection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.mdb")
        Dim CheckCommand As New OleDb.OleDbCommand("SELECT * FROM Users WHERE Username = '" & Username.Text & "' And Password = '" & Password.Text & "'", CheckConnection)
        Dim MyReader As OleDb.OleDbDataReader = CheckCommand.ExecuteReader
        If MyReader.Read Then
            Msgbox("Your login was sucessfull")
        Else
            Msgbox("Not logged in")
        End If
        MyReader.Close()
        CheckCommand.Dispose()

Andy
 
Back
Top