Option Strict Off
Imports System.Data.SqlClient
Public Class 供應商管理_製鞋樣品
    Dim conn As New SqlConnection
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet
    Dim DTP As String
    Private Sub 讀取供應商料表()
        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 供應商編碼, 廠商, 公司名, 公司地址, 公司電話1, 公司電話2, 公司電話3, 公司傳真1, 公司傳真2, 公司傳真3 AS 廠商類別, 聯繫人1, 聯繫人2, 聯繫人3, 聯繫人1職稱, " &
                   "聯繫人2職稱, 聯繫人3職稱, 聯繫人1電話, 聯繫人2電話, 聯繫人3電話, 聯繫人1郵箱, 聯繫人2郵箱, 聯繫人3郵箱, 停用 FROM 供應商清單 ORDER BY 供應商編碼"
        Else
            SQL1 = "SELECT 供應商編碼, 廠商, 公司名, 公司地址, 公司電話1, 公司電話2, 公司電話3, 公司傳真1, 公司傳真2, 公司傳真3, 聯繫人1, 聯繫人2, 聯繫人3, 聯繫人1職稱, " &
                   "聯繫人2職稱, 聯繫人3職稱, 聯繫人1電話, 聯繫人2電話, 聯繫人3電話, 聯繫人1郵箱, 聯繫人2郵箱, 聯繫人3郵箱, 停用 FROM 供應商清單 " &
                   "WHERE 供應商編碼 LIKE N'%" & PA & "%' OR 廠商 LIKE N'%" & PA & "%' OR 公司名 LIKE N'%" & PA & "%' OR 公司地址 LIKE N'%" & PA & "%' OR " &
                   "聯繫人1 LIKE N'%" & PA & "%' OR 聯繫人2 LIKE N'%" & PA & "%' OR 聯繫人3 LIKE N'%" & PA & "%' ORDER BY 供應商編碼"
        End If
        列印用SQL = SQL1
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 80 : DataGridView1.Columns(1).Width = 90 : DataGridView1.Columns(2).Width = 200
        DataGridView1.Columns(3).Width = 400 : DataGridView1.Columns(4).Width = 120 : DataGridView1.Columns(5).Width = 120
        DataGridView1.Columns(6).Width = 120 : DataGridView1.Columns(7).Width = 120 : DataGridView1.Columns(8).Width = 110
        DataGridView1.Columns(9).Width = 110 : DataGridView1.Columns(10).Width = 110 : DataGridView1.Columns(11).Width = 110
        DataGridView1.Columns(12).Width = 110 : DataGridView1.Columns(22).Width = 50
        DataGridView1.Columns(13).Visible = False : DataGridView1.Columns(14).Visible = False : DataGridView1.Columns(15).Visible = False
        DataGridView1.Columns(16).Visible = False : DataGridView1.Columns(17).Visible = False : DataGridView1.Columns(18).Visible = False
        DataGridView1.Columns(19).Visible = False : DataGridView1.Columns(20).Visible = False : DataGridView1.Columns(21).Visible = False
    End Sub
    Private Sub 格式設定1()
        TextBox1.Enabled = False : Button5.Enabled = False : Button1.Enabled = True : Button7.Enabled = True : Button2.Enabled = False : Button3.Enabled = True
        Label2.ForeColor = Color.Black : Label3.ForeColor = Color.Black : Label4.ForeColor = Color.Black : Label5.ForeColor = Color.Black : Label8.ForeColor = Color.Black
    End Sub
    Private Sub 格式設定2()
        Label2.ForeColor = Color.Red : Label3.ForeColor = Color.Red : Label4.ForeColor = Color.Red : Label5.ForeColor = Color.Red : Label8.ForeColor = Color.Red
    End Sub
    Private Sub Set_日期格式轉換()
        DTP = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "HH:mm:ss")
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 廠商類別 FROM 廠商類別 ORDER BY 廠商類別"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear()
        While (dr.Read()) : ComboBox1.Items.Add(dr("廠商類別")) : End While : conn.Close()
    End Sub
    Private Sub 供應商管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MdiParent = WINPROFIT_ERP_SYS : Me.WindowState = 2 : Me.AutoScroll = True
        ComboBox1下拉表單資料載入() : 格式設定1() : 讀取供應商料表() : Set_grid()
        TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False : TextBox7.Enabled = False : TextBox8.Enabled = False
        TextBox9.Enabled = False : ComboBox1.Enabled = False : TextBox11.Enabled = False : TextBox12.Enabled = False : TextBox13.Enabled = False : TextBox14.Enabled = False : TextBox15.Enabled = False
        TextBox16.Enabled = False : TextBox17.Enabled = False : TextBox18.Enabled = False : TextBox19.Enabled = False : TextBox21.Enabled = False : TextBox22.Enabled = False : TextBox23.Enabled = False
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView1(0, e.RowIndex).Value.ToString : TextBox2.Text = DataGridView1(1, e.RowIndex).Value.ToString
            TextBox3.Text = DataGridView1(2, e.RowIndex).Value.ToString : TextBox4.Text = DataGridView1(3, e.RowIndex).Value.ToString
            TextBox5.Text = DataGridView1(4, e.RowIndex).Value.ToString : TextBox6.Text = DataGridView1(5, e.RowIndex).Value.ToString
            TextBox7.Text = DataGridView1(6, e.RowIndex).Value.ToString : TextBox8.Text = DataGridView1(7, e.RowIndex).Value.ToString
            TextBox9.Text = DataGridView1(8, e.RowIndex).Value.ToString : ComboBox1.Text = DataGridView1(9, e.RowIndex).Value.ToString
            TextBox11.Text = DataGridView1(10, e.RowIndex).Value.ToString : TextBox12.Text = DataGridView1(11, e.RowIndex).Value.ToString
            TextBox13.Text = DataGridView1(12, e.RowIndex).Value.ToString : TextBox14.Text = DataGridView1(13, e.RowIndex).Value.ToString
            TextBox15.Text = DataGridView1(14, e.RowIndex).Value.ToString : TextBox16.Text = DataGridView1(15, e.RowIndex).Value.ToString
            TextBox17.Text = DataGridView1(16, e.RowIndex).Value.ToString : TextBox18.Text = DataGridView1(17, e.RowIndex).Value.ToString
            TextBox19.Text = DataGridView1(18, e.RowIndex).Value.ToString : TextBox21.Text = DataGridView1(19, e.RowIndex).Value.ToString
            TextBox22.Text = DataGridView1(20, e.RowIndex).Value.ToString : TextBox23.Text = DataGridView1(21, e.RowIndex).Value.ToString
            停用_ch.Checked = DataGridView1(22, e.RowIndex).Value
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox2.Text = "" : TextBox3.Text = "" : TextBox4.Text = "" : TextBox5.Text = "" : TextBox6.Text = "" : TextBox7.Text = "" : TextBox8.Text = "" : TextBox9.Text = "" : ComboBox1.Text = ""
        TextBox11.Text = "" : TextBox12.Text = "" : TextBox13.Text = "" : TextBox14.Text = "" : TextBox15.Text = "" : TextBox16.Text = "" : TextBox17.Text = "" : TextBox18.Text = ""
        TextBox19.Text = "" : TextBox21.Text = "" : TextBox22.Text = "" : TextBox23.Text = ""
        TextBox2.Enabled = True : TextBox3.Enabled = True : TextBox4.Enabled = True : TextBox5.Enabled = True : TextBox6.Enabled = True : TextBox7.Enabled = True : TextBox8.Enabled = True
        TextBox9.Enabled = True : ComboBox1.Enabled = True : TextBox11.Enabled = True : TextBox12.Enabled = True : TextBox13.Enabled = True : TextBox14.Enabled = True : TextBox15.Enabled = True
        TextBox16.Enabled = True : TextBox17.Enabled = True : TextBox18.Enabled = True : TextBox19.Enabled = True : TextBox21.Enabled = True : TextBox22.Enabled = True : TextBox23.Enabled = True
        DataGridView1.Enabled = False
        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 : TextBox1.Text = dr("供應商編碼").ToString : Else : TextBox1.Text = "" : End If
        conn.Close()
        Dim NUM1 As Integer
        NUM1 = Double.Parse(Strings.Right(TextBox1.Text, 4)) + 1
        If NUM1 < 10 Then : TextBox1.Text = "SU" & "000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : TextBox1.Text = "SU" & "00" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : TextBox1.Text = "SU" & "0" & NUM1
        ElseIf NUM1 > 999 Then : TextBox1.Text = "SU" & NUM1
        End If
        格式設定2()
        Button5.Enabled = True : Button1.Enabled = False : Button7.Enabled = False : Button2.Enabled = False : Button3.Enabled = False
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox8.Text = "" Then
            MsgBox("紅色字體處為必填項，不可空白")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 供應商清單 (供應商編碼, 廠商, 公司名, 公司地址, 公司電話1, 公司電話2, 公司電話3, 公司傳真1, 公司傳真2, 公司傳真3, 聯繫人1, 聯繫人2, 聯繫人3, " &
                  "聯繫人1職稱, 聯繫人2職稱, 聯繫人3職稱, 聯繫人1電話, 聯繫人2電話, 聯繫人3電話, 聯繫人1郵箱, 聯繫人2郵箱, 聯繫人3郵箱, 停用) VALUES (N'" & TextBox1.Text &
                      "',N'" & TextBox2.Text & "',N'" & TextBox3.Text & "',N'" & TextBox4.Text & "',N'" & TextBox5.Text & "',N'" & TextBox6.Text & "',N'" & TextBox7.Text &
                      "',N'" & TextBox8.Text & "',N'" & TextBox9.Text & "',N'" & ComboBox1.Text & "',N'" & TextBox11.Text & "',N'" & TextBox12.Text & "',N'" & TextBox13.Text &
                      "',N'" & TextBox14.Text & "',N'" & TextBox15.Text & "',N'" & TextBox16.Text & "',N'" & TextBox17.Text & "',N'" & TextBox18.Text & "',N'" & TextBox19.Text &
                      "',N'" & TextBox21.Text & "',N'" & TextBox22.Text & "',N'" & TextBox23.Text & "', N'0')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
            conn.Close()
            MsgBox("新增完成")
            格式設定1()
            讀取供應商料表() : Set_grid()
            TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False : TextBox7.Enabled = False
            TextBox8.Enabled = False : TextBox9.Enabled = False : ComboBox1.Enabled = False : TextBox11.Enabled = False : TextBox12.Enabled = False : TextBox13.Enabled = False
            TextBox14.Enabled = False : TextBox15.Enabled = False : TextBox16.Enabled = False : TextBox17.Enabled = False : TextBox18.Enabled = False : TextBox19.Enabled = False
            TextBox21.Enabled = False : TextBox22.Enabled = False : TextBox23.Enabled = False
            DataGridView1.Enabled = True
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        conn.Close() : If conn.State = ConnectionState.Closed Then : conn.Open() : End If
        SQL1 = "UPDATE    供應商清單 SET 公司名 = N'" & TextBox3.Text & "', 公司地址 = N'" & TextBox4.Text &
               "', 公司電話1 = N'" & TextBox5.Text & "', 公司電話2 = N'" & TextBox6.Text & "', 公司電話3 = N'" & TextBox7.Text & "', 公司傳真1 = N'" & TextBox8.Text &
               "', 公司傳真2 = N'" & TextBox9.Text & "', 公司傳真3 = N'" & ComboBox1.Text & "', 聯繫人1 = N'" & TextBox11.Text & "', 聯繫人2 = N'" & TextBox12.Text &
               "', 聯繫人3 = N'" & TextBox13.Text & "', 聯繫人1職稱 = N'" & TextBox14.Text & "', 聯繫人2職稱 = N'" & TextBox15.Text & "', 聯繫人3職稱 = N'" & TextBox16.Text &
               "', 聯繫人1電話 = N'" & TextBox17.Text & "', 聯繫人2電話 = N'" & TextBox18.Text & "', 聯繫人3電話 = N'" & TextBox19.Text & "', 聯繫人1郵箱 = N'" & TextBox21.Text &
               "', 聯繫人2郵箱 = N'" & TextBox22.Text & "', 聯繫人3郵箱 = N'" & TextBox23.Text & "', 停用 = '" & 停用_ch.Checked & "' WHERE (供應商編碼 = '" & TextBox1.Text & "')"
        cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
        conn.Close()
        Set_日期格式轉換() : 格式設定1()
        MsgBox("修改完成")
        讀取供應商料表() : Set_grid()
        TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox4.Enabled = False : TextBox5.Enabled = False : TextBox6.Enabled = False : TextBox7.Enabled = False
        TextBox8.Enabled = False : TextBox9.Enabled = False : ComboBox1.Enabled = False : TextBox11.Enabled = False : TextBox12.Enabled = False : TextBox13.Enabled = False
        TextBox14.Enabled = False : TextBox15.Enabled = False : TextBox16.Enabled = False : TextBox17.Enabled = False : TextBox18.Enabled = False : TextBox19.Enabled = False
        TextBox21.Enabled = False : TextBox22.Enabled = False : TextBox23.Enabled = False
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If conn.State = ConnectionState.Closed Then : conn.Open() : End If
        SQL1 = "SELECT 廠商 FROM 樣品訂單部件表 WHERE (廠商 = N'" & TextBox2.Text & "')"
        cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : dr = cmd.ExecuteReader
        If dr.Read() Then
            MsgBox("該廠商已經有資料引用不可刪除?")
        Else
            conn.Close()
            Dim aa As MsgBoxResult
            aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
            If aa = MsgBoxResult.Ok Then
                If conn.State = ConnectionState.Closed Then : conn.Open() : End If
                SQL1 = "DELETE FROM 供應商清單 WHERE (供應商編碼 = '" & TextBox1.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                Set_日期格式轉換()
                MsgBox("刪除完成")
                讀取供應商料表() : Set_grid()
            End If
        End If : conn.Close()
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        PA = InputBox("請輸入要查詢的供應商資料") : 讀取供應商料表() : Set_grid() : PA = ""
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        列印_供應商資料表.Show()
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Button5.Enabled = False : Button1.Enabled = False : Button7.Enabled = False : Button2.Enabled = True : Button3.Enabled = False
        TextBox3.Enabled = True : TextBox4.Enabled = True : TextBox5.Enabled = True : TextBox6.Enabled = True : TextBox7.Enabled = True : TextBox8.Enabled = True
        TextBox9.Enabled = True : ComboBox1.Enabled = True : TextBox11.Enabled = True : TextBox12.Enabled = True : TextBox13.Enabled = True : TextBox14.Enabled = True : TextBox15.Enabled = True
        TextBox16.Enabled = True : TextBox17.Enabled = True : TextBox18.Enabled = True : TextBox19.Enabled = True : TextBox21.Enabled = True : TextBox22.Enabled = True : TextBox23.Enabled = True
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        清單修改.Close() : 廠商類別修改 = True : 清單修改.ShowDialog() : ComboBox1下拉表單資料載入() : 廠商類別修改 = False
    End Sub
End Class