Filter DataView by Numeric field - vb.net

With my Table1 access I have 5 fields:
COMPANY_Id Numeric Type,
COMPANY_Ordre Numeric Type,
COMPANY_Total Numeric Type,
COMPANY_Name Text Type,
COMPANY_Date Text Type
When I make a filter by COMPANY_Total, COMPANY_Name, or COMPANY_Date, then the filter works. But with the same type Numeric of fields (COMPANY_Ordre) it does not work.
This is my code of filter dataView:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Try
Dim Dv_Filtre As DataView = DT.DefaultView
Dv_Filtre.RowFilter = "Convert( [COMPANY_Ordre], 'System.String') LIKE '" & TextBox1.Text & "%' OR Convert( [COMPANY_Total], 'System.String') LIKE '" & TextBox1.Text & "%' OR [COMPANY_Name] LIKE '" & TextBox1.Text & "%' OR [COMPANY_Date] LIKE '" & TextBox1.Text & "%'"
Catch ex As Exception
End Try
End Sub
Picture Of two numeric field

Try to add the % also before the textbox1 to get much more results not only data that ONLY ends with specific criteria, such as this:
Dv_Filtre.RowFilter="Convert([COMPANY_Ordre],'System.String') LIKE '%" & TextBox1.Text & "%' OR ...
I hope this can help you brother
^_^

Related

SUMIFS for vb.net

Good day,
How can I do like sumifs in sql query in vb.net
Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
Try
query = "SELECT SUM(prdInput) FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
retrieveSingleResult(query)
proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()
Catch ex As Exception
End Try
End Sub
here is my code. The plan is to take the Sum of column prdInput based on the helper which is txtlot txtPartnumber and txprocess.
I managed to get display the first prdinput the query found but I need the the sum of its since I have multiple txtlot partnumber and process.
Thanks
Edit: I already did the SUM(prdInput) on my query but nothing shows on the textbox.
You're almost there. You just needed to alias the column that you performed the sum aggregate on:
Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
Try
query = "SELECT SUM(prdInput) AS prdInput FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
retrieveSingleResult(query)
proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()
Catch ex As Exception
End Try
End Sub
It might be easier to see the difference with a formatted query:
SELECT
SUM(prdInput) AS prdInput --you forgot to give this column a name
FROM ProdOutput
WHERE HELPER = '" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';

filtering datagridview data using "NOT LIKE" condition is unsuccessful.how to modify the code?

I am developing a simple library management system. i try to filter data with "NOT LIKE" condition. the following code which i use is working, no errors.But gives mixed results.when i filter only "Gunasena" in publisher data rows with Added_Date.but this gives Gnasena+godage datas.Added_Date filter is totally correct.Help me to solve this problem,Thanks. this code works 100% correct in "LIKE" condition.
Private Sub combobox4_TextChange(sender As Object, e As EventArgs) Handles combobox4.TextChange
Dim filters As New List(Of String)
If combobox1.Text = "Added Date" And withoutButton.Checked Then
filters.Add("[Added_Date] LIKE '" & combobox2.Text & "%' And [Author] NOT LIKE '" &
combobox4.Text & "%'")
End If
If combobox1.Text = "Added Date" And withoutButton.Checked Then
filters.Add("[Added_Date] LIKE '" &
combobox2.Text & "%' And [Publisher] not LIKE '" & combobox4.Text & "%'")
End If
BooksBindingSource.Filter = String.Join(" Or ", filters)
End Sub

Syntax error: Missing operand after 'ID' operator

i know theres loads of posts with this problem, but i cant seem to find the error in my code, any ideas?
heres my code:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
On Error GoTo SearchErr
If txtSearch.Text = "" Then
Exit Sub
Else
Dim cantFind As String = txtSearch.Text
MainBindingSource.Filter = "(Convert(ID, 'System.String') LIKE '" & txtSearch.Text & "')" & _
"OR (Student ID LIKE '" & txtSearch.Text & "') OR (First Name LIKE '" & txtSearch.Text & "')" & _
"OR (Last Name LIKE '" & txtSearch.Text & "')"
If MainBindingSource.Count <> 0 Then
With dgvStudentInfo
.DataSource = MainBindingSource
End With
Im trying to search an Access DB in Vb.net
I think SQL is seeing "Student ID" as two words. If your table fields really have spaces in them you need to add brackets, "[Student ID]". (You may need single or double quote identifiers instead of brackets. Not sure.)

Filter datasource where column like/contains VB.NET

UPDATE
Thanks to Hanlet the problem is solved, the syntax is like so:
source1.Filter = "[Column] LIKE '%" & TextBox1.Text & "%'"
Also for anyone interested, this is the syntax for performing the above on multiple criteria:
source1.Filter = "[Column1] LIKE '%" & TextBox1.Text & "%' OR [Column2] LIKE '%" & TextBox1.Text & "%'"
I currently have code to filter my datasource where the column 'Customer Name' equals the text in the text box.
However, what I want is a filter that is similar to the LIKE function in sql, so if the customer name is 'John' and the user inputs 'Jo' into the text box, it will filter all customers who's name is LIKE/contains 'Jo'
This is the current filter code (if you want the code showing how the data is bound just ask):
Dim source1 As New BindingSource()
source1.Filter = "[Customer Name] = '" & TextBox1.Text & "'"
dTableMain.Refresh()
Thanks for any help!
I've done something similar in the past. Here's the general syntax:
' The searchString is searched for in both the Cost Center field AND the Code field
BindingSource.Filter = "[col1] like " & searchString & " OR [col2] like " & searchString
In your case, it would be:
source1.Filter = "[Customer Name] like " & TextBox1.Text
Hope it helps!

Searching a database with a ComboBox

for my college project I am designing a program that can read a Microsoft Access database about animals and display the data. I have managed to program it so that I can search by the animals name and it will display the records in a another form, but I need to be able to search other fields such as LatinName or AverageWeight and then display any results that have a match into a combo box which the user can then select and the program would display the code e.g. if I were to type in 50kg and there are two records with 50kg, it would display them both and then allow me to select which one I wanted.
Any help or advice would be much appreciated, fell free to ask if you need more information.
Public Class Form1
Private Sub btnsear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsear.Click
If (txtname.Text = "") Then
MsgBox("Invalid Search")
Else
Try
Dim newsql As String
newsql = "select * from Animals where AnimalName like " & "'%" & txtname.Text & "%'"
'MsgBox("select * from Animals where AnimalName like " & "'" & txtname.Text & "'")
'msgbox(newsql)
Dim con As New OleDb.OleDbConnection
Dim da As New OleDb.OleDbDataAdapter
' dim ds as NewDataTable
Dim dt As New DataTable("Animals")
' uses the 2010 compatible connection string
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = h:\Animals.accdb"
con.Open()
da = New OleDb.OleDbDataAdapter(newsql, con)
da.Fill(dt)
Form2.Show()
'show name in unbound text box
Form2.nametxt.Text = dt.Rows(0).Item(1)
Form2.latintxt.Text = dt.Rows(0).Item(2)
Form2.locationtxt.Text = dt.Rows(0).Item(3)
Form2.heighttxt.Text = dt.Rows(0).Item(4)
Form2.weighttxt.Text = dt.Rows(0).Item(5)
Form2.diettxt.Text = dt.Rows(0).Item(6)
Form2.statustxt.Text = dt.Rows(0).Item(7)
Form2.lifetxt.Text = dt.Rows(0).Item(9)
Form2.breedtxt.Text = dt.Rows(0).Item(10)
Form2.lengthtxt.Text = dt.Rows(0).Item(11)
Form2.txtimage.Text = dt.Rows(0).Item(12)
Form2.socialchk.Checked = dt.Rows(0).Item(8)
If dt.Rows(0).Item(8) = True Then
Form2.socialchk.Checked = True
Else
Form2.socialchk.Checked = False
End If
Catch
MsgBox("Item Not Found")
'con.close()
End Try
End If
If (txtopt.Text = "'") Then
Try
Dim newsql As String
newsql = "select * from Animals where AnimalName like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where LatinName like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where Location like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageHeight like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageWeight like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where DietaryNeeds like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where ConservationStatus like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageLifeSpan like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where BreedingSeason like " & "'%" & txtopt.Text & "%'"
newsql = "select * from Animals where AverageLength like " & "'%" & txtopt.Text & "%'"
Catch
End Try
End If
End Sub
So you start the form with blank textboxes. You want to be able to search any of the textboxes by typing something in them. You should use multiple handles to get input from the textboxes, something like:
Private Sub searchByKeydown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nameTxt.KeyDown, latintxt.KeyDown, locationtxt.KeyDown, heighttxt.KeyDown ' and all the rest of your textbox names
If e.KeyCode = Keys.Return Then ' This checks if the return key was pressed and will start the search
Dim tmpText As TextBox = DirectCast(sender, TextBox) ' grab the textbox that triggered the event
If tmpText.text = String.Empty Then ' enter was pressed without a search term
MsgBox("Please enter a search term")
Exit Sub
End If
Dim strSearchQuery As String = tmpText.text ' This is what the user wants to search
Select Case tmpText.name
Case "nameTxt"
' put in your search routine here for this textbox
Case "latintxt"
' put in your search routine here for this textbox
Case "locationtxt"
' put in your search routine here for this textbox
Case "heighttxt"
' put in your search routine here for this textbox
End Select
End If
End Sub
You will have more case statements, but this should get you going. Since you can already search by name, it's just a matter of changing the SQL statement for the other searches.
Per NiteTrip's answer, I believe you would also need to set the KeyPreview property of the form to TRUE so that it will pick up the keypress.