Results 1 to 8 of 8

Thread: Syntax error in the expression

  1. #1

    Thread Starter
    Addicted Member ashveen's Avatar
    Join Date
    Sep 2013
    Location
    Sri Lanka
    Posts
    141

    Question Syntax error in the expression

    The below code is for a search box, this code searches the data from the data grid view.


    Code:
     Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
            Payroll_CalculatorBindingSource.Filter = String.Format("[DepartmentCode , [EmployeeCode], [ID], [RecordYear], [MonthNumber], [MonthName] ] like '{0}%'", Me.TextBox1.Text)
        End Sub
    when I type something in the search box I get this

    Syntax error in the expression.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Syntax error in the expression

    I would imagine that might be the case, since the code is being called for every character being typed in the TextBox.
    Usually, you to type a complete line of text, and then process that, rather than process as each character is being entered.
    An easy approach would be to have a button that you press next to the textbox.
    Another possibility, would be to only set the filter when the Enter key has been pressed.
    That may not be the issue you're dealing with, since even a single character might not be a Syntax error. I'm not familiar with database stuff, so don't know if the "syntax error" is comming from the Payroll_CalculatorBindingSource object or not, because the String.Format method itself should produce a string without issue, as far as I can see.

  3. #3

    Thread Starter
    Addicted Member ashveen's Avatar
    Join Date
    Sep 2013
    Location
    Sri Lanka
    Posts
    141

    Re: Syntax error in the expression

    Do u know any code to write for a search box that will search data when the Enter key is pressed???
    Thank YOU!

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Syntax error in the expression

    The syntax of the filter is off. Why don't you explain to us how you want the filter to work?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Syntax error in the expression

    Yeah... that's an interesting format there... one I've never seen before... usually the filter syntax is similar to that of a SQL Where clause...

    .Filter = "Department like '%" & searchstring & "%'"

    or if you need multiple fields:
    .Filter = "(Department like '%" & searchstring & "%') or (OfficeName like '%" & searchstring & "%')"

    % is the wildcard in SQL Server... not sure if the .Filter uses the same or if it uses "*" ... one of those two should work.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member ashveen's Avatar
    Join Date
    Sep 2013
    Location
    Sri Lanka
    Posts
    141

    Re: Syntax error in the expression

    dday9
    The filter searches/finds the data that I enter in the textbox and then display that data in the data grid view.

    techgnome
    I'm not using SQL Server, I'm using MS ACCESS as the back end. yes I want to search multiple records in one text box.

    Can you please tell me how???
    thank you!!!

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Syntax error in the expression

    As I mentioned, % is for SQL Server... I think Access uses * ... but it depends on what .Filter uses... not sure if it respects the source database's convention, or if it uses it's own... again, it will be either % or *... one will work, the other won't (but won't give an error either).

    And you mean multiple fields, not records, right? I gave an example of that... Or at least I thought I had... the site has been acting all weird today, so I suppose it's possible that part of my post go cut off...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Syntax error in the expression

    The filter searches/finds the data that I enter in the textbox and then display that data in the data grid view.
    Not what I'm asking. Which data columns do you want to match and do you it to be an exact match, a like match, starts with match, ends with match, etc.

    As I mentioned, % is for SQL Server... I think Access uses * ... but it depends on what .Filter uses... not sure if it respects the source database's convention, or if it uses it's own... again, it will be either % or *... one will work, the other won't (but won't give an error either).
    Filter uses %
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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