Hi Good People,
I have a Data Grid View and when the form Loads, The Data Grid View is Populated with all my Data from my SQL Database.
I also Have two Combo Boxes (CmbxDriverName) and (CmbxWeekNumber)
I also have two Textboxes (TxtDriverName) And (TxtWeekNumber)
When I select a Value from Combo Box (CmbxDriverName) The Driver Name get put into the (TxtDriverName)
Then the Data Grid View shows all of the data for the selected Diver Name.
Here Is where I want to Load the Combo Box (CmbxWeekNumber) to show the week Numbers related to the Driver Name only.
The Code I have checks if the (TxtDriverName) is Null or Empty, And If empty go ahead and load All Week Numbers into the Combo Box (CmbxWeekNumber), Everything is working fine up to here
Now, If I select A Driver Name from the Combo Box, The textbox (TxtDriverName) = Selected Driver Name from the combo Box
I Then want to Load/Fill the Combo Box (CmbxWeekNumber) with only the selected Driver Name, with Week Number that Corresponds to the Driver Name Textbox (TxtDriverName) Can Anyone check my code to see if I have written the code right. Or Maybe I might have to split my code into two Subs. Basically everything will work on a Text Change Event in the Textboxes.
Private Sub LoadCmbxWeekNumber()
'Query Database
SQL.ExecQuery("SELECT * FROM MultiRecords")
'check if any records exist, If no records exist, Then exit sub
If SQL.RecordCount < 1 Then Exit Sub
'check if Textbox (TxtDriverName.Text) Is Null or Empty
If String.IsNullOrEmpty(TxtDriverName.Text) Then
'if Textbox (TxtDriverName.Text) Is Null or Empty. Then Load Combo Box (CmbxWeekNumber)
'with all Week Numbers In the Database
SQL.ExecQuery("SELECT DISTINCT WeekNumber FROM MultiRecords ORDER BY WeekNumber ASC")
For Each r As DataRow In SQL.SQLDT.Rows
CmbxWeekNumber.Items.Add(r("WeekNumber"))
Next
'if Textbox (TxtDriverName) Is NOT Null or Empty. Then Load Combo Box (CmbxWeekNumber)
'with all Week Numbers corresponding to the Textbox (TxtDriverName.Text)
ElseIf Not String.IsNullOrEmpty(TxtDriverName.Text) Then
'Add Parameter to Equal the Textbox (TxtDriverName)
SQL.AddParam("@DriverName", TxtDriverName.Text)
'Query Database For Week Number That = Driver Name (TxtDriverName.Text)
SQL.ExecQuery("SELECT WeekNumber WHERE DriverName=@DriverName ORDER BY WeekNumber ASC")
'Load Combo Box (CmbxWeekNumber) With Week Numbers, Where the drivers Name = (TxtDriverName.Text)
For Each r As DataRow In SQL.SQLDT.Rows
CmbxWeekNumber.Items.Add(r("WeekNumber"))
Next
End If
End Sub
Private Sub TxtDriverName_TextChanged(sender As Object, e As EventArgs) Handles TxtDriverName.TextChanged
FindDriver()
LoadCmbxWeekNumber()
End Sub
Private Sub FindDriver()
SQL.AddParam("@DriverName", TxtDriverName.Text)
'Load The DataGridView ( DGVMultiRecords ) Where Driver Name = TxtDriverName
LoadGrid("SELECT * FROM MultiRecords WHERE DriverName=@DriverName;")
End Sub
Kind Regards
Gary