Option Strict Off
Imports System.Data.SqlClient
Public Class 人員名單入錄
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet
    Dim dr As SqlDataReader
    Dim STR1 As String
    Dim DTP As String
    Private Sub Set_人員清單()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        If PA = "" Then
            SQL1 = "SELECT 資料流水號 AS DataID, 序號, 姓名, 類別, 部門, 險種, 購買日期 FROM 人員名單 ORDER BY DataID"
        Else
            SQL1 = "SELECT 資料流水號 AS DataID, 序號, 姓名, 類別, 部門, 險種, 購買日期 FROM 人員名單 " &
                   "WHERE 姓名 LIKE '%" & PA & "%' OR 類別 LIKE '%" & PA & "%' OR 部門 LIKE '%" & PA & "%' OR 險種 LIKE '%" & PA & "%' ORDER BY DataID"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
        Set_grid()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 120 : DataGridView1.Columns(1).Width = 70 : DataGridView1.Columns(2).Width = 80 : DataGridView1.Columns(3).Width = 90
        DataGridView1.Columns(4).Width = 90 : DataGridView1.Columns(5).Width = 90 : DataGridView1.Columns(6).Width = 90
        For I As Integer = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(I).Cells(1).Value = I + 1
        Next
    End Sub
    Private Sub 清單存檔()
        For I As Integer = 0 To DataGridView1.Rows.Count - 1
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人員名單 SET 序號 = N'" & DataGridView1(1, I).Value.ToString & "' WHERE (資料流水號 = '" & DataGridView1(0, I).Value.ToString & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close() : conn.Close()
        Next
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(DateTimePicker1.Value, "yyyy/MM/dd")
    End Sub
    Private Sub 人員名單入錄_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = 楠晉鞋業AAS : Me.AutoScroll = True
        TextBox1.Enabled = False : TextBox2.Enabled = False : Button1.Enabled = False : Button4.Enabled = True
        Set_人員清單()
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("DataID").Value.ToString : TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("序號").Value
        TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("姓名").Value : ComboBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("類別").Value
        ComboBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("部門").Value : ComboBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("險種").Value
        DateTimePicker1.Text = DataGridView1.Rows(e.RowIndex).Cells("購買日期").Value
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = "" : Button4.Enabled = False : Button1.Enabled = True
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP (1) 資料流水號 FROM 人員名單 ORDER BY 資料流水號 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : STR1 = dr("資料流水號").ToString : Else : STR1 = "" : End If
        conn.Close()
        Dim NUM1 As Integer
        NUM1 = Double.Parse(Strings.Right(STR1, 8)) + 1
        If NUM1 < 10 Then : STR1 = "PN" & "0000000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : STR1 = "PN" & "000000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : STR1 = "PN" & "00000" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : STR1 = "PN" & "0000" & NUM1
        ElseIf NUM1 > 9999 And NUM1 < 100000 Then : STR1 = "PN" & "000" & NUM1
        ElseIf NUM1 > 99999 And NUM1 < 100000 Then : STR1 = "PN" & "00" & NUM1
        ElseIf NUM1 > 999999 And NUM1 < 1000000 Then : STR1 = "PN" & "0" & NUM1
        ElseIf NUM1 > 9999999 Then : STR1 = "PN" & NUM1
        End If
        TextBox1.Text = STR1
        TextBox2.Text = DataGridView1.Rows.Count + 1
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Set_日期格式轉換()
        If CheckBox1.Checked = True Then
            ComboBox2.Text = "" : DTP = ""
        End If
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 人員名單 ( 資料流水號, 序號, 姓名, 類別, 部門, 險種, 購買日期) " &
               "VALUES (N'" & TextBox1.Text & "',N'" & TextBox2.Text & "',N'" & TextBox3.Text & "',N'" & ComboBox3.Text & "',N'" & ComboBox1.Text & "',N'" & ComboBox2.Text & "',N'" & DTP & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Set_人員清單() : Set_grid() : 清單存檔() : Button1.Enabled = False : Button4.Enabled = True
        MsgBox("新增完成")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("輸入資料不齊全")
        Else
            Set_日期格式轉換()
            If CheckBox1.Checked = True Then
                ComboBox2.Text = "" : DTP = ""
            End If
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 人員名單 SET 姓名 = N'" & TextBox3.Text & "', 類別 = N'" & ComboBox3.Text & "', 部門 = N'" & ComboBox1.Text & "', " &
                   "險種 = N'" & ComboBox2.Text & "', 購買日期 = N'" & DTP & "' WHERE (資料流水號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close() : conn.Close()
            Set_人員清單() : Set_grid()
            MsgBox("修改完成")
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If TextBox1.Text = "" Then
            MsgBox("沒有選取要刪除的資料")
        Else
            SQL1 = "DELETE FROM 人員名單 WHERE (資料流水號 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            Dim aa As MsgBoxResult
            aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
            If aa = MsgBoxResult.Ok Then : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : End If
            conn.Close() : conn.Close()
            TextBox1.Text = "" : TextBox2.Text = "" : TextBox3.Text = ""
            Set_人員清單() : Set_grid()
            MsgBox("刪除完成")
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_人員清單() : PA = ""
    End Sub
End Class