CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    14

    [RESOLVED] Syntax error ( missing operator ) in query expression

    Hello guys,

    I am getting this error message:

    Syntax error (missing operator ) in query expression ' PatientNumber = "

    The code is as follows:

    Set rs = con.Execute("Select * from Patients where PatientNumber =" & txtnumb & "")
    If rs.EOF = True Then
    Else
    txtfullname = rs!Fullname
    txtnumb = rs!PatientNumber
    End If
    Set con = Nothing

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Syntax error ( missing operator ) in query expression

    Before generating the query you should check for the value in the txtNumb text box. It seems like you are passing an empty value to the query because of which it is failing.

    Also remember, it is always better to use parameterized queries. Search the forum for these.

    Also, please use code tags when you are posting sourcecode in the forum. Look at our FAQ on how to use the code tags.

  3. #3
    Join Date
    Apr 2009
    Posts
    394

    Re: Syntax error ( missing operator ) in query expression

    Code:
    >Set rs = con.Execute("Select * from Patients where PatientNumber =" & txtnumb & "")
    If it is a number then...
    Code:
    Set rs = con.Execute("Select * from Patients where PatientNumber =" & txtnumb)
    but if it is text then...
    Code:
    Set rs = con.Execute("Select * from Patients where PatientNumber ='" & txtnumb & "'")
    Good Luck

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Syntax error ( missing operator ) in query expression

    That is if the field in the database is defined as a number or is defined as text. If the field is defined as text you must use the 's even if the value is a number.

  5. #5
    Join Date
    Mar 2009
    Posts
    14

    Re: Syntax error ( missing operator ) in query expression

    The field in the database is defined as a number with no duplicates. i have tried removing the 's
    but it still gives me the same error message

  6. #6
    Join Date
    Mar 2009
    Posts
    14

    Re: Syntax error ( missing operator ) in query expression

    Hey guys

    i tried to change the field in the database to text and put the following code:

    Set rs = con.Execute("Select * from Patients where PatientNumber ='" & txtnumb & "'")

    this code runs well. but i want in the field txtnumb to accept numbers only as i have specified in the database with no duplicates. i dont want the numbers in that field to be repeated.

    thank you in anticipation

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Syntax error ( missing operator ) in query expression

    Set it as a AUTONUMBER field, and use it as a key. Enter the date/time entered into this db as well
    (I like a DELETED field as well)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: Syntax error ( missing operator ) in query expression

    Quote Originally Posted by vb5prgrmr View Post
    Code:
    >Set rs = con.Execute("Select * from Patients where PatientNumber =" & txtnumb & "")
    If it is a number then...
    Code:
    Set rs = con.Execute("Select * from Patients where PatientNumber =" & txtnumb)
    but if it is text then...
    Code:
    Set rs = con.Execute("Select * from Patients where PatientNumber ='" & txtnumb & "'")
    If it is a number then...
    Code:
    Set rs = con.Execute("Select * from Patients where PatientNumber =" & val(txtnumb))
    would be better, if txtnumb is empty then '0' value is passed to query.
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  9. #9
    Join Date
    Mar 2009
    Posts
    14

    Re: Syntax error ( missing operator ) in query expression

    Thanks COMITSolution for all your help.
    This solutions worked out fine :

    If it is a number then...
    Code:

    Set rs = con.Execute("Select * from Patients where PatientNumber =" & val(txtnumb))

    and thanks to all of you guys. i appreciate all the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured