Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Public Class 樣品單控制表
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet : ReadOnly ds2 As New DataSet : ReadOnly ds4 As New DataSet : ReadOnly ds5 As New DataSet : ReadOnly ds6 As New DataSet
    Dim dr As SqlDataReader
    Dim 新流水號 As String : Dim 新流水號1 As String : Dim 新流水號3 As String : Dim 新流水號4 As String : Dim 新流水號5 As String
    Dim DTP1 As String : Dim DTP2 As String : Dim DTP3 As String : Dim DTP4 As String : Dim DTP5 As String : Dim 新項次 As String
    Dim NUM1 As Integer
    Dim 新增文件 As Boolean
    Private Sub Set_清單()
        樣品單_dgv.DataSource = Nothing : ds.Clear()
        樣品單_dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        樣品單_dgv.ColumnHeadersHeight = 25
        樣品單_dgv.AllowUserToAddRows = False
        樣品單_dgv.RowTemplate.Height = 50
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        If PA = "" Then
            SQL1 = "SELECT   SO, 季節, 客戶, 型體號碼, 型體名稱, 國家, 狀態, ISS_DATE, ETA, 數量, 制單人, LEFT(制單時間, 10) AS 制單時間, 版次, 工廠, REQ, C_N, 中底, 楦頭編號, 模具編號 
                    FROM     樣品訂單控制表 
                    WHERE    狀態 LIKE N'" & 樣品進度表分類3 & "' 
                    ORDER BY SO DESC, 版次 DESC"
        Else
            SQL1 = "SELECT   SO, 季節, 客戶, 型體號碼, 型體名稱, 國家, 狀態, ISS_DATE, ETA, 數量, 制單人, LEFT(制單時間, 10) AS 制單時間, 版次, 工廠, REQ, C_N, 中底, 楦頭編號, 模具編號 
                    FROM     樣品訂單控制表 
                    WHERE  ((SO LIKE N'%" & PA & "%') OR (季節 LIKE N'%" & PA & "%') OR (客戶 LIKE N'%" & PA & "%') OR (型體號碼 LIKE N'%" & PA & "%') 
                             OR (型體名稱 LIKE N'%" & PA & "%') OR (國家 LIKE N'%" & PA & "%') OR (狀態 LIKE N'%" & PA & "%') OR (ISS_DATE LIKE N'%" & PA & "%') OR (ETA LIKE N'%" & PA & "%')) 
                    ORDER BY SO DESC, 版次 DESC"
        End If
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : 樣品單_dgv.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_grid()
        If CheckBox1.Checked = False Then
            樣品單_dgv.Size = New Size(475, 924)
            樣品單_dgv.Columns(0).Width = 55 : 樣品單_dgv.Columns(1).Width = 65 : 樣品單_dgv.Columns(2).Width = 125 : 樣品單_dgv.Columns(3).Width = 80 : 樣品單_dgv.Columns(4).Width = 80
        Else
            樣品單_dgv.Size = New Size(660, 924)
            樣品單_dgv.Columns(0).Width = 55 : 樣品單_dgv.Columns(1).Width = 65 : 樣品單_dgv.Columns(2).Width = 170 : 樣品單_dgv.Columns(3).Width = 150 : 樣品單_dgv.Columns(4).Width = 150
        End If

        樣品單_dgv.Columns(5).Visible = False : 樣品單_dgv.Columns(6).Visible = False : 樣品單_dgv.Columns(7).Visible = False : 樣品單_dgv.Columns(8).Visible = False
        樣品單_dgv.Columns(9).Visible = False : 樣品單_dgv.Columns(10).Visible = False : 樣品單_dgv.Columns(11).Visible = False : 樣品單_dgv.Columns(12).Width = 45
        樣品單_dgv.Columns(13).Visible = False : 樣品單_dgv.Columns(14).Visible = False : 樣品單_dgv.Columns(15).Visible = False : 樣品單_dgv.Columns(16).Visible = False
        樣品單_dgv.Columns(17).Visible = False : 樣品單_dgv.Columns(18).Visible = False
    End Sub
    Private Sub Set_清單1()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 項次, 顏色, 架號, 借出者, 數量, SIZE, KEEP, 流水號, 版次 FROM 樣品訂單清單 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "') ORDER BY 項次"
        列印用SQL = TextBox1.Text : 列印用SQL2 = TextBox3.Text
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()
        Set_DGV2下拉式清單1()

        DataGridView2.Columns(0).FillWeight = 40 : DataGridView2.Columns(1).Visible = False : DataGridView2.Columns(2).FillWeight = 160 : DataGridView2.Columns(3).FillWeight = 73
        DataGridView2.Columns(4).FillWeight = 73 : DataGridView2.Columns(5).FillWeight = 50 : DataGridView2.Columns(6).FillWeight = 45 : DataGridView2.Columns(7).FillWeight = 45
        DataGridView2.Columns(8).Visible = False : DataGridView2.Columns(9).Visible = False
        DataGridView2.Columns(5).DefaultCellStyle.Format = "#,##0.0"
        DataGridView2.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    Private Sub Set_清單2()
        DataGridView3.DataSource = Nothing : ds2.Clear()
        DataGridView3.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView3.ColumnHeadersHeight = 25
        DataGridView3.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 樣品訂單清單.項次, 樣品訂單部件表.部件, 樣品訂單部件表.材料名稱, 樣品訂單部件表.材料說明 AS 顏色, 樣品訂單部件表.廠商, 樣品訂單部件表.備註, " &
               "樣品訂單部件表.版次, 樣品訂單部件表.流水號, 樣品訂單部件表.項次 AS 排列 " &
               "FROM 樣品訂單部件表 INNER JOIN 樣品訂單清單 ON 樣品訂單部件表.流水號 = 樣品訂單清單.流水號 " &
               "WHERE (樣品訂單部件表.SO LIKE N'" & TextBox1.Text & "') AND (樣品訂單部件表.版次 LIKE N'" & TextBox3.Text & "') 
                ORDER BY   樣品訂單部件表.流水號, 樣品訂單部件表.項次, 樣品訂單部件表.部件"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close()
        Set_DGV2下拉式清單3()

        DataGridView3.Columns(0).Width = 40 : DataGridView3.Columns(1).Width = 200 : DataGridView3.Columns(2).Width = 300 : DataGridView3.Columns(3).Width = 171
        DataGridView3.Columns(4).Visible = False : DataGridView3.Columns(5).Width = 100 : DataGridView3.Columns(6).Width = 532 : DataGridView3.Columns(7).Visible = False
        DataGridView3.Columns(8).Visible = False : DataGridView3.Columns(9).Width = 40
        DataGridView3.EditMode = DataGridViewEditMode.EditOnEnter
        TextBox9.Text = DataGridView3.Rows.Count
    End Sub
    Private Sub Set_清單3()
        DataGridView4.DataSource = Nothing : ds4.Clear()
        DataGridView4.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView4.ColumnHeadersHeight = 25
        DataGridView4.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 部件 FROM 樣品訂單部件表 WHERE (SO LIKE N'" & TextBox1.Text & "') AND (版次 LIKE N'" & TextBox3.Text & "') GROUP BY 部件"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds4) : DataGridView4.DataSource = ds4.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單4()
        DataGridView5.DataSource = Nothing : ds5.Clear()
        DataGridView5.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView5.ColumnHeadersHeight = 25
        DataGridView5.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 項次, 備註 FROM 樣品訂單清單 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "') ORDER BY 項次"
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds5) : DataGridView5.DataSource = ds5.Tables(0) : conn.Close()

        DataGridView5.Columns(0).FillWeight = 40 : DataGridView5.Columns(1).FillWeight = 1343
        DataGridView5.EditMode = DataGridViewEditMode.EditOnEnter
    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 ComboBox2下拉表單資料載入()
        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
        ComboBox2.Items.Clear() : While (dr.Read()) : ComboBox2.Items.Add(dr("客戶簡稱")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox3下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  狀態 FROM PURPOSE清單 ORDER BY 狀態"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox3.Items.Clear() : While (dr.Read()) : ComboBox3.Items.Add(dr("狀態")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox4下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 型體號碼 FROM 型體控制表 WHERE 客戶簡稱 LIKE N'" & ComboBox2.Text & "' ORDER BY 型體號碼"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox4.Items.Clear() : While (dr.Read()) : ComboBox4.Items.Add(dr("型體號碼")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox5下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 型體名稱 FROM 型體控制表 WHERE 型體號碼 LIKE N'" & ComboBox4.Text & "' ORDER BY 型體名稱"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox5.Items.Clear() : While (dr.Read()) : ComboBox5.Items.Add(dr("型體名稱")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox6下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 部件 FROM 部件清單 WHERE 部件 LIKE N'" & ComboBox11.Text & "%' AND 樣品單用選擇 LIKE '1' ORDER BY 部件"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox6.Items.Clear() : While (dr.Read()) : ComboBox6.Items.Add(dr("部件")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox7下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT SEASON FROM 季節清單 ORDER BY SEASON"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox7.Items.Clear() : While (dr.Read()) : ComboBox7.Items.Add(dr("SEASON")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox8下拉表單資料載入()
        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
        ComboBox8.Items.Clear() : While (dr.Read()) : ComboBox8.Items.Add(dr("工廠")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox9下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  FOB FROM 港口清單 ORDER BY FOB"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox9.Items.Clear() : While (dr.Read()) : ComboBox9.Items.Add(dr("FOB")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox12下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 廠商 FROM 供應商清單 WHERE 公司傳真3 LIKE '" & ComboBox18.Text & "' AND 停用 = 0 ORDER BY 廠商"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox12.Items.Clear() : While (dr.Read()) : ComboBox12.Items.Add(dr("廠商")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox13下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 樣品物料調料單明細.規格, 樣品物料調料單明細.名稱 
                    FROM 供應商清單 INNER JOIN 樣品物料調料單抬頭 ON 供應商清單.供應商編碼 = 樣品物料調料單抬頭.供應商編號 RIGHT OUTER JOIN 樣品物料調料單明細 ON 樣品物料調料單抬頭.調料單號 = 
                         樣品物料調料單明細.調料單號
                    WHERE (供應商清單.廠商 LIKE N'" & ComboBox12.Text & "') GROUP BY 樣品物料調料單明細.規格, 樣品物料調料單明細.名稱 ORDER BY 樣品物料調料單明細.規格, 樣品物料調料單明細.名稱"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox13.Items.Clear() : While (dr.Read()) : ComboBox13.Items.Add(dr("規格") & " / " & dr("名稱")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox14下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 樣品物料調料單明細.顏色 
                    FROM 供應商清單 INNER JOIN 樣品物料調料單抬頭 ON 供應商清單.供應商編碼 = 樣品物料調料單抬頭.供應商編號 RIGHT OUTER JOIN 樣品物料調料單明細 ON 樣品物料調料單抬頭.調料單號 = 
                         樣品物料調料單明細.調料單號
                    WHERE (供應商清單.廠商 LIKE N'" & ComboBox12.Text & "') GROUP BY 樣品物料調料單明細.顏色 ORDER BY 樣品物料調料單明細.顏色"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox14.Items.Clear() : While (dr.Read()) : ComboBox14.Items.Add(dr("顏色")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox15下拉表單資料載入()
        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
        ComboBox15.Items.Clear() : While (dr.Read()) : ComboBox15.Items.Add(dr("底台")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox16下拉表單資料載入()
        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
        ComboBox16.Items.Clear() : While (dr.Read()) : ComboBox16.Items.Add(dr("中底")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox17下拉表單資料載入()
        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
        ComboBox17.Items.Clear() : While (dr.Read()) : ComboBox17.Items.Add(dr("楦頭編號")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox19下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT CN, 模具編號 FROM 模具開發控制表 GROUP BY CN, 模具編號 ORDER BY 模具編號, CN"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox19.Items.Clear() : While (dr.Read()) : ComboBox19.Items.Add(dr("模具編號")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox18下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 公司傳真3 AS 類別 FROM 供應商清單 GROUP BY 公司傳真3 ORDER BY 公司傳真3"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox18.Items.Clear() : While (dr.Read()) : ComboBox18.Items.Add(dr("類別")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox19_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox19.TextChanged
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT CN FROM 模具開發控制表 WHERE 模具編號 LIKE N'" & ComboBox19.Text & "'"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        If dr.Read() Then
            TextBox12.Text = dr("CN")
        End If : conn.Close()
    End Sub

    Private Sub Set_DGV2下拉式清單1()
        If DataGridView2.Columns.Count.ToString = 32 Then
            DataGridView2.Columns.Remove("顏色")
        Else
        End If
        Dim Col As New DataGridViewComboBoxColumn With {
            .Width = 120,
            .DataPropertyName = "顏色"
        }
        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
        Col.Items.Clear()
        While (dr.Read()) : Col.Items.Add(dr("顏色")) : End While : conn.Close()
        Col.HeaderText = "顏色" : Col.Name = "顏色"
        DataGridView2.Columns.Insert(2, Col)
    End Sub
    Private Sub Set_DGV2下拉式清單3()
        If DataGridView3.Columns.Count.ToString = 32 Then
            DataGridView3.Columns.Remove("廠商")
        Else
        End If
        Dim Col As New DataGridViewComboBoxColumn With {
            .Width = 120,
            .DataPropertyName = "廠商"
        }
        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
        Col.Items.Clear()
        While (dr.Read()) : Col.Items.Add(dr("廠商")) : End While : conn.Close()
        Col.HeaderText = "廠商" : Col.Name = "廠商"
        DataGridView3.Columns.Insert(5, Col)
    End Sub
    Private Sub Set_日期格式轉換()
        DTP1 = DateTimePicker2.Text : DTP2 = DateTimePicker1.Text : DTP3 = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "hh:mm:ss")
        DTP4 = DateTimePicker4.Text : DTP5 = DateTimePicker5.Text
        If DTP1 = " " Then : DTP1 = "" : Else : DTP1 = Format(DateTimePicker2.Value, "yyyy/MM/dd") : End If
        If DTP2 = " " Then : DTP2 = "" : Else : DTP2 = Format(DateTimePicker1.Value, "yyyy/MM/dd") : End If
        If DTP4 = " " Then : DTP4 = "" : Else : DTP4 = Format(DateTimePicker4.Value, "yyyy/MM/dd") : End If
        If DTP5 = " " Then : DTP5 = "" : Else : DTP5 = Format(DateTimePicker5.Value, "yyyy/MM/dd") : End If
    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下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox6下拉表單資料載入() : ComboBox16下拉表單資料載入() : ComboBox17下拉表單資料載入()
        ComboBox7下拉表單資料載入() : ComboBox8下拉表單資料載入() : ComboBox9下拉表單資料載入() : ComboBox15下拉表單資料載入() : ComboBox18下拉表單資料載入() : ComboBox19下拉表單資料載入()
        DateTimePicker1.Format = DateTimePickerFormat.Custom : DateTimePicker1.CustomFormat = " " : DateTimePicker2.Format = DateTimePickerFormat.Custom : DateTimePicker2.CustomFormat = " "
        TextBox1.Enabled = False : TextBox3.Enabled = False : TextBox2.Enabled = False : TextBox4.Enabled = False : TextBox17.Enabled = False : TextBox5.Visible = False
        Button11.Enabled = False : Button6.Enabled = False : Button7.Enabled = False : Button8.Enabled = False : Button15.Enabled = False : Label62.Visible = False
        DataGridView4.Visible = False : DataGridView6.Visible = False : ComboBox10.Visible = False : GroupBox1.Visible = False : Button23.Visible = False
        ComboBox10.Text = Year(Today)
        Set_清單()
        If CC(23) = False Then : Button29.Enabled = False : Button5.Enabled = False : Button12.Enabled = False : Button16.Enabled = False : End If
        If CC(24) = False Then : Button10.Enabled = False : End If
        If CC(25) = False Then : Button1.Enabled = False : Button2.Enabled = False : Button3.Enabled = False : Button14.Enabled = False : Button17.Enabled = False : Button18.Enabled = False : End If
    End Sub
    Private Sub 樣品單控制表_Unload(sender As Object, e As EventArgs) Handles MyBase.Closing
        If Button11.Enabled = True And 新增文件 = True Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 樣品訂單控制表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 樣品訂單清單 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 樣品訂單部件表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
        End If
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Label62.Visible = True Then : Label62.Visible = False : Else : Label62.Visible = True : End If
    End Sub
    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles 樣品單_dgv.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = 樣品單_dgv.Rows.Count - 1 Then
            Exit Sub
        Else
            If 樣品單_dgv(0, e.RowIndex).Value.ToString <> 樣品單_dgv(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(樣品單_dgv.RowHeadersVisible, 樣品單_dgv.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + 樣品單_dgv.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - 樣品單_dgv.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub DataGridView3_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles DataGridView3.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView3.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView3(0, e.RowIndex).Value.ToString <> DataGridView3(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView3.RowHeadersVisible, DataGridView3.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView3.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView3.HorizontalScrollingOffset
                e.Graphics.DrawLine(linePen, startX, startY, endX, startY)
                Exit Sub
            End If
        End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles 樣品單_dgv.CellClick
        If e.RowIndex = -1 Then : Else
            If Button11.Enabled = False Then
                TextBox7.Text = "" : TextBox10.Text = "" : TextBox11.Text = "" : TextBox12.Text = ""
                ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox7下拉表單資料載入()
                ComboBox8下拉表單資料載入() : ComboBox15下拉表單資料載入() : ComboBox16下拉表單資料載入() : ComboBox17下拉表單資料載入() : ComboBox19下拉表單資料載入()
                TextBox1.Text = 樣品單_dgv(0, e.RowIndex).Value.ToString : ComboBox7.Text = 樣品單_dgv(1, e.RowIndex).Value.ToString : ComboBox2.Text = 樣品單_dgv(2, e.RowIndex).Value.ToString
                ComboBox4.Text = 樣品單_dgv(3, e.RowIndex).Value.ToString : ComboBox5.Text = 樣品單_dgv(4, e.RowIndex).Value.ToString : ComboBox1.Text = 樣品單_dgv(5, e.RowIndex).Value.ToString
                ComboBox3.Text = 樣品單_dgv(6, e.RowIndex).Value.ToString : TextBox16.Text = 樣品單_dgv(9, e.RowIndex).Value.ToString : TextBox2.Text = 樣品單_dgv(10, e.RowIndex).Value.ToString
                DateTimePicker3.Value = 樣品單_dgv(11, e.RowIndex).Value.ToString : TextBox3.Text = 樣品單_dgv(12, e.RowIndex).Value.ToString
                ComboBox8.Text = 樣品單_dgv(13, e.RowIndex).Value.ToString : TextBox6.Text = 樣品單_dgv(14, e.RowIndex).Value.ToString : ComboBox17.Text = 樣品單_dgv(17, e.RowIndex).Value.ToString
                ComboBox15.Text = 樣品單_dgv(15, e.RowIndex).Value.ToString : ComboBox16.Text = 樣品單_dgv(16, e.RowIndex).Value.ToString : ComboBox19.Text = 樣品單_dgv(18, e.RowIndex).Value.ToString
                DTP1 = 樣品單_dgv(7, e.RowIndex).Value.ToString
                If DTP1 = "" Then : DateTimePicker2.Format = DateTimePickerFormat.Custom : DateTimePicker2.CustomFormat = " "
                Else : DateTimePicker2.Format = DateTimePickerFormat.Short : DateTimePicker2.Text = DTP1 : End If

                DTP2 = 樣品單_dgv(8, e.RowIndex).Value.ToString
                If DTP2 = "" Then : DateTimePicker1.Format = DateTimePickerFormat.Custom : DateTimePicker1.CustomFormat = " "
                Else : DateTimePicker1.Format = DateTimePickerFormat.Short : DateTimePicker1.Text = DTP2 : End If

                Set_清單1() : Set_清單2() : Set_清單3() : Set_清單4()
                PictureBox1.Image = Nothing : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                               "WHERE 客戶 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體號碼").Value &
                               "' AND 形體名 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體名稱").Value & "' AND 類別 LIKE N'設計圖'"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    PictureBox1.Image = Nothing : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                                   "WHERE 客戶 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體號碼").Value &
                                   "' AND 形體名 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體名稱").Value & "' AND 類別 LIKE N'設計圖'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    While dr.Read() = True
                        Dim unused As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                        Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                    End While
                    conn.Close()
                Else
                    PictureBox1.Image = Nothing : conn.Close()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                               "WHERE 客戶 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                    If dr.Read() Then
                        PictureBox1.Image = Nothing : conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "SELECT TOP(1) 圖片 FROM 鞋型圖片資料庫 " &
                                    "WHERE 客戶 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                        While dr.Read() = True
                            Dim unused1 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                            Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                        End While
                        conn.Close()
                    Else
                        PictureBox1.Image = Nothing : conn.Close()
                        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                        SQL1 = "SELECT 圖片 FROM 鞋樣素描圖控制表 WHERE 型體編號 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                        If dr.Read() Then
                            PictureBox1.Image = Nothing : conn.Close()
                            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                            SQL1 = "SELECT 圖片 FROM 鞋樣素描圖控制表 WHERE 型體編號 LIKE N'" & 樣品單_dgv.Rows(e.RowIndex).Cells("型體號碼").Value & "'"
                            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                            While dr.Read() = True
                                Dim unused2 As Byte() = New Byte(-1) {} : Dim bytes As Byte() = DirectCast(dr.Item("圖片"), Byte())
                                Dim oStream As New MemoryStream(bytes) : PictureBox1.Image = Bitmap.FromStream(oStream)
                            End While
                            conn.Close()
                        Else
                            conn.Close()
                        End If
                    End If
                End If : PictureBox1.SizeMode = 4
            End If
            TextBox16.Text = Format(Val(TextBox16.Text), "###0.0")
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox4.Text = DataGridView2(0, e.RowIndex).Value.ToString : TextBox5.Text = DataGridView2(7, e.RowIndex).Value.ToString
            TextBox16.Text = "0"
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                TextBox16.Text = Val(TextBox16.Text) + DataGridView2.Rows(i).Cells("數量").Value
            Next
        End If
    End Sub
    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox17.Text = DataGridView3(1, e.RowIndex).Value.ToString
            TextBox8.Text = e.RowIndex + 1
        End If
    End Sub
    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        ComboBox4下拉表單資料載入()
    End Sub
    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
        ComboBox5下拉表單資料載入()
    End Sub
    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.MouseDown
        DateTimePicker2.Format = DateTimePickerFormat.Short
    End Sub
    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.MouseDown
        DateTimePicker1.Format = DateTimePickerFormat.Short
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        清單修改.Close() : 季節清單修改 = True : 清單修改.ShowDialog() : ComboBox7下拉表單資料載入() : 季節清單修改 = False
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        清單修改.Close() : 國家清單修改 = True : 清單修改.ShowDialog() : ComboBox1下拉表單資料載入() : 國家清單修改 = False
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        清單修改.Close() : PURPOSE清單修改 = True : 清單修改.ShowDialog() : ComboBox3下拉表單資料載入() : PURPOSE清單修改 = False
    End Sub
    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        部件清單控制表.Close() : 部件清單修改 = True : 部件清單控制表.ShowDialog() : ComboBox6下拉表單資料載入() : 部件清單修改 = False
    End Sub
    Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
        PA = "" : 供應商管理_製鞋樣品.Show() : 供應商管理_製鞋樣品.BringToFront()
    End Sub
    Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
        清單修改.Close() : 顏色清單修改 = True : 清單修改.ShowDialog() : Set_清單1() : 顏色清單修改 = False
    End Sub
    Private Sub Button22_Click(sender As Object, e As EventArgs) Handles Button22.Click
        FACTORY_資料管理.Show() : FACTORY_資料管理.BringToFront() : ComboBox8下拉表單資料載入()
    End Sub
    Private Sub Button28_Click(sender As Object, e As EventArgs) Handles Button28.Click
        清單修改.Close() : 客戶清單修改 = True : 清單修改.ShowDialog() : ComboBox2下拉表單資料載入() : 客戶清單修改 = False
    End Sub
    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        DateTimePicker2.Format = DateTimePickerFormat.Custom : DateTimePicker2.CustomFormat = " "
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        DateTimePicker1.Format = DateTimePickerFormat.Custom : DateTimePicker1.CustomFormat = " "
    End Sub
    Private Sub Button25_Click(sender As Object, e As EventArgs) Handles Button25.Click
        DateTimePicker5.Format = DateTimePickerFormat.Custom : DateTimePicker5.CustomFormat = " "
    End Sub
    Private Sub Button24_Click(sender As Object, e As EventArgs) Handles Button24.Click
        DateTimePicker4.Format = DateTimePickerFormat.Custom : DateTimePicker4.CustomFormat = " "
    End Sub
    Private Sub 新增準備3()
        NUM1 = Double.Parse(新項次) + 1
        If NUM1 < 10 Then : 新項次 = "0" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1
        End If
    End Sub
    Private Sub 新增準備4_1()
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            資料數 = DataGridView2.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 樣品訂單清單 SET 顏色 = N'" & DataGridView2.Rows(i).Cells("顏色").Value & "',架號 = N'" & DataGridView2.Rows(i).Cells("架號").Value &
                       "',數量 = N'" & DataGridView2.Rows(i).Cells("數量").Value & "',SIZE = N'" & DataGridView2.Rows(i).Cells("SIZE").Value & "',KEEP = N'" & DataGridView2.Rows(i).Cells("KEEP").Value &
                       "', 借出者 = N'" & DataGridView2.Rows(i).Cells("借出者").Value & "',備註 = N'" & DataGridView5.Rows(i).Cells("備註").Value & "' 
                    WHERE (SO LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & DataGridView2.Rows(i).Cells("項次").Value & "' AND 版次 LIKE N'" & DataGridView2.Rows(i).Cells("版次").Value & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub 新增準備4_2()
        For i As Integer = 0 To DataGridView3.Rows.Count - 1
            資料數 = DataGridView3.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 樣品訂單部件表 SET  材料名稱 = N'" & DataGridView3.Rows(i).Cells("材料名稱").Value & "', 材料說明 = N'" & DataGridView3.Rows(i).Cells("顏色").Value &
                   "', 備註 = N'" & DataGridView3.Rows(i).Cells("備註").Value & "', 廠商 = N'" & DataGridView3.Rows(i).Cells("廠商").Value &
                   "', 項次 = N'" & DataGridView3.Rows(i).Cells("排列").Value & "' " &
                       "WHERE (SO LIKE N'" & TextBox1.Text & "' AND 流水號 LIKE N'" & DataGridView3.Rows(i).Cells("流水號").Value & "' AND 版次 LIKE N'" & TextBox3.Text &
                       "'  AND 部件 LIKE N'" & DataGridView3.Rows(i).Cells("部件").Value & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
    End Sub
    Private Sub 新流水號2()
        Dim NUM1 As Integer
        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 : 新流水號1 = dr("流水號") : Else : 新流水號1 = "SR0000000000" : End If : conn.Close()

        NUM1 = Double.Parse(Strings.Right(新流水號1, 10)) + 1
        If NUM1 < 10 Then : 新流水號1 = "SR" & "000000000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新流水號1 = "SR" & "00000000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : 新流水號1 = "SR" & "0000000" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : 新流水號1 = "SR" & "000000" & NUM1
        ElseIf NUM1 > 9999 And NUM1 < 100000 Then : 新流水號1 = "SR" & "00000" & NUM1
        ElseIf NUM1 > 99999 And NUM1 < 1000000 Then : 新流水號1 = "SR" & "0000" & NUM1
        ElseIf NUM1 > 999999 And NUM1 < 10000000 Then : 新流水號1 = "SR" & "000" & NUM1
        ElseIf NUM1 > 9999999 And NUM1 < 100000000 Then : 新流水號1 = "SR" & "00" & NUM1
        ElseIf NUM1 > 99999999 And NUM1 < 1000000000 Then : 新流水號1 = "SR" & "0" & NUM1
        ElseIf NUM1 > 999999999 Then : 新流水號1 = "SR" & NUM1
        End If
    End Sub
    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        新增準備4_1() : 新增準備4_2()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP(1) 項次 FROM 樣品訂單清單 WHERE SO LIKE N'" & TextBox1.Text & "' AND (版次 LIKE N'" & TextBox3.Text & "') ORDER BY 項次 DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 新項次 = dr("項次") : Else : 新項次 = "00" : End If : conn.Close()
        新增準備3()

        新流水號2()

        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 樣品訂單清單 (SO, 項次, 架號, 數量, SIZE, KEEP, 顏色, 流水號, 版次, 借出者, 備註) " &
               "VALUES (N'" & TextBox1.Text & "', N'" & 新項次 & "', N'', N'0', N'', N'', N'', N'" & 新流水號1 & "', N'" & TextBox3.Text & "', N'', N'" & 樣品備註 & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Set_清單3()
        For i As Integer = 0 To DataGridView4.Rows.Count - 1
            資料數 = DataGridView4.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 樣品訂單部件表 (SO, 項次, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) VALUES (N'" & TextBox1.Text & "', N'', N'" & DataGridView4.Rows(i).Cells("部件").Value &
                   "', N'', N'', N'', N'', N'" & TextBox3.Text & "', N'" & 新流水號1 & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        Set_清單1() : Set_清單2() : Set_清單4()
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If TextBox4.Text = "" Then
            MsgBox("未選擇需要刪除的資料")
        Else
            新增準備4_1() : 新增準備4_2()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 樣品訂單清單 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & TextBox4.Text & "' AND (版次 LIKE N'" & TextBox3.Text & "'))"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            Set_清單1()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 樣品訂單部件表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 流水號 LIKE N'" & TextBox5.Text & "' AND (版次 LIKE N'" & TextBox3.Text & "'))"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            Set_清單1()
            Dim NUM1 As Integer = 0
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                NUM1 += 1 : If NUM1 < 10 Then : 新項次 = "0" & NUM1 : ElseIf NUM1 > 9 And NUM1 < 100 Then : 新項次 = NUM1 : End If
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "UPDATE 樣品訂單清單 SET 項次 = N'" & 新項次 & "' " &
                       "WHERE (SO LIKE N'" & TextBox1.Text & "' AND 項次 LIKE N'" & DataGridView2.Rows(i).Cells("項次").Value & "' AND 版次 LIKE N'" & DataGridView2.Rows(i).Cells("版次").Value & "')"
                cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
                conn.Close()
            Next
            Set_清單1() : Set_清單2() : Set_清單4() : TextBox4.Text = "" : TextBox5.Text = ""
        End If
    End Sub
    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If ComboBox6.Text = "" Then
            MsgBox("未選擇需要新增的部件")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 部件 FROM 樣品訂單部件表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "' AND 部件 LIKE N'" & ComboBox6.Text & "') GROUP BY 部件"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                MsgBox("該部件已新增，請確認內容。")
                conn.Close()
            Else
                conn.Close()
                新增準備4_2()
                For i As Integer = 0 To DataGridView2.Rows.Count - 1
                    資料數 = DataGridView2.Rows.Count : MyModule1.進度條()
                    If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                    SQL1 = "INSERT INTO 樣品訂單部件表 (SO, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) VALUES (N'" & TextBox1.Text & "', N'" & ComboBox6.Text &
                           "', N'', N'', N'', N'', N'" & TextBox3.Text & "', N'" & DataGridView2.Rows(i).Cells("流水號").Value & "')"
                    cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
                Set_清單2() : Set_清單3()
            End If
        End If
    End Sub
    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        If TextBox17.Text = "" Then
            MsgBox("未選擇需要刪除的資料")
        Else
            新增準備4_2()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 樣品訂單部件表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 部件 LIKE N'" & TextBox17.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
            Set_清單2() : Set_清單3() : TextBox17.Text = ""
        End If
    End Sub
    Private Sub 新增準備1()
        Dim NUM1 As Integer
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP(1) SO FROM 樣品訂單控制表 ORDER BY SO DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 新流水號 = dr("SO") : Else : 新流水號 = "S00000" : End If : conn.Close()

        NUM1 = Double.Parse(Strings.Right(新流水號, 5)) + 1
        If NUM1 < 10 Then : 新流水號 = "S" & "0000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新流水號 = "S" & "000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : 新流水號 = "S" & "00" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : 新流水號 = "S" & "0" & NUM1
        ElseIf NUM1 > 9999 Then : 新流水號 = "S" & NUM1
        End If
        TextBox1.Text = 新流水號
    End Sub
    Private Sub 新增準備2()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 樣品訂單控制表 (SO, 季節, 客戶, 型體號碼, 型體名稱, 國家, 狀態, ISS_DATE, ETA, 數量, 制單人, 制單時間, 版次, 工廠, REQ, C_N, 中底, 楦頭編號, 模具編號) " &
               "VALUES (N'" & TextBox1.Text & "', N'" & ComboBox7.Text & "', N'" & ComboBox2.Text & "', N'" & ComboBox4.Text & "', N'" & ComboBox5.Text & "', N'" & ComboBox1.Text &
               "', N'" & ComboBox3.Text & "', N'" & DTP1 & "', N'" & DTP2 & "', N'" & TextBox16.Text & "', N'" & gUserName & "', N'" & DTP3 & "', N'V01', N'" & ComboBox8.Text &
               "', N'" & TextBox6.Text & "', N'" & ComboBox15.Text & "', N'" & ComboBox16.Text & "', N'" & ComboBox17.Text & "', N'" & ComboBox19.Text & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

        新流水號2()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 樣品訂單清單 (SO, 項次, 架號, 數量, SIZE, KEEP, 顏色, 流水號, 版次, 借出者, 備註) " &
               "VALUES (N'" & TextBox1.Text & "', N'01', N'', N'0', N'', N'', N'', N'" & 新流水號1 & "', N'" & TextBox3.Text & "', N'', N'" & 樣品備註 & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Set_清單1() : Set_清單4()
        MsgBox("資料新增準備完成")
    End Sub
    Private Sub Button29_Click(sender As Object, e As EventArgs) Handles Button29.Click
        Dim aa1 As MsgBoxResult
        aa1 = MsgBox("確定要使用--一般新增--生成一張空白的樣品單嗎?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            新增文件 = True : Timer1.Enabled = True
            Button29.Enabled = False : Button5.Enabled = False : Button12.Enabled = False : Button16.Enabled = False
            Button11.Enabled = True : Button6.Enabled = True : Button7.Enabled = True : Button8.Enabled = True : Button15.Enabled = True
            TextBox3.Text = "V01"
            新增準備1()
            DateTimePicker1.Format = DateTimePickerFormat.Custom : DateTimePicker1.CustomFormat = " " : DateTimePicker2.Format = DateTimePickerFormat.Custom : DateTimePicker2.CustomFormat = " "
            ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox4下拉表單資料載入() : ComboBox19下拉表單資料載入()
            ComboBox5下拉表單資料載入() : ComboBox6下拉表單資料載入() : ComboBox7下拉表單資料載入() : ComboBox8下拉表單資料載入() : ComboBox15下拉表單資料載入()
            TextBox2.Text = gUserName : TextBox4.Text = "" : TextBox5.Text = "" : TextBox16.Text = "0" : TextBox17.Text = "" : TextBox7.Text = "" : TextBox10.Text = ""
            DataGridView2.DataSource = Nothing : ds1.Clear() : DataGridView3.DataSource = Nothing : ds2.Clear() : DataGridView4.DataSource = Nothing : ds4.Clear()
            Set_日期格式轉換()
            新增準備2() : Set_清單()
        End If
    End Sub
    Private Sub 新增準備6()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "INSERT INTO 樣品訂單控制表 (SO, 季節, 客戶, 型體號碼, 型體名稱, 國家, 狀態, ISS_DATE, ETA, 數量, 制單人, 制單時間, 版次, 工廠, REQ, C_N, 中底, 楦頭編號, 模具編號) " &
               "VALUES (N'" & TextBox1.Text & "', N'" & ComboBox7.Text & "', N'" & ComboBox2.Text & "', N'" & ComboBox4.Text & "', N'" & ComboBox5.Text & "', N'" & ComboBox1.Text &
               "', N'" & ComboBox3.Text & "', N'" & DTP1 & "', N'" & DTP2 & "', N'" & TextBox16.Text & "', N'" & gUserName & "', N'" & DTP3 & "', N'" & TextBox3.Text & "', N'" & ComboBox8.Text &
               "', N'" & TextBox6.Text & "', N'" & ComboBox15.Text & "', N'" & ComboBox16.Text & "', N'" & ComboBox17.Text & "', N'" & ComboBox19.Text & "')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            資料數 = DataGridView2.Rows.Count : MyModule1.進度條()
            新流水號2()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 樣品訂單清單 (SO, 項次, 架號, 數量, SIZE, KEEP, 顏色, 流水號, 版次, 借出者, 備註) VALUES (N'" & TextBox1.Text & "', N'" & DataGridView2.Rows(i).Cells("項次").Value &
                   "', N'" & DataGridView2.Rows(i).Cells("架號").Value & "', N'" & DataGridView2.Rows(i).Cells("數量").Value & "', N'" & DataGridView2.Rows(i).Cells("SIZE").Value &
                   "', N'" & DataGridView2.Rows(i).Cells("KEEP").Value & "', N'" & DataGridView2.Rows(i).Cells("顏色").Value & "', N'" & 新流水號1 & "', N'" & TextBox3.Text & "', N'', N'" & 樣品備註 & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        Set_清單1() : Set_清單4()
        For i As Integer = 0 To DataGridView3.Rows.Count - 1
            資料數 = DataGridView3.Rows.Count : MyModule1.進度條()
            For ii As Integer = 0 To DataGridView2.Rows.Count - 1
                If DataGridView3.Rows(i).Cells("項次").Value = DataGridView2.Rows(ii).Cells("項次").Value Then
                    PA = DataGridView2.Rows(ii).Cells("流水號").Value
                End If
            Next
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO  樣品訂單部件表 (SO, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) VALUES (N'" & TextBox1.Text & "', N'" & DataGridView3.Rows(i).Cells("部件").Value &
                "', N'" & DataGridView3.Rows(i).Cells("材料名稱").Value & "', N'" & DataGridView3.Rows(i).Cells("顏色").Value & "', N'" & DataGridView3.Rows(i).Cells("備註").Value &
                "', N'" & DataGridView3.Rows(i).Cells("廠商").Value & "', N'" & TextBox3.Text & "', N'" & PA & "')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        Set_清單2() : Set_清單3()
        MsgBox("資料新增準備完成") : PA = ""
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim aa1 As MsgBoxResult
        aa1 = MsgBox("確定要使用--複製新增--將選中的樣品單資料複製成另一張樣品單嗎?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            If TextBox1.Text = "" Then
                MsgBox("複製新增需要選擇被複製的單據")
            Else
                新增文件 = True : Timer1.Enabled = True
                Button29.Enabled = False : Button5.Enabled = False : Button12.Enabled = False : Button16.Enabled = False
                Button11.Enabled = True : Button6.Enabled = True : Button7.Enabled = True : Button8.Enabled = True : Button15.Enabled = True
                TextBox3.Text = "V01"
                Set_日期格式轉換()
                新增準備1() : 新增準備6() : Set_清單()
            End If
        End If
    End Sub
    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇要修改的樣品單")
        Else
            新增文件 = False : Timer1.Enabled = True
            Button29.Enabled = False : Button5.Enabled = False : Button12.Enabled = False : Button16.Enabled = False
            Button11.Enabled = True : Button6.Enabled = True : Button7.Enabled = True : Button8.Enabled = True : Button15.Enabled = True
        End If
    End Sub
    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        Dim aa1 As MsgBoxResult
        aa1 = MsgBox("確定要使用--REVISED--將選中的樣品單改版成下一個版本嗎?", MsgBoxStyle.OkCancel)
        If aa1 = MsgBoxResult.Ok Then
            If TextBox1.Text = "" Then
                MsgBox("REVISED需要選擇前版的單據")
            Else
                新增文件 = True : Timer1.Enabled = True
                Button29.Enabled = False : Button5.Enabled = False : Button12.Enabled = False : Button16.Enabled = False
                Button11.Enabled = True : Button6.Enabled = True : Button7.Enabled = True : Button8.Enabled = True : Button15.Enabled = True
                Dim NUM1 As Integer
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT TOP (1) 版次 FROM 樣品訂單控制表 WHERE (SO LIKE N'" & TextBox1.Text & "') ORDER BY   版次 DESC"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then : 新流水號3 = dr("版次") : Else : 新流水號3 = "V00" : End If : conn.Close()

                NUM1 = Double.Parse(Strings.Right(新流水號3, 2)) + 1
                If NUM1 < 10 Then : 新流水號3 = "V" & "0" & NUM1
                ElseIf NUM1 > 9 Then : 新流水號3 = "V" & NUM1
                End If
                TextBox3.Text = 新流水號3

                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "SELECT 版次 FROM 樣品訂單控制表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "') ORDER BY   版次 DESC"
                cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
                If dr.Read() Then
                    MsgBox("該單據已有 " & TextBox3.Text & " 版本，REVISED需要選擇最後的版本，請重新選擇。")
                    新增文件 = False : Timer1.Enabled = False : Label62.Visible = False
                    Button29.Enabled = True : Button5.Enabled = True : Button12.Enabled = True : Button16.Enabled = True
                    Button11.Enabled = False : Button6.Enabled = False : Button7.Enabled = False : Button8.Enabled = False : Button15.Enabled = False
                    conn.Close()
                Else
                    conn.Close()
                    Set_日期格式轉換() : 新增準備6() : Set_清單()
                End If
            End If
        End If
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If ComboBox4.Text = "" Or TextBox16.Text = "" Or ComboBox1.Text = "" Or ComboBox2.Text = "" Or ComboBox3.Text = "" Or ComboBox7.Text = "" Then
            MsgBox("紅字區資料不可空白，請仔細檢查。")
        Else
            Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 樣品訂單控制表 SET 季節 = N'" & ComboBox7.Text & "', 客戶 = N'" & ComboBox2.Text & "', 型體號碼 = N'" & ComboBox4.Text & "', 型體名稱 = N'" & ComboBox5.Text &
                   "', 國家 = N'" & ComboBox1.Text & "', 狀態 = N'" & ComboBox3.Text & "', ISS_DATE = N'" & DTP1 & "', ETA = N'" & DTP2 & "', 數量 = N'" & TextBox16.Text &
                   "', 工廠 = N'" & ComboBox8.Text & "', REQ = N'" & TextBox6.Text & "', C_N = N'" & ComboBox15.Text & "', 中底 = N'" & ComboBox16.Text & "' , 楦頭編號 = N'" & ComboBox17.Text & "' , 
                   模具編號 = N'" & ComboBox19.Text & "' 
                   WHERE SO LIKE N'" & TextBox1.Text & "' AND 版次  LIKE N'" & TextBox3.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

            新增準備4_1() : 新增準備4_2()
            新增文件 = False : Timer1.Enabled = False : Label62.Visible = False
            Button29.Enabled = True : Button5.Enabled = True : Button12.Enabled = True : Button16.Enabled = True
            Button11.Enabled = False : Button6.Enabled = False : Button7.Enabled = False : Button8.Enabled = False : Button15.Enabled = False
            MsgBox("存檔完成") : Set_清單()
        End If
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        If TextBox1.Text = "" Then
            MsgBox("沒有選擇要刪除的資料")
        Else
            Dim aa As MsgBoxResult
            aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
            If aa = MsgBoxResult.Ok Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 樣品訂單控制表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 樣品訂單清單 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "DELETE FROM 樣品訂單部件表 WHERE (SO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "')"
                cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : conn.Close()
                MsgBox("刪除完成") : Set_清單() : DataGridView2.DataSource = Nothing : ds1.Clear() : DataGridView3.DataSource = Nothing : ds2.Clear()
            End If
        End If
    End Sub
    Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
        新增準備4_2() : Set_清單2()
    End Sub
    Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
        If 列印用SQL = "" Then
            MsgBox("沒有選擇要列印的樣品單")
        Else
            列印_樣品單_NEW.ShowDialog() : 列印用SQL = "" : 列印用SQL2 = ""
        End If
    End Sub
    Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
        If 列印用SQL = "" Then
            MsgBox("沒有選擇要列印的樣品單")
        Else
            列印_樣品貼標.ShowDialog() : 列印用SQL = "" : 列印用SQL2 = ""
        End If
    End Sub
    Private Sub 轉預告單準備1()
        Dim NUM1 As Integer
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT TOP(1) PAO FROM 預告訂單控制表 WHERE PAO LIKE N'%" & Strings.Right(ComboBox10.Text, 2) & "' ORDER BY PAO DESC"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : 新流水號4 = dr("PAO") : Else : 新流水號4 = "MT00000-" & Strings.Right(ComboBox10.Text, 2) : End If : conn.Close()

        NUM1 = Double.Parse(Strings.Mid(新流水號4, 3, 5)) + 1
        If NUM1 < 10 Then : 新流水號4 = "MT" & "0000" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新流水號4 = "MT" & "000" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : 新流水號4 = "MT" & "00" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : 新流水號4 = "MT" & "0" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 9999 Then : 新流水號4 = "MT" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        End If
    End Sub
    Private Sub 轉預告單準備2()
        Dim NUM1 As Integer
        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 : 新流水號5 = dr("流水號") : Else : 新流水號5 = "SY0000000000" : End If : conn.Close()

        NUM1 = Double.Parse(Strings.Right(新流水號5, 10)) + 1
        If NUM1 < 10 Then : 新流水號5 = "SY" & "000000000" & NUM1
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新流水號5 = "SY" & "00000000" & NUM1
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : 新流水號5 = "SY" & "0000000" & NUM1
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : 新流水號5 = "SY" & "000000" & NUM1
        ElseIf NUM1 > 9999 And NUM1 < 100000 Then : 新流水號5 = "SY" & "00000" & NUM1
        ElseIf NUM1 > 99999 And NUM1 < 1000000 Then : 新流水號5 = "SY" & "0000" & NUM1
        ElseIf NUM1 > 999999 And NUM1 < 10000000 Then : 新流水號5 = "SY" & "000" & NUM1
        ElseIf NUM1 > 9999999 And NUM1 < 100000000 Then : 新流水號5 = "SY" & "00" & NUM1
        ElseIf NUM1 > 99999999 And NUM1 < 1000000000 Then : 新流水號5 = "SY" & "0" & NUM1
        ElseIf NUM1 > 999999999 Then : 新流水號5 = "SY" & NUM1
        End If
    End Sub
    Private Sub Set_DGV6載入前設定()
        DataGridView6.DataSource = Nothing : ds6.Clear()
        DataGridView6.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView6.ColumnHeadersHeight = 25
        DataGridView6.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV6載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds6) : DataGridView6.DataSource = ds6.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單5()
        Set_DGV6載入前設定()
        SQL1 = "SELECT PAO, 項次, 顏色, 雙數, SPLS, BUYING, SELLING, 流水號, 版次, 備註 FROM 預告訂單清單 WHERE (PAO LIKE N'" & 新流水號4 & "' AND 版次 LIKE N'V01') ORDER BY 項次"
        Set_DGV6載入後設定()
    End Sub
    Private Sub Button23_Click(sender As Object, e As EventArgs) Handles Button23.Click
        GroupBox1.Visible = True
    End Sub
    Private Sub Button26_Click(sender As Object, e As EventArgs) Handles Button26.Click
        If ComboBox4.Text = "" Or TextBox16.Text = "" Or ComboBox1.Text = "" Or ComboBox2.Text = "" Or ComboBox3.Text = "" Or ComboBox7.Text = "" Then
            MsgBox("紅字區資料不可空白，請仔細檢查，確定所選擇的樣品單資料正確。")
        Else
            轉預告單準備1()
            Set_日期格式轉換()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "INSERT INTO 預告訂單控制表 (PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次) " &
                   "VALUES (N'" & 新流水號4 & "', N'" & TextBox1.Text & "', N'" & ComboBox7.Text & "', N'', N'" & ComboBox2.Text & "', N'" & ComboBox8.Text & "', N'" & ComboBox4.Text &
                   "', N'" & ComboBox5.Text & "', N'0', N'" & DTP4 & "', N'" & DTP5 & "', N'" & ComboBox9.Text & "', N'', N'', N'" & gUserName & "', N'" & DTP3 & "', N'V01')"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                資料數 = DataGridView2.Rows.Count : MyModule1.進度條()
                轉預告單準備2()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 預告訂單清單 (PAO, 項次, 顏色, 雙數, SPLS, BUYING, SELLING, 流水號, 版次, 備註) VALUES (N'" & 新流水號4 & "', N'" & DataGridView2.Rows(i).Cells("項次").Value &
                       "', N'" & DataGridView2.Rows(i).Cells("顏色").Value & "', N'0', N'0', N'0', N'0', N'" & 新流水號5 & "', N'V01', N'')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                Set_清單5()
                For t As Integer = 0 To DataGridView3.Rows.Count - 1
                    If DataGridView3.Rows(t).Cells("項次").Value = DataGridView2.Rows(i).Cells("項次").Value Then
                        For ii As Integer = 0 To DataGridView6.Rows.Count - 1
                            If DataGridView6.Rows(ii).Cells("項次").Value = DataGridView2.Rows(i).Cells("項次").Value Then
                                PA = DataGridView6.Rows(ii).Cells("流水號").Value
                                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                                SQL1 = "INSERT INTO  預告訂單部件表 (PAO, 項次, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) VALUES (N'" & 新流水號4 & "',
                                        N'', N'" & DataGridView3.Rows(t).Cells("部件").Value & "', N'" & DataGridView3.Rows(t).Cells("材料名稱").Value & "', 
                                        N'" & DataGridView3.Rows(t).Cells("顏色").Value & "', N'" & DataGridView3.Rows(t).Cells("備註").Value & "', 
                                        N'" & DataGridView3.Rows(t).Cells("廠商").Value & "', N'V01', N'" & PA & "')"
                                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
                            End If
                        Next
                    End If
                Next
            Next : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
            MsgBox("單據轉制完成。")
        End If
        GroupBox1.Visible = False
    End Sub
    Private Sub Button27_Click(sender As Object, e As EventArgs) Handles Button27.Click
        GroupBox1.Visible = False
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        Set_grid()
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_清單() : PA = ""
    End Sub
    Private Sub Button32_Click(sender As Object, e As EventArgs) Handles Button32.Click
        PA = "" : Set_清單()
    End Sub
    Private Sub TextBox16_TextChanged(sender As Object, e As EventArgs) Handles TextBox16.Click
        TextBox16.Text = "0"
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            TextBox16.Text = Val(TextBox16.Text) + DataGridView2.Rows(i).Cells("數量").Value
        Next
    End Sub
    Private Sub ComboBox11_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox11.SelectedIndexChanged
        ComboBox6下拉表單資料載入()
    End Sub
    Private Sub Button30_Click(sender As Object, e As EventArgs) Handles Button30.Click
        DataGridView3.Rows(Val(TextBox8.Text) - 1).Cells("廠商").Value = ComboBox12.Text
        DataGridView3.Rows(Val(TextBox8.Text) - 1).Cells("材料名稱").Value = ComboBox13.Text
        DataGridView3.Rows(Val(TextBox8.Text) - 1).Cells("顏色").Value = ComboBox14.Text
    End Sub
    Private Sub ComboBox12_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox12.SelectedIndexChanged
        ComboBox13下拉表單資料載入() : ComboBox14下拉表單資料載入()
    End Sub
    Private Sub ComboBox15_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox15.SelectedIndexChanged
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 中皮, 結構 FROM 中皮結構 WHERE 底台 LIKE N'" & ComboBox15.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : TextBox7.Text = dr("中皮") : TextBox10.Text = dr("結構") : End If : conn.Close()
    End Sub
    Private Sub ComboBox16_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox16.SelectedIndexChanged
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 做法 FROM 中底做法 WHERE 中底 LIKE N'" & ComboBox16.Text & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        If dr.Read() Then : TextBox11.Text = dr("做法") : End If : conn.Close()
    End Sub
    Private Sub ComboBox18_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox18.SelectedIndexChanged
        ComboBox12下拉表單資料載入()
    End Sub
End Class