I am trying to create a SQL Select statement in VB.
I need to select a column based on a check box, and then select that data where it writes it to a datagridview, and adds 3 columns to be appended to a different table.
The code below does not attempt to create all the above just yet, just pulling the data and putting it in a datagridview and adding the 3 columns
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Declarations - Some will give unused errors (this is expected)
Dim CheckBox1 As CheckBox
Dim CheckBox2 As CheckBox
Dim CheckBox3 As CheckBox
Dim Runflg As Char
'IF loop for checkbox selection
If Me.CheckBox1.Checked = False And Me.CheckBox2.Checked = False And Me.CheckBox3.Checked = False Then
MsgBox("You must Select ONE Shift!!!")
Exit Sub
End If
If Me.CheckBox1.Checked = True And Me.CheckBox2.Checked = True And Me.CheckBox3.Checked = True Then
MsgBox("You must Select ONE Shift!!!")
Exit Sub
End If
If Me.CheckBox1.Checked = True And Me.CheckBox2.Checked = True Or Me.CheckBox1.Checked = True And Me.CheckBox3.Checked = True Or Me.CheckBox2.Checked = True And Me.CheckBox3.Checked = True Then
MsgBox("You must Select ONE Shift!!!")
Exit Sub
End If
If Me.CheckBox1.Checked = True Then
Runflg = "RunFlag1"
End If
If Me.CheckBox2.Checked = True Then
Runflg = "RunFlag2"
End If
If Me.CheckBox3.Checked = True Then
Runflg = "RunFlag3"
End If
'SQL Data Connection
Dim sqlCon1 As New SqlConnection("Data Source=SERVER1\DEV01;database=Production;uid=sa;pwd=passwordhere")
Dim daAdap1 As New SqlDataAdapter("SELECT MACHINENUMBER, STYLE FROM ProductionSet WHERE VALUES Runflg = 1", sqlCon1)' My issue is here
Dim SQLcmdBuilder1 As New SqlCommandBuilder(daAdap1)
Dim ds1 As New DataSet
' Adds Columns to Datagridview
Dim AddCol1 As New DataGridViewTextBoxColumn
Dim AddCol2 As New DataGridViewTextBoxColumn
Dim AddCol3 As New DataGridViewTextBoxColumn
AddCol1.DataPropertyName = "Sections"
AddCol1.HeaderText = "Sections"
AddCol1.Name = "Sections"
AddCol2.DataPropertyName = "RunTime"
AddCol2.HeaderText = "RunTime"
AddCol2.Name = "RunTime"
AddCol3.DataPropertyName = "Date"
AddCol3.HeaderText = "Date"
AddCol3.Name = "Date"
'Writes datagridview to form
daAdap1.Fill(ds1, Runflg)
DataGridView1.DataSource = ds1.Tables(0)
DataGridView1.Columns.Add(AddCol1)
DataGridView1.Columns.Add(AddCol2)
DataGridView1.Columns.Add(AddCol3)
End Sub
My main focus is the following:
1. Force a checkbox selection.
2. Based on the selection, execute a Select Statement where it pulls the data from all records where the Runflg variable equals column name of RunFlag1, RunFlag2 or Runflag3, and the record in the column equal 1.
3. Write the info to a datagridview.
4. Allow the user to enter in Sections and Runtime in added columns 1 and 2, and add the Data Selected in a drop down on the form to added column 3.
5. Write/Append the data in the datagridview to another table.
I haven't got past the select statement yet, so numbers 3., 4., and 5. are not completed.