I am fairly new to programming! and I am using Visual Basic Studio 2017 to make a windows form
I have a form with 2 radio buttons, 6 checkboxes, and 1 combo box. I have the data in an access database. The point is when one of the radio buttons is chosen, only certain data from the database gets filled in the combo box.
My database has 8 columns, the first column is an id column. It has data like this for example. Burger-FR001. Burger-GR001. Burger-GR002. Sandwich-FR001 and so on. So the radio button filters for either burger or sandwich. I used an SQL statement with Like
If optBurger.Checked = True Then
SQLa = "SELECT FROM Menu WHERE MENU_ID Like '%Burger%'"
For example, if one radio button says Burger and the other sandwich, By choosing Burger the combo box only shows me the data related to burgers.
- Burger-FR001
- Burger-GR001
- Burger-GR002
The checkboxes act as a filter, if I choose a checkbox for GR it should only show me the entries with FR, and multiple checkboxes can be chosen. This is done by selecting a "Type" column in the database.
if chkGR.Checked = True Then
SQLa = SQLa & "AND Type= 'GR'"
I know this can be done using if.. else if statements, but with 6 checkboxes and the possibility of adding more, this gets tedious and big! I would need to make 36 if else statement for each checkbox!
I tried looping through the controls and if any box is checked it will remove the first three letters of the name (chk) and add the name to the SQL select statement since that is how they are named in the database
But I cannot seem to make it work! I have spent on this so far. If anyone can help I would appreciate that! I think the loop is correct (or it could be a mess, I am new to this ..)
Code is below:
If optBurger.Checked = True Then
cboSelect.Items.Clear()
SQLa = "SELECT FROM Menu WHERE MENU_ID Like '%Burger%'"
Dim strChk As String = ""
Dim chk As CheckBox
' Loop through each control on the form, we are looking for checkboxes
For Each ctl As Control In grpSubsystem.Controls
If TypeOf ctl Is CheckBox Then
chk = DirectCast(ctl, CheckBox)
If chk.Checked = True Then
SQLa = SQLa & "AND Type ='" & Microsoft.VisualBasic.Mid(chk.Text, 1) & "'"
End If
End If
CallAccess()
RSa.MoveFirst()
Do Until RSa.EOF()
cboSelect.Items.Add(RSa.Fields("MENU_ID").Value)
RSa.MoveNext()
Loop
CloseAccess()
end if
I am trying to do this using the checkChanged event
Thanks in advance