Typing in a Combobox to filter for selection in a subform grid - combobox

I have a form in ms access 2016 with a subform in datagrid view. I have a column that is a combobox and I am filtering the values as you type. It works for the first selection, but when after I make my selection and I want to either go to the next record and make another selection or change the selection I made in my current row, all the values disappear and are blank. This is my code:
Option Compare Database
Private Const RecordSQL As String = "SELECT ConID, ItemNo, Description FROM Consignment"
Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String, lookupField As String)
Dim strSQL As String
If Len(combo.Text) > 0 Then
strSQL = defaultSQL & " WHERE " & lookupField & " LIKE '*" & combo.Text & "*'"
Else
strSQL = defaultSQL 'This is the default row source of combo box
End If
combo.RowSource = strSQL
combo.Dropdown
End Sub
Private Sub Select_Item_AfterUpdate()
'reset dropdown list
Select_Item.RowSource = RecordSQL
Select_Item.Requery
Select_Item.Dropdown
Select_Item.SetFocus
End Sub
Private Sub Select_Item_Change()
FilterComboAsYouType Me.Select_Item, RecordSQL, "Description"
End Sub

Related

Read check status of Checklistbox in vb.net bound to a databasetable

I can successfully populate a checklistbox in vb.net with data stored in a database with this code:
Private Sub report_enter() Handles tp_report.Enter
Dim rep As DataTable = sqlite.SelectData("SELECT field,name,obligatory from cnf_oblfields WHERE module='report'")
clb_obl.DataSource = rep
clb_obl.ValueMember = "field"
clb_obl.DisplayMember = "name"
For i = 0 To rep.Rows.Count - 1
clb_obl.SetItemChecked(i, sqlite.Int2Bool(rep.Rows(i).Item(2)))
Next
End Sub
Now, the user can check and uncheck some boxes. I want to store the new status of these parameters back to the table. I tried this code:
Private Sub bt_obli_save_Click(sender As Object, e As EventArgs) Handles bt_obli_save.Click
For Each item In clb_obl.Items
Dim row As DataRow = item.row
MsgBox("INSERT OR REPLACE INTO cnf_oblfields ('field', 'obligatory') VALUES ('" & item.item("field").ToString & "', '" & item.item("obligatory").ToString & "')")
Next
End Sub
My problem is, that the SUB which shall write the results back to the database gives back the original data, not the changes that i made. This is probably due to the bound datatable in the background?
Thank you for your help!
Lukas
you have to update the datatable when you modify the checkedlistbox like so
Private Sub clb_obl_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles clb_obl.ItemCheck
DirectCast(clb_obl.Items(e.Index), DataRowView)("obligatory") = e.NewValue
End Sub

MS Access: Use VBA to Change Query using Multi-Select List Box

I am trying to do a query that changes based on the selection of a multi-select list box.
My code works fine for 1 item, but if you try to select multiple items it does not work properly.
Private Sub Toggle7_Click()
Dim strSQL As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = Application.CurrentDb
Set qdf = db.QueryDefs("SSSBasic")
Dim Criteria1 As String
Dim strList As String
Dim FstrList As String
Dim varSelected As Variant
'Criteria1 = Me.List_Group
Dim ctl As Control
Set ctl = Me!List_Group
If ctl.ItemsSelected.Count = 0 Then
MsgBox "You haven't selected anything"
Else
For Each varSelected In ctl.ItemsSelected
strList = strList & ctl.ItemData(varSelected) & " Or "
Next varSelected
FstrList = Left$(strList, Len(strList) - 4)
'MsgBox "You selected the following items:" & vbCrLf & FstrList
End If
strSQL = "SELECT * FROM SSSTable WHERE (SSSTable.Group = """ & FstrList & """)"
qdf.SQL = strSQL
DoCmd.OpenQuery "SSSBasic"
End Sub
When I select Proc and Non-Proc from list box, the code will run, but I get zero records. This is what the SQL looks like:
SELECT *
FROM SSSTable
WHERE (((SSSTable.Group)="Proc Or Non-Proc"));
I cannot figure out how to get it to work...
Thank you for your help!

MS Access: Combobox controlled records in form don't show up when combobox item is selected by button with vba

I have a form:
where the all textboxes are connected to a source table. The combobox up top, ConcreteItem, controls which record is currently displayed in the form. The combobox works and all the data displays correctly but I also added a Next Item and Previous Item button for the combobox. I have this code for the combobox:
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "ItemDescription = '" & Me.cbo_ConcreteItem.Value & "'"
If rs.NoMatch = True Then
MsgBox "No such record"
Else
Me.Bookmark = rs.Bookmark
End If
rs.Close
This works perfectly. But I have this for the Next Item button.
Dim idx As Long
idx = cbo_ConcreteItem.ListIndex
If idx <> cbo_ConcreteItem.ListCount - 1 Then
cbo_ConcreteItem.Value = cbo_ConcreteItem.ItemData(cbo_ConcreteItem.ListIndex + 1)
Else
cbo_ConcreteItem.Value = cbo_ConcreteItem.ItemData(0)
End If
Me.Refresh`
Unfortunately, when the combobox goes on to the next item, the combobox displays the correct selection but nothing changes below it. Even when I have a "refresh line" in the code, nothing changes. The only solution is to actually select from the combobox itself.
Can someone show me what the problem is?
try manually triggering the combo box event when you change the bookmark
add this line after setting the bookmark...
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "ItemDescription = '" & Me.cbo_ConcreteItem.Value & "'"
If rs.NoMatch = True Then
MsgBox "No such record"
Else
Me.Bookmark = rs.Bookmark
cbo_ConcreteItem_Change
End If
rs.Close

How to do a search-as-you-type textbox in a DataGridView using VB.NET

I have connected my DataGridView to a database but I can't implement the search function.
The flow of the program would be when I click one column of the DataGridView and I type in the search box, I can only get results from that same column not the other columns beside it.
It should also search letter by letter so basically a TextChanged event.
This is how i would do it
First, to have two variable to store your original datatable from database, and also a string variable to store your selected dgv column headertext (which will be used to do the filter later on).
Private oriDataTable As New DataTable
Private columnToFilter As String = String.Empty
My test on some dummy data
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'dummy datatable
oriDataTable.Columns.Add(New DataColumn("ID"))
oriDataTable.Columns.Add(New DataColumn("FirstName"))
oriDataTable.Columns.Add(New DataColumn("LastName"))
For i = 0 To 5
Dim dr As DataRow = oriDataTable.NewRow()
dr.Item("ID") = i
dr.Item("FirstName") = "fn type1 " & i
dr.Item("LastName") = "ln type1 " & i
oriDataTable.Rows.Add(dr)
Next
For i = 6 To 10
Dim dr As DataRow = oriDataTable.NewRow()
dr.Item("ID") = i
dr.Item("FirstName") = "fn type2" & i
dr.Item("LastName") = "ln type2" & i
oriDataTable.Rows.Add(dr)
Next
'Since you already connected to database
'i assume that you could fill a datatable and bind to dgv
dgvToFilter.DataSource = oriDataTable
columnToFilter = "ID" 'Assign any default column name
End Sub
Then add a ColumnHeaderMouseClick event handler on your dgv, update the columnToFilter each time when user click on it.
Private Sub dgvToFilter_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvToFilter.ColumnHeaderMouseClick
Dim clickedColumn As DataGridViewColumn = dgvToFilter.Columns(e.ColumnIndex)
'Note:HeaderText must match with your datatable column name
columnToFilter = clickedColumn.HeaderText
lblHeaderSelected.Text = columnToFilter
End Sub
And lastly the TextChaged Event. Use the DataTable.Select method to filter the datatable and update the result, if any, to the dgv.
Private Sub txtFilterText_TextChanged(sender As Object, e As EventArgs) Handles txtFilterText.TextChanged
If txtFilterText.Text.Length <= 0 Then dgvToFilter.DataSource = oriDataTable
Dim filterString = String.Format("{0} LIKE '{1}%'", columnToFilter, txtFilterText.Text)
Dim dataRows As DataRow() = oriDataTable.Select(filterString)
'Choose what you wan to do if no row is found. I bind back the oriDataTable.
dgvToFilter.DataSource = If(dataRows.Count > 0, dataRows.CopyToDataTable(), oriDataTable)
End Sub
You can try this.
Private Sub txtUname_TextChanged(sender As Object, e As EventArgs) Handles txtUname.TextChanged
dtaAdap = New SqlDataAdapter("Select * from tbl_user where Fname like '%" & txtUname.Text & "%'" & vbCrLf &
" OR Lname like '%" & txtUname.Text & "%'", con)
dt = New DataTable
dtaAdap.Fill(dt)
DataGridView1.DataSource = dt
End Sub
The query in SQLAdapter goes a little something like this:
Select * from <tbl_name> where <firstparametercolumnname> like '%"& <your searchtexboxname.text here> &"%'
OR <secondparametercolumnname> like '%"& <your searchtexboxname.text here> &"%'
and so on depending on the number of fields you want to look at. Note: "con" is my SQLConnection.
This whole code snippet will fill your DatagridView with the result of the query everytime the user key in something on your searchtextbox.

how to filter the combobox values in vb

I am new to VB.I have a policy no. combobox in VB which populates after I enter first four digits of the policy no.However, if I continue to type the number it overwrites what I have already typed and wipes out the dropdown list, as if selecting for the new 4 numbers I typed.I Want to acheive a scenario where I will enter the first 4 numbers to populate the dropdown after that as i go on entering the policyno the values in the dropdown list get searched.
E.G:Policy number: 969003648, as I type 9690 the drop down is filled in with the policy no's starting from 969000001, now as I go on typing the values as 969003 etc the searched values gets limited to the policy no's having 969003 as starting values...Please assist
My code:
Private Sub PolicyNo_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 46 Or KeyCode = 8 Then
Me.PolicyNo.value = ""
Else
If Len(Me.PolicyNo.Text) >= 4 Then
Me.PolicyNo.RowSource = ""
Call ReloadPolicyNo(Nz(Me.PolicyNo.Text, ""))
Function ReloadPolicyNo(sPolicyNo As String)
Me.PolicyNo.RowSource = "SELECT Inventory.PolicyNo FROM Inventory " & _
"WHERE Left(Inventory.PolicyNo," & Len(Me.PolicyNo.Text) & ") = '" & Me.PolicyNo.Text & "' order by Inventory.PolicyNo"
End Function
Why don't you use the Textchanged event on your combo box to get what is currently in the combobox ?
Example
'THIS FIRES WHEN TEXT IS ENTERED INTO THE COMBOBOX
Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
'GET VALUE IN COMBOBOX
Dim enteredText As String = ComboBox1.Text
Dim strSelect As String = "select * from table where field like = '" & enteredText & "'%"
'load COMBO BOX HERE
End Sub
It's better to use DataView here , I declared DataTable as class Variable.
Dim dt as DataTable
Fetched All the data from database and bind it to declared DataTable, This DataTable further added to DataView and use RowFilter to filter DataView.
Compay_Type is one of the column in my DataTable Below code will filter DataView and bind data where column Company_Type=1
Dim dvComp As New DataView(dt)
dvComp.RowFilter = "Company_Type=1"
ComboBox1.DisplayMember = "CompanyName"
ComboBox1.ValueMember = "CompanyID"
ComboBox1.DataSource = dvComp
To remove Filter just use dvComp.RowFilter = Nothing

Resources