Looking to
Filter a ComboBox based on the value selected from another ComboBox
Having trouble getting the SelectedValue to become the Filter parameter. Hope that makes sense.
stringValue = cobContractNetworkGroup.SelectedValue
If stringValue = "Something" Then
cobAffiliation.Enabled = True
AppReturnAffiliationBindingSource.Filter = "AffiliationType LIKE '%" & AppReturnGroupContractNetworkBindingSource.Current & "%' "
Else
cobAffiliation.Enabled = False
AppReturnAffiliationBindingSource.Filter = ""
End If
Related
I have a UserForm with ComboBoxes 1-8 that each pick up text in designated cells in the ws upon UserForm_Activate. I have set ComboBoxes 2-8 to .Visible=False if ComboBox1.Value = "".
Is there a way to use an abbreviated code to set .Visible=False for each ComboBox without listing each one separately? I have added below what I'm using now, but I create forms like this often and would rather use a "Dim i as Integer / For i =" type thing instead that I could just copy and paste where needed. Thank you in advance!
If ComboBox1.Value = "" Then
ComboBox2.Visible = False
ComboBox3.Visible = False
ComboBox4.Visible = False
ComboBox5.Visible = False
ComboBox6.Visible = False
ComboBox7.Visible = False
ComboBox8.Visible = False
Indirect referencing of controls
Use indirect referencing via Controls() and try
Dim i As Long, current As Long
current = 1 ' << change to the combobox to be excepted
For i = 1 to 8 ' loop through all comboboxes
Me.Controls("ComboBox" & i).Visible = False
Next i
Me.Controls("ComboBox" & current).Visible = True
Here as you can see I'm not using combox_click because it is declared above and the current one is another function to be called without clicking, here my question is how do I get the pre-selected value from the combo box without clicking on the box?
Public Sub ComDep_Change()
Dim sQuery As String
Dim oRS As New ADODB.Recordset
Dim rsPR As New ADODB.Recordset
Dim dateFormat As String
Dim sPONO As String
Dim sPOAmt As String
'oRS.Open "po_receiveable", PRCnn, adOpenDynamic, adLockOptimistic
combVal = ComDep.List(ComDep.ListIndex)
If Not combVal = "ALL_DEPT" And frmMain.OptLatestCN.Value = True Then
'MsgBox ("Works")
dateFormat = "#" + CStr(Day(Now)) + "/" + CStr(Month(Now)) + "/" + CStr(Year(Now) - 3) + "#"
sQuery = "select * from CN_Request_Header where dept = '" & combVal & "' and requestdate >= " & dateFormat & ""
' sQuery = "Select PO_No, PO_Requestor, PO_Req_Dept, PO_Status, PO_Approval_M, PO_Approval_GM, PO_Approval_D, PO_HRApproval, VC_No, TH_Sup_Inv, PO_HR_Rmk, PO_Req_Date, PO_SupplierName, PO_OverallAmt from PR_INFO where PO_Req_Dept = '" & combVal & "'"
' MsgBox ("Result" & sQuery)
rsPR.Open sQuery, PRCnn, adOpenDynamic, adLockOptimistic
lvwCreditNote.ListItems.Clear
Do While Not rsPR.EOF
Set listitem = frmMain.lvwCreditNote.ListItems.Add
With listitem
.Text = CStr(Trim(rsPR!requestID))
.SubItems(1) = Trim(rsPR!requestID)
.SubItems(2) = Format(CStr(rsPR!requestdate), "dd-mmm-yy")
.SubItems(3) = Trim(rsPR!createby)
.SubItems(4) = Trim(rsPR!dept)
.SubItems(5) = Trim(rsPR!reqstatus)
If IsNull(rsPR!custName) Then
.SubItems(6) = ""
Else
.SubItems(6) = Trim(rsPR!custName)
End If
If IsNull(rsPR!cnamt) Then
.SubItems(7) = "0.00"
Else
.SubItems(7) = Format(rsPR!cnamt, "#,###,##0.00")
End If
You would get the currently SELECTED element in a combobox
by using the SELECTEDINDEX Property
If nothing is selected then you get the value -1 returned to you.
If something is selected it will be in the range 0 to Combox.Count - 1
To get the text for the selected item
You could use something like...
Dim SelIndex as Integer
Dim SelText As String
'
SelIndex = MyCombobox.SelectedIndex
If (SelIndex >= 0) AND (SelIndex <= MyCombobox.Count - 1) Then
SelText = MyCombobox.List(Index)
Else
'Nothing was selected in the combobox
End If
I'm building a combobox with the following:
Select 1 or 2 seats:
1
2
And I need to show 1 or 2 as selected depending on the result of a query.
How can I do that?
I have done so far:
SQL = " SELECT numberOfSeats FROM mytable "
SQL = SQL & " WHERE userID ='"
SQL = SQL & txtuserID.Text & "'"
Set auxRes = UAN.OpenResultset(SQL, rdOpenDynamic, rdConcurValues, 0)
cmbNumberOfSeats.Clear
cmbNumberOfSeats.AddItem "Select 1 or 2 seats"
cmbNumberOfSeats.AddItem "1"
cmbNumberOfSeats.AddItem "2"
Thanks!!
You can use the ListIndex property of the ComboBox control to get / set the index of the selected item. You use it like so:
Dim nSelectedIndex As Long
nSelectedIndex = cmbNumberOfSeats.ListIndex
If (nSelectedIndex < 0) Then
'No selected item in the combo box
Else
'There's a selected item, handle it
End If
To set the selected item:
cmbNumberOfSeats.ListIndex = nNewSelectedIndex
The index of the first item is 0; when there's no selection, ListIndex returns -1.
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
this is the content of table 1
Code,Description
1,Pineapple
2,Banana
3,Strawberry
4,Papaya
5,Apple
and it is binded to my datagridview.
I want to sort it by its displaymember and not by valuemember, so it will sort like this
Code,Description
5,Apple
2,Banana
4,Papaya
1,Pineapple
3,Strawberry
note: Description is only display to my datagridview and not the code field.
EDIT:
this is my code:
dim dgvColCmb as New System.Windows.Forms.DataGridViewComboBoxColumn
with dgvColCmb
tmpDSet.Tables(0).DefaultView.Sort = tmpDSet.Tables(0).Columns(1).ColumnName & " DESC "
Dim tmpDTable As DataTable = tmpDSet.Tables(0).DefaultView.ToTable
.DataSource = tmpDTable
.ValueMember = tmpDTable.Columns(0).ColumnName
.DisplayMember = tmpDTable.Columns(1).ColumnName
.ReadOnly = True
end with
dtg.columns.add(dgvColCmb)
dtg.DataSource = sDSet.Tables(0)
You can sort the DataTable that is binded to the datagridview:
table.DefaultView.Sort = "Description desc";
DataTable sortedData = table.DefaultView.ToTable();
Try wrapping the DataTable in a DataView, then setting the Sort property of the DataView to the correct column. You would use the DataView as the DataSource, rather than the DataTable. Like so:
Dim l_view As New DataView(sDSet.Tables(0))
l_view.Sort = "Description" ' ...or... "Description DESC"
dtg.DataSource = l_view