I'm faceing a problem with compering the values of the Acess with the values that the user enters.

The user should enter his /her username and password and choses the title of his/her job.

if the user enters wrong username and wrong password and chosses the wrong job title than a Error message should appear.

I don't know how to do it , I tried alot , but I failed also, I searched for tutorial but I didn't find any.

I'm using Visual Basic 2010 and Acess 2007 .

I hope you can help me.

Using ADO.NET to connect with an Access database is straightforward. Can you post your most recent code that doesn't work? Start by doing just the bare minimum in a console mode program so that all of the unnecessary fluff doesn't get in the way.

thank you .
I don't know much about comparing the entered values with the values that I have in the Access.

I tried to upload my program , But the filetype is not allowed.
the current Code :

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

 If UsernameTextBox.Text = "" And PasswordTextBox.Text = "" Then
            MessageBox.Show("Enter your username & Password", "Login Error", MessageBoxButtons.OK)
        ElseIf LoginDataSet.LoginRow = UsernameTextBox.Text And LoginDataSet.LoginRow = PasswordTextBox.Text Then
            ViewBorrowerForm.Show()

        End If


    End Sub

I'll just use local variables for the parameters and you can substitute your control values as appropriate. What you can do is create the query then try to retrieve the number of matches for entered values. If that number is zero then the entered values were incorrect.

Dim username As String = "John"
Dim password As String = "aardvark"
Dim jobtitle As String = "IT Guru"

Dim qry As String = "SELECT COUNT(*) FROM mytable" &
                    " WHERE [username] = '" & username & "'" &
                    "   AND [password] = '" & password & "'" &
                    "   AND [jobtitle] = '" & jobtitle & "'"

Dim con As New ADODB.Connection
Dim rec As New ADODB.Recordset

con.Open("Driver={Microsoft Access Driver (*.mdb)};Dbq=mydb.acc;Uid=username;Pwd=password;")
rec.Open(qry, con, CursorTypeEnum.adOpenStatic)

If rec(0).Value = 0 Then
    'invalid login
Else
    'valid login
End If

rec.Close()
con.Close()

However, you should really be using OleDb rather than ADO because it allows you to use parameterized queries to reduce the risk of SQL injection attacks. Please see here for an example.

Edited 2 Years Ago by Reverend Jim

Thank you for helping me out.

Could you please explain these statments

Dim con As New ADODB.Connection
Dim rec As New ADODB.Recordset
con.Open("Driver={Microsoft Access Driver (*.mdb)};Dbq=mydb.acc;Uid=username;Pwd=password;")
        rec.Open(qry, con, CursorTypeEnum.adOpenStatic)

I didn't understand them well.

What is the use of these statments ?

Thank you.

You need a connection to the database. That is con. You specify a connection string to indicate the type of database and possibly credentials (username, password). You execute queries either through con (if you don't need results back - delete and insert queries for example) or through a recordset object (rec) when you do need results (select queries).

Thank you again .
there is an Erorr occurring in these two statments :

  Dim con As New **ADODB.Connection**
  Dim rec As New **ADODB.Recordset**

You have to add adodb as a project reference

Project -> Add Reference
under the .NET tab, select adodb

and include the line

Imports ADODB

unfortunately, there is no adodb under .Net tab.
Maybe ,beacuse I have Visual Basic 2010 express .

It's there. It just doesnt sort properly by default. Go to the .NET tab then click on "Component Name" to sort on that column. adodb should be second on the list (just below Accessibility).

It's not showing up :( .
I'm really sorry for bothering you .

CustomMarshalers is showing up Accessibility.

Attachments DD.png 66.77 KB

Your login code doesn't make much sense

 If UsernameTextBox.Text = "" And PasswordTextBox.Text = "" Then
            MessageBox.Show("Enter your username & Password", "Login Error", 

should be :

 If UsernameTextBox.Text = "" Or PasswordTextBox.Text = "" Then
            MessageBox.Show("Enter your username & Password", "Login Error", 

How to add/install adodb in visual Basic 2010 Express .
I don't have it in my version.

Edited 2 Years Ago by VB user

I'd recommend against using ADODB. ADO.NET (the OleDb variants, specifically) should be your go-to for database connections in .NET and is the replacement for ADODB. Following the previous link you'll find that the documentation provides sufficient examples to come up with something workable. You can also find appropriate connection strings here.

Closer to the top of this thread I recommended OleDb and pointed out the problem of SQL injection attacks as well as linking to an example in the Code Snippets.

This article has been dead for over six months. Start a new discussion instead.