-
Mar 11th, 2014, 10:14 AM
#1
Thread Starter
Addicted Member
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.
-
Mar 11th, 2014, 11:14 AM
#2
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.
-
Mar 11th, 2014, 11:30 AM
#3
Thread Starter
Addicted Member
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!
-
Mar 11th, 2014, 11:32 AM
#4
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?
-
Mar 11th, 2014, 11:48 AM
#5
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
-
Mar 11th, 2014, 12:21 PM
#6
Thread Starter
Addicted Member
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.
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!!!
-
Mar 11th, 2014, 01:16 PM
#7
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
-
Mar 11th, 2014, 01:25 PM
#8
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 %
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|