none
Unable to connect to database in ms access RRS feed

  • Question

  • Hi,

    First time to use an ms access database 2013 in application. I have a login form created in vb.net.(visual studio 2008.  When trying to connect i got an error "Failed to connect to database. These are the process i made in my form.

    In DataBibding properties I created a new connection and tested successfully. lease see below the whole code.  Kindly I ask help on why and what is the problem. Thank you.

       
        Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
            'frmMain.ShowDialog()
            ' Check if username or password is empty
    
            If txtPassword.Text = "" Or txtUsername.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=C:\Users\JOV\Desktop\Dot Net Project\OSS\OSS\DATA\KPI.accdb"
                Try
    
                    'conn.Open()
                    'MsgBox("Susscess")
                    Dim sql As String = "SELECT * FROM User WHERE username='" & txtUsername.Text & "' AND password = '" & txtPassword.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
                        frmMain.ShowDialog()
                        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
                        txtPassword.Text = ""
                        txtUsername.Text = ""
    
                        'Focus on Username field
                        txtUsername.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

    • Edited by Lenoj Friday, March 13, 2015 7:56 AM
    Friday, March 13, 2015 7:25 AM

Answers

All replies

  • could you replace, this

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

    by

     Catch ex As Exception
                    MessageBox
    .Show(ex.message)
               
    End Try

    and tel us the exact error message

    • Marked as answer by Lenoj Monday, March 16, 2015 5:22 AM
    Friday, March 13, 2015 8:06 AM
  • Hi Rbie,

    thanks for the reply. When i removed catch and try. i got this error.

    The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

    By the way, my ms access is 32 bit and visual studio is 32bit my machine is 64bit.

    Friday, March 13, 2015 8:17 AM
  • Hi Rbie,

    thanks for the reply. When i removed catch and try. i got this error.

    The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

    By the way, my ms access is 32 bit and visual studio is 32bit my machine is 64bit.


    Change the Platform for your project to x86 (Build...Configuration Manager...). This will enable your app to run 32-bit on 64-bit Windows.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, March 13, 2015 4:23 PM
  • Thank you very much Rbie and Paul.  installing the x86 platform version and update the patches and it's already working.

    Monday, March 16, 2015 5:25 AM