I want to check if a set of specific words contained in a gridview.
For example i want to check if any rows in the gridview contains the words "apple".
If it contains "apple" i want to know which are the rows that contains the word "apple".
Is that possible?
How can i go about doing it?
you can do it in RowDataBound if you used templatefield and have controls other than Label,Literal and Textbox then this may not work
i am assuming that you are using Boundfields since you have not specified any Gridview markup in the Question
Public Sub yourGridview_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For i = 1 To e.Row.Cells.Count
If e.Row.Cells(i).Text.Contains("specifiedtext") Then
'do your operations here
End If
Next
End If
End Sub
You should not search the GridView for any occurences of the given word but it's DataSource. Note that this can be different for example in case of Paging.
So i would recommend to do it in your DAL's query, for example:
SELECT IdColumn FROM Table t
WHERE Col1 LIKE '%#word%'
OR Col2 LIKE '%#word%'
OR Col3 LIKE '%#word%'
OR COl4 LIKE '%#word%'
Then you can use RowDataBound of the GridView to change the CSS(f.e. SearchMatch) of the row which ID is contained in this list.
Related
Need some help, examples, and advice
I have 2 tables | tblMAIN | tblRECORDS
and One Form | frmFAMILY - data source: tblMAIN
I am trying to create a simple SQL Statement using VBA in access that will
match records From tblRECORDS that match WHERE text box OR 2 combo box values on the form frmMAIN match and then fill List Box listRECORDS with data And ignore any values if they are 0.
all of the values are numbers but would like to know how to format if i decide to search for text, like, etc.
I would also like to incorporate some If Then Else. Does anyone know a good resource for SQL statements using VBA examples?
This is what I have but it is not working.
Private Sub Form_Current()
Dim strRecords As String
strRecords = "Select * FROM tblRECORDS WHERE tblRECORDS.RecordID = [Forms]![frmFAMILY].[txtID] OR tblRECORDS.RecordID2 = [Forms]![frmFAMILY].[cboSpouse1]"
Me.listRECORDS.RowSource = strRecords
Me.listRECORDS.Requery
End Sub
I have resolved it by adding OR instead of AND.
I have a combo box called NameFilt. I wish the row source to be as follows:
SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl
WHERE (((VoucherListTbl.BuyerName) Is Not Null)) OR (((VoucherListTbl.BuyerName)<>""));
i.e show all the unique BuyerNames from my table and dont include any blanks
The above SQL is generated in the query bulider by clicking on the 3 dots in combo box's row source in the property sheet, then selecting the BuyerName field and then entering Is Not Null Or <>" in the criteria. Clicking run in the query builder displays the exact result I expect.
On closing and saving the query builder and then clicking in the combo box on the form I get a different result - All the DISTINCT names are there, but there is a blank at the start of the list.
When I attempt to use this SQL in my VBA code I get another result. The code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl WHERE (((VoucherListTbl.BuyerName) Is Not Null))
OR (((VoucherListTbl.BuyerName)<>""));"
Me.NameFilt.Dropdown
End Sub
This results in the combo box's dropdown showing only one option - a blank! There are no names listed.
Moreover, If the WHERE clause is removed i.e. the code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName FROM VoucherListTbl;"
Me.NameFilt.Dropdown
End Sub
Then the DISTINCT names are shown, with a blank option at the top of the list which is what one would expect
Please could you help by explaining why the WHERE clause will not work for me when entered into the VBA code
Many thanks
If you use "" within "" it will break the string and that's the reason of non-working WHERE condition. Encode quotes with "" i.e. end of your original string should look like <>""""));" or replace "" with '' and try again.
Note, same query can be written as
SELECT DISTINCT BuyerName
FROM VoucherListTbl
WHERE IsNull(BuyerName,'')<>''
I have reorganized everything then.
I want to capture the text that is contained in a label I have in a form in order
to be used as the input for the WHERE criteria.
The query is the next called qry_A:
SELECT
tbl_R.ID_R,
tbl_C.Cuenta,
tbl_C.Nombre,
IIf(tbl_RD.Deb Is Null,0,tbl_RD.Deb) AS Deb,
IIf(tbl_RD.Cre Is Null,0,tbl_RD.Cre) AS Cre,
tbl_R.NIT,
tbl_R.Fecha,
tbl_R.Com,
tbl_F.ID_F,
tbl_R.ID_U
FROM
(tbl_F INNER JOIN tbl_R ON tbl_F.ID_F = tbl_R.Fideicomiso)
INNER JOIN (tbl_C RIGHT JOIN tbl_RD ON tbl_C.Cuenta = tbl_RD.Cuenta) ON tbl_R.ID_R = tbl_RD.ID_R
WHERE ((tbl_R.ID_U)=[Forms]![frm_qry_A]![lbl_Usuario]);
I've checked the name of the label and the name of the form. Not sure if this is possible to do with a label, I've checked and tried with a Text Field in the form and it works, I mean, if I capture the text of a Text Field of another form that I have it goes, this is the line added to the query in order to illustrate what I did:
WHERE ((tbl_R.ID_U)=[Forms]![frm_B]![txt_Usuario]);
With that I capture the criteria or text declared in the Form called form_B as this:
Private Sub Form_Load()
Me.lbl_Usuario.Caption = LCase(UsuarioLogeado)
Me.txt_Usuario = UsuarioLogeado
End Sub
You forget to type Parameters collection name:
'Associate parameter
tomarConsulta.Parameters!capturarUsuario = txt_Usuario
or better:
'Associate parameter
tomarConsulta.Parameters("capturarUsuario").Value = txt_Usuario
The label's Caption property contains the text which the label displays. So reference Caption explicitly when you reference the label in the SQL statement.
WHERE tbl_R.ID_U=[Forms]![frm_B]![lbl_Usuario].Caption
I got a bounded combobox to a group name in vb.net. i can view in the drop down items of the combobox the set of GROUP NAMES. but when i do an insert or update query, i need to make the selected group name refers to the GROUP NUMBER. I don't want to store letters in the database, instead i prefer numbers. how can i do that?!
Here is my code so far :
cmd1.Parameters.AddWithValue("#group", DirectCast(Additemcombobox.SelectedItem,
DataRowView).Item("GroupName"))
Storing the group name in database is currently working well.
My question might not be well explained. Please ask me in case...
any help would be appreciated
You can show one element to the user such as the name, but use a different one for the code to identify the item using DisplayMember and ValueMember
Dim SQL = "SELECT Id, Name FROM GroupCode ORDER BY Name"
...
GrpDT = New DataTable
GrpDT.Load(cmd.ExecuteReader)
cboGroup.DataSource = GrpDT
cboGroup.DisplayMember = "Name"
cboGroup.ValueMember = "Id"
The user will only see the names, while the code can use ValueMember:
Private Sub cboGroup_SelectedValueChanged(...etc
Console.WriteLine(cboGroup.SelectedValue)
End Sub
It prints the Group ID not the name. Depending on the ORDER BY clause in the SQL and the ID, the SelectedIndex may or may not match, so respond to SelectedValueChanged event. If you use SelectedValue instead of SelectedItem you wont have to thrash about with a DataRowView item.
Note that SelectedValue is Object so you will have to cast to integer or whatever for use elsewhere.
I have a problem in MS Access and I don't know how can I solve this problem.
I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:
The user select 'A' value From the list box then this query should be run:
SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID
then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).
How can I do that? How can I Link the list box and the query to the textboxes?
thank you so much
Every ListBox has a ListBox_Click() procedure, which is run when an element of the listbox is clicked.
You can get, what you want, if you do something like this:
Sub ListBox1_Click()
Dim valueOfListbox As String
valueOfListBox = ListBox1.Value
' **** here comes your code ****
' get the actual value from the listbox and do the query
' change the values of the textboxes to the result of the query
End Sub
If your listbox is named different than "ListBox1" you have to change the name of the procedure. For example if the listbox is named "blaBox" it has to be blaBox_Click().
Depending on the structure of your data, I would do something like this. Set the table/query of your listbox to have all the data you want to display.
SELECT ID, Name, XValue, YValue, Wert FROM Mytable
In the Format tab of the property sheet of the listbox, set the column count to 2, and the column widths to 0";2" or your preference. This will hide the other values in the listbox rows.
In the three textboxes, set the control source like so.
Textbox1.ControlSource: =[Listbox1].[Columns](3)
Textbox2.ControlSource: =[Listbox1].[Columns](4)
Textbox3.ControlSource: =[Listbox1].[Columns](5)
You may need to adjust the numbers. A listbox has a property called Columns that enable you to access the values at different columns of the listbox. If you don't want to or cannot have all the data in the listbox, then you can use the DLookUp function in each textbox.
Textbox1.ControlSource: =DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")
The reference to [Listbox1] will pull the value of the bound column. If the bound column is not the data you are looking up by then you will need to reference a column (i.e. [Listbox1].[Columns](2)).