5 Replies - 9431 Views - Last Post: 31 March 2010 - 12:10 AM Rate Topic: -----

#1 saurabhv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-July 08

MS Access and VB Error (Syntax error (missing operator) in query expre

Posted 18 July 2008 - 08:30 AM

Hi,

This is not a homework of mine and i had never tried ms access database with vb so i just gave it a shot. so this is my first time and i am just trying not asking anyone to complete my homework. I have been searching for this a lot on internet but couldn't find a solution to this so i decided to ask this question here.
i have this sql code. what i want to do is that create a basic login form where users puts it's username in the UsernameTextbox and Password in the PasswordTextbox. If the the information match then a message box appears saying that the user is valid and if the information does not exist, a mesage box appears saying the users is not valid.
now everytime i execute(run) the code, i come up with this error

Syntax error (missing operator) in query expression ''UserName' = 'Admin' 'PassWord' = 'testing123''.

the field names(UserName, PassWord) are also correct
they are same as in the database.
the information put in the username and password textbox are correct and they match the information on the database. But i dont know why this error comes up.

my code module is the following:

Imports System.Data.OleDb

Public Class LoginForm1

	' Cancel button
	Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
		Me.Close()
	End Sub

	'ok button
	Private Sub ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ok.Click
		Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\Saurabh\My Documents\Versa Server 2008.mdb")
		Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM ['Login'] WHERE 'UserName' = '" & UsernameTextbox.Text & "'" & " 'PassWord' = '" & PasswordTextbox.Text & "'", con)
		con.Open()
		Dim sdr As OleDbDataReader = cmd.ExecuteReader()
		' If the record can be queried, Pass verification and open another form.
		If (sdr.Read() = True) Then
			MessageBox.Show("The user is valid!")
			Dim mainForm As New Form
			mainForm.Show()
			Me.Hide()
		Else
			MessageBox.Show("Invalid username or password!")
		End If
	End Sub
End Class



I am sure that i am making a silly mistake but still i dont know what is wrong so can anyone please help me out a little.
i am new to ms access and vb.net

any help will be appreciated.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: MS Access and VB Error (Syntax error (missing operator) in query expre

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: MS Access and VB Error (Syntax error (missing operator) in query expre

Posted 18 July 2008 - 10:20 AM

Your problem is you don't put quotes around the column names. Use this


Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Login WHERE UserName = '" & UsernameTextbox.Text & "' AND PassWord = '" & PasswordTextbox.Text & "'", con)



When you put single quotes around the column name the database assumes that is a string and not a column. You also forgot the AND in the query
Was This Post Helpful? 0
  • +
  • -

#3 saurabhv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-July 08

Re: MS Access and VB Error (Syntax error (missing operator) in query expre

Posted 18 July 2008 - 12:02 PM

View PostPsychoCoder, on 18 Jul, 2008 - 10:20 AM, said:

Your problem is you don't put quotes around the column names. Use this


Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Login WHERE UserName = '" & UsernameTextbox.Text & "' AND PassWord = '" & PasswordTextbox.Text & "'", con)



When you put single quotes around the column name the database assumes that is a string and not a column. You also forgot the AND in the query


Thanks a lot
that was really helpful and it is working properly now
thanks for that
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: MS Access and VB Error (Syntax error (missing operator) in query expre

Posted 18 July 2008 - 12:09 PM

Not a problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Gunasekaran*


Reputation:

Re: MS Access and VB Error (Syntax error (missing operator) in query expre

Posted 31 March 2010 - 12:07 AM

please send me Correct Answer: i have occur this problem "syntax error (missing operator) in query expression 'Services No='101"."


Try
            If ComboBox7.Text <> "" Then
                Dim dr As OleDbDataReader
                objcon.con.Open()
                cmd = New OleDbCommand("select * from Services where Services No = " & Val(ComboBox7.Text), objcon.con)
                dr = cmd.ExecuteReader
                'clrall()
                While dr.Read
                    doors.Text = dr(0)
                    TextBox5.Text = dr(1)
                    TextBox1.Text = dr(2)
                    TextBox3.Text = dr(3)
                    TextBox6.Text = dr(4)
                    TextBox4.Text = dr(5)
                    TextBox12.Text = dr(6)
                    TextBox13.Text = dr(7)
                    TextBox45.Text = dr(8)
                    TextBox7.Text = dr(9)
                End While
                objcon.con.Close()
            Else
                MsgBox("Enter the ID for Searching", MsgBoxStyle.Critical, "ERROR")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

This post has been edited by AdamSpeight2008: 01 April 2010 - 04:03 AM
Reason for edit:: Please use the code Tags

Was This Post Helpful? 0

#6 Guest_Gunasekaran*


Reputation:

Re: MS Access and VB Error (Syntax error (missing operator) in query expre

Posted 31 March 2010 - 12:10 AM

please send me Correct Answer: i have occur this problem "syntax error (missing operator) in query expression 'Services No='101"."


Try
            If ComboBox7.Text <> "" Then
                Dim dr As OleDbDataReader
                objcon.con.Open()
                cmd = New OleDbCommand("select * from Services where Services No = " & Val(ComboBox7.Text), objcon.con)
                dr = cmd.ExecuteReader
                'clrall()
                While dr.Read
                    doors.Text = dr(0)
                    TextBox5.Text = dr(1)
                    TextBox1.Text = dr(2)
                    TextBox3.Text = dr(3)
                    TextBox6.Text = dr(4)
                    TextBox4.Text = dr(5)
                    TextBox12.Text = dr(6)
                    TextBox13.Text = dr(7)
                    TextBox45.Text = dr(8)
                    TextBox7.Text = dr(9)
                End While
                objcon.con.Close()
            Else
                MsgBox("Enter the ID for Searching", MsgBoxStyle.Critical, "ERROR")
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

This post has been edited by AdamSpeight2008: 01 April 2010 - 04:03 AM
Reason for edit:: Please use the Code Tags

Was This Post Helpful? 0

Page 1 of 1