Im making a simple login form for my project and I faced this annoying database connection problem.
The problem is I can't connect to "MSACCESS2013" it directs me to my errorhandler but I'm pretty sure my datasource is correct. I already created a datasource and I'm using its connection string. I already exploited all possible solutions. Please help me. I'm using VB2010 and MSACCESS2013 and Windows 10 OS.

Public Class LogIn

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ' Check if username or password is empty
        If TextBox2.Text = "" Or TextBox1.Text = "" Then
            MessageBox.Show("Please complete the required fields..", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            ' Both fields was supply
            ' Check if user exist in database
            ' Connect to DB
            Dim conn As New System.Data.OleDb.OleDbConnection()
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PIA-AMS.accdb"

            Try

                Dim sql As String = "SELECT * FROM user WHERE Username='" & TextBox1.Text & "' AND Password = '" & TextBox2.Text & "'"
                Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)

                'Open Database Connection
                sqlCom.Connection = conn
                conn.Open()

                Dim sqlRead As System.Data.OleDb.OleDbDataReader = sqlCom.ExecuteReader()

                If sqlRead.Read() Then
                    MainPanel.Show()
                    Me.Hide()

                Else
                    ' If user enter wrong username and password combination
                    ' Throw an error message
                    MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    'Clear all fields
                    TextBox2.Text = ""
                    TextBox1.Text = ""

                    'Focus on Username field
                    TextBox1.Focus()
                End If

            Catch ex As Exception
                MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class

Recommended Answers

All 2 Replies

At ConnectionStrings.com the string for Access 2013 (standard security) is

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;

Try specifying the fully qualified file name for the database. It also might help (more info) if you comment out the try/catch logic and get the actual error message instead of masking it with your own.

It turn out that the problem is the name of fields in my database. I realized that both user and password are reserved words in Access. Anyway thanks for your swift reply I will keep in mind your advise mate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.