0

I am creating an application and I am having difficulty at the first hurdle. I have created a login form consisting of a username and password text box where the the entries must be matched to a record in the database to proceed.

The application is created in Visual Studio Express 2013 and the database is created in Access 2013. When I attempt to compile I am getting the following error message.

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll.

Additional information: Syntax error in FROM clause.

The code for the page is below

Imports System

Imports System.Data

Imports System.Data.OleDb

Imports System.Data.SqlClient

Public Class frm_1_Login

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btn_Login.Click
    ' Check if username or password is empty
    If txt_Username.Text = "" Or txt_Password.Text = "" Then
        lbl_LoginWarning.Visible = True
    Else
        'Connect To Database
        Dim conn As New System.Data.OleDb.OleDbConnection()
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Kevin\Desktop\Non Conformance\NonConformance.accdb"

        Dim sql As String = "SELECT * FROM User WHERE UserName ='" & txt_Username.Text & "' AND UserPassword = '" & txt_Password.Text & "'"

        Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql)

        'open database connection
        sqlCom.Connection = conn
        conn.Open()



        ' Try


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

        If sqlRead.Read() Then
            frm_2_MainMenu.Show()
            Me.Hide()
        Else
            'if user enters wrong username and password combination display error message
            lbl_LoginWarning.Visible = True
            'clear all fields
            txt_Password.Text = ""
            txt_Username.Text = ""
            'Focus in username field
            txt_Username.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
End Class

When its throwing me the error message it is highlighting this line of code afterwards

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

I have copied the query into access and that works fine.

I am unsure what is causing this and any help would be much appreciated!

Thanks for reading.

1 Answer 1

2

User is a reserved word in Access: Reserverd words in access. You must incapsulate reserved words with [] to make em work.

Your query should be

SELECT * FROM [User] WHERE UserName ='" & txt_Username.Text & "' AND UserPassword = '" & txt_Password.Text & "'

And please read up on the use of parameters in your queries, it'll save you much problems later on. Here is some handy info on queries and parameters for OleDb (Access)

Sign up to request clarification or add additional context in comments.

2 Comments

Jesus christ..... I have changed field names and all sorts, encapsulated the field names but it never occured to me to encapsulate the table name.... Thanks so much for the response!
No problem! have a great day :) Feel free to approve it as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.