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 : ReadOnly 新流水號1 As String : Dim 新流水號3 As String : ReadOnly 新流水號4 As String : Dim 新流水號5 As String
    Dim DTP1 As String : Dim DTP2 As String : Dim DTP3 As String : ReadOnly DTP4 As String
    ReadOnly DTP5 As String : Dim 新項次 As String
    Dim NUM1 As Integer
    Dim 新增文件 As Boolean
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.RowTemplate.Height = 50
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        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_清單()
        Set_DGV1載入前設定()
        If PA = "" Then
            SQL1 = "SELECT PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次, 訂單屬性 
                    FROM 預告訂單控制表 ORDER BY 季節, PAO DESC, 版次 DESC"
        Else
            SQL1 = "SELECT PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次, 訂單屬性 FROM 預告訂單控制表 " &
                   "WHERE (SO LIKE N'%" & PA & "%') OR (季節 LIKE N'%" & PA & "%') OR (客戶 LIKE N'%" & PA & "%') OR (型體號碼 LIKE N'%" & PA & "%') OR (型體名稱 LIKE N'%" & PA & "%') " &
                   "OR (ISSUE_DATE LIKE N'%" & PA & "%') OR (ETD LIKE N'%" & PA & "%') OR (FOB LIKE N'%" & PA & "%') OR (ConstructionName LIKE N'%" & PA & "%') OR (PS LIKE N'%" & PA & "%') " &
                   "OR (制單人 LIKE N'%" & PA & "%') ORDER BY  季節, PAO DESC, 版次 DESC"
        End If
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        If CheckBox1.Checked = False Then
            DataGridView1.Size = New Size(475, 925)
            DataGridView1.Columns(0).Width = 85 : DataGridView1.Columns(1).Width = 55 : DataGridView1.Columns(2).Width = 65 : DataGridView1.Columns(4).Width = 100 : DataGridView1.Columns(6).Width = 100
            DataGridView1.Columns(7).Visible = False
        Else
            DataGridView1.Size = New Size(745, 925)
            DataGridView1.Columns(0).Width = 85 : DataGridView1.Columns(1).Width = 55 : DataGridView1.Columns(2).Width = 65 : DataGridView1.Columns(4).Width = 170 : DataGridView1.Columns(6).Width = 150
            DataGridView1.Columns(7).Visible = True : DataGridView1.Columns(7).Width = 150
        End If
        DataGridView1.Columns(3).Visible = False : DataGridView1.Columns(5).Visible = False : DataGridView1.Columns(8).Visible = False
        DataGridView1.Columns(9).Visible = False : DataGridView1.Columns(10).Visible = False : DataGridView1.Columns(11).Visible = False : DataGridView1.Columns(12).Visible = False
        DataGridView1.Columns(13).Visible = False : DataGridView1.Columns(14).Visible = False : DataGridView1.Columns(15).Visible = False : DataGridView1.Columns(16).Width = 45
        DataGridView1.Columns(17).Visible = False
    End Sub
    Private Sub Set_DGV2載入前設定()
        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
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close()
        Set_DGV2下拉式清單1() : Set_grid1()
    End Sub
    Private Sub Set_清單1()
        Set_DGV2載入前設定()
        SQL1 = "SELECT 項次, 顏色, 雙數, SPLS, BUYING AS [BUYING($)], SELLING, 版次, 流水號 FROM 預告訂單清單 WHERE (PAO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "') ORDER BY 項次"
        列印用SQL = TextBox1.Text : 列印用SQL2 = TextBox3.Text
        Set_DGV2載入後設定()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Width = 40 : DataGridView2.Columns(1).Visible = False : DataGridView2.Columns(2).Width = 160 : DataGridView2.Columns(3).Width = 72 : DataGridView2.Columns(4).Width = 71
        DataGridView2.Columns(5).Width = 72 : DataGridView2.Columns(6).Width = 71 : DataGridView2.Columns(7).Visible = False : DataGridView2.Columns(8).Visible = False
        DataGridView2.Columns(3).DefaultCellStyle.Format = "#,##0.0" : DataGridView2.Columns(5).DefaultCellStyle.Format = "#,##0.00"
        DataGridView2.Columns(4).DefaultCellStyle.Format = "#,##0.00" : DataGridView2.Columns(6).DefaultCellStyle.Format = "#,##0.00"
        DataGridView2.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    Private Sub Set_DGV3載入前設定()
        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
    End Sub
    Private Sub Set_DGV3載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView3.DataSource = ds2.Tables(0) : conn.Close()
        Set_DGV2下拉式清單3() : Set_grid2()
    End Sub
    Private Sub Set_清單2()
        Set_DGV3載入前設定()
        SQL1 = "SELECT 預告訂單清單.項次, 預告訂單部件表.部件, 預告訂單部件表.材料名稱, 預告訂單部件表.材料說明 AS 顏色, 預告訂單部件表.廠商, 預告訂單部件表.備註, " &
               "預告訂單部件表.版次, 預告訂單部件表.流水號, 預告訂單部件表.項次 AS 排列 " &
               "FROM 預告訂單部件表 INNER JOIN 預告訂單清單 ON 預告訂單部件表.流水號 = 預告訂單清單.流水號 " &
               "WHERE (預告訂單部件表.PAO LIKE N'" & TextBox1.Text & "') AND (預告訂單部件表.版次 LIKE N'" & TextBox3.Text & "') ORDER BY   預告訂單部件表.流水號, 預告訂單部件表.項次, 預告訂單部件表.部件"
        Set_DGV3載入後設定()
    End Sub
    Private Sub Set_grid2()
        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
    End Sub
    Private Sub Set_DGV4載入前設定()
        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
    End Sub
    Private Sub Set_DGV4載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds4) : DataGridView4.DataSource = ds4.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_清單3()
        Set_DGV4載入前設定()
        SQL1 = "SELECT 部件 FROM 預告訂單部件表 WHERE (PAO LIKE N'" & TextBox1.Text & "') AND (版次 LIKE N'" & TextBox3.Text & "') GROUP BY 部件"
        Set_DGV4載入後設定()
    End Sub
    Private Sub Set_DGV5載入前設定()
        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
    End Sub
    Private Sub Set_DGV5載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds5) : DataGridView5.DataSource = ds5.Tables(0) : conn.Close() : Set_grid3()
    End Sub
    Private Sub Set_清單4()
        Set_DGV5載入前設定()
        SQL1 = "SELECT 項次, 備註 FROM 預告訂單清單 WHERE (PAO LIKE N'" & TextBox1.Text & "' AND 版次 LIKE N'" & TextBox3.Text & "') ORDER BY 項次"
        Set_DGV5載入後設定()
    End Sub
    Private Sub Set_grid3()
        DataGridView5.Columns(0).Width = 40 : DataGridView5.Columns(1).Width = 1343
        DataGridView3.EditMode = DataGridViewEditMode.EditOnEnter
    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_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 ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT  FOB FROM 港口清單"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear() : While (dr.Read()) : ComboBox1.Items.Add(dr("FOB")) : 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  CUST FROM MTTOOCUST清單 ORDER BY CUST"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox2.Items.Clear() : While (dr.Read()) : ComboBox2.Items.Add(dr("CUST")) : 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 客戶控制表 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'" & ComboBox3.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 部件清單 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 Set_日期格式轉換()
        DTP1 = DateTimePicker2.Text : DTP2 = DateTimePicker1.Text : DTP3 = Format(Today(), "yyyy/MM/dd") & "-" & Format(TimeOfDay(), "hh:mm:ss")
        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
    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下拉表單資料載入()
        ComboBox7下拉表單資料載入() : ComboBox8下拉表單資料載入()
        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 : TextBox7.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 : ComboBox10.Visible = False
        ComboBox10.Text = Year(Today)
        Set_清單()
    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 (PAO 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 (PAO 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 (PAO 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 DataGridView1.RowPostPaint
        Dim linePen As New Pen(Color.Blue, 2)
        If e.RowIndex = DataGridView1.Rows.Count - 1 Then
            Exit Sub
        Else
            If DataGridView1(0, e.RowIndex).Value.ToString <> DataGridView1(0, e.RowIndex + 1).Value.ToString Then
                Dim startX As Integer = IIf(DataGridView1.RowHeadersVisible, DataGridView1.RowHeadersWidth, 0)
                Dim startY As Integer = e.RowBounds.Top + e.RowBounds.Height - 1
                Dim endX As Integer = startX + DataGridView1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - DataGridView1.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 DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            If Button11.Enabled = False Then
                ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox7下拉表單資料載入() : ComboBox8下拉表單資料載入()

                TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("PAO").Value : TextBox7.Text = DataGridView1.Rows(e.RowIndex).Cells("SO").Value
                ComboBox7.Text = DataGridView1.Rows(e.RowIndex).Cells("季節").Value : ComboBox8.Text = DataGridView1.Rows(e.RowIndex).Cells("工廠").Value
                ComboBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("客戶").Value : ComboBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("BUYER").Value
                ComboBox4.Text = DataGridView1.Rows(e.RowIndex).Cells("型體號碼").Value : TextBox16.Text = DataGridView1.Rows(e.RowIndex).Cells("總數量").Value
                TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("版次").Value : ComboBox5.Text = DataGridView1.Rows(e.RowIndex).Cells("型體名稱").Value
                ComboBox1.Text = DataGridView1.Rows(e.RowIndex).Cells("FOB").Value : TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells("ConstructionName").Value
                TextBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("制單人").Value : DateTimePicker3.Value = Strings.Left(DataGridView1.Rows(e.RowIndex).Cells("制單時間").Value, 10)
                TextBox8.Text = DataGridView1.Rows(e.RowIndex).Cells("PS").Value
                If DataGridView1.Rows(e.RowIndex).Cells("訂單屬性").Value.ToString = "" Then
                    ComboBox9.Text = "NA"
                Else
                    ComboBox9.Text = DataGridView1.Rows(e.RowIndex).Cells("訂單屬性").Value
                End If


                DTP1 = DataGridView1.Rows(e.RowIndex).Cells("ISSUE_DATE").Value
                If DTP1 = "" Then : DateTimePicker2.Format = DateTimePickerFormat.Custom : DateTimePicker2.CustomFormat = " "
                Else : DateTimePicker2.Format = DateTimePickerFormat.Short : DateTimePicker2.Text = DTP1 : End If

                DTP2 = DataGridView1.Rows(e.RowIndex).Cells("ETD").Value
                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'" & DataGridView1.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(e.RowIndex).Cells("型體號碼").Value &
                               "' AND 形體名 LIKE N'" & DataGridView1.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'" & DataGridView1.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.Rows(e.RowIndex).Cells("型體號碼").Value &
                                   "' AND 形體名 LIKE N'" & DataGridView1.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'" & DataGridView1.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.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'" & DataGridView1.Rows(e.RowIndex).Cells("客戶").Value & "' AND 形體號 LIKE N'" & DataGridView1.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'" & DataGridView1.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'" & DataGridView1.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.Rows(e.RowIndex).Cells("項次").Value : TextBox5.Text = DataGridView2.Rows(e.RowIndex).Cells("流水號").Value
            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'" & ComboBox2.Text & "' AND 形體號 LIKE N'" & ComboBox4.Text &
                       "' AND 形體名 LIKE N'" & ComboBox5.Text & "' AND 顏色 LIKE N'" & DataGridView2.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'" & ComboBox2.Text & "' AND 形體號 LIKE N'" & ComboBox4.Text &
                       "' AND 形體名 LIKE N'" & ComboBox5.Text & "' AND 顏色 LIKE N'" & DataGridView2.Rows(e.RowIndex).Cells("顏色").Value & "'"
                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
                conn.Close()
            End If : PictureBox1.SizeMode = 4
            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
        End If
    End Sub
    Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
        ComboBox4下拉表單資料載入()
    End Sub
    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
        ComboBox5下拉表單資料載入()
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click
        Set_grid()
    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 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 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 新增準備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 : 新流水號 = dr("PAO") : Else : 新流水號 = "MT00000-" & Strings.Right(ComboBox10.Text, 2) : End If : conn.Close()

        NUM1 = Double.Parse(Strings.Mid(新流水號, 3, 5)) + 1
        If NUM1 < 10 Then : 新流水號 = "MT" & "0000" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 9 And NUM1 < 100 Then : 新流水號 = "MT" & "000" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 99 And NUM1 < 1000 Then : 新流水號 = "MT" & "00" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 999 And NUM1 < 10000 Then : 新流水號 = "MT" & "0" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        ElseIf NUM1 > 9999 Then : 新流水號 = "MT" & NUM1 & "-" & Strings.Right(ComboBox10.Text, 2)
        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 預告訂單控制表 (PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次, 訂單屬性) " &
               "VALUES (N'" & TextBox1.Text & "', N'" & TextBox7.Text & "', N'" & ComboBox7.Text & "', N'" & ComboBox2.Text & "', N'" & ComboBox3.Text & "', N'" & ComboBox8.Text & "', N'" & ComboBox4.Text &
               "', N'" & ComboBox5.Text & "', N'" & TextBox16.Text & "', N'" & DTP2 & "', N'" & DTP1 & "', N'" & ComboBox1.Text & "', N'" & TextBox6.Text & "', N'" & TextBox8.Text & "', N'" & gUserName &
               "', N'" & DTP3 & "', N'V01', N'" & ComboBox9.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 預告訂單清單 (PAO, 項次, 顏色, 雙數, SPLS, BUYING, SELLING, 流水號, 版次, 備註) " &
               "VALUES (N'" & TextBox1.Text & "', N'01', N'', N'0', N'0', N'0', N'0', N'" & 新流水號5 & "', N'" & TextBox3.Text & "', N'')"
        cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Set_清單1() : Set_清單4()
        MsgBox("資料新增準備完成")
    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 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" : TextBox6.Text = "" : TextBox8.Text = ""
            新增準備1()
            DateTimePicker1.Format = DateTimePickerFormat.Custom : DateTimePicker1.CustomFormat = " " : DateTimePicker2.Format = DateTimePickerFormat.Custom : DateTimePicker2.CustomFormat = " "
            ComboBox1下拉表單資料載入() : ComboBox2下拉表單資料載入() : ComboBox3下拉表單資料載入() : ComboBox4下拉表單資料載入()
            ComboBox5下拉表單資料載入() : ComboBox6下拉表單資料載入() : ComboBox7下拉表單資料載入() : ComboBox8下拉表單資料載入()
            TextBox2.Text = gUserName : TextBox4.Text = "" : TextBox5.Text = "" : TextBox16.Text = "0" : TextBox17.Text = "" : TextBox7.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 預告訂單控制表 (PAO, SO, 季節, 客戶, BUYER, 工廠, 型體號碼, 型體名稱, 總數量, ETD, ISSUE_DATE, FOB, ConstructionName, PS, 制單人, 制單時間, 版次, 訂單屬性) " &
               "VALUES (N'" & TextBox1.Text & "', N'" & TextBox7.Text & "', N'" & ComboBox7.Text & "', N'" & ComboBox2.Text & "', N'" & ComboBox3.Text & "', N'" & ComboBox8.Text & "', N'" & ComboBox4.Text &
               "', N'" & ComboBox5.Text & "', N'" & TextBox16.Text & "', N'" & DTP2 & "', N'" & DTP1 & "', N'" & ComboBox1.Text & "', N'" & TextBox6.Text & "', N'" & TextBox8.Text & "', N'" & gUserName &
               "', N'" & DTP3 & "', N'" & TextBox3.Text & "', N'" & ComboBox9.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 預告訂單清單 (PAO, 項次, 顏色, 雙數, SPLS, BUYING, SELLING, 流水號, 版次, 備註) " &
               "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("SPLS").Value & "', N'" & DataGridView2.Rows(i).Cells("BUYING($)").Value & "', N'" & DataGridView2.Rows(i).Cells("SELLING").Value &
                    "', N'" & 新流水號5 & "', N'" & TextBox3.Text & "', 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  預告訂單部件表 (PAO, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) 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 (PAO 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 (PAO 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 ComboBox5.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 SO = N'" & TextBox7.Text & "',季節 = N'" & ComboBox7.Text & "', 客戶 = N'" & ComboBox2.Text & "', 型體號碼 = N'" & ComboBox4.Text &
                   "', 型體名稱 = N'" & ComboBox5.Text &
                   "', FOB = N'" & ComboBox1.Text & "', BUYER = N'" & ComboBox3.Text & "', ISSUE_DATE = N'" & DTP1 & "', ETD = N'" & DTP2 & "', 總數量 = N'" & TextBox16.Text &
                   "', 工廠 = N'" & ComboBox8.Text & "', ConstructionName = N'" & TextBox6.Text & "', PS = N'" & TextBox8.Text & "', 訂單屬性 = N'" & ComboBox9.Text & "'
                   WHERE PAO LIKE N'" & TextBox1.Text & "' AND 版次  LIKE N'" & TextBox3.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()

            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 訂單控制表 SET 型體號碼 = N'" & ComboBox4.Text & "', GOODBOX = N'" & ComboBox5.Text & "' WHERE 訂單種類 LIKE N'" & TextBox1.Text & "-" & 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 (PAO 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 (PAO 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 (PAO 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 新增準備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 &
                       "',SPLS = N'" & DataGridView2.Rows(i).Cells("SPLS").Value & "',BUYING = N'" & DataGridView2.Rows(i).Cells("BUYING($)").Value & "',SELLING = N'" & DataGridView2.Rows(i).Cells("SELLING").Value &
                       "' WHERE (PAO 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 (PAO 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 新增準備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 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 PAO 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 預告訂單清單 (PAO, 項次, 顏色, 雙數, SPLS, BUYING, SELLING, 流水號, 版次, 備註) " &
               "VALUES (N'" & TextBox1.Text & "', N'" & 新項次 & "', N'', N'0', N'0', N'0', N'0', N'" & 新流水號5 & "', N'" & TextBox3.Text & "', 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 預告訂單部件表 (PAO, 項次, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) VALUES (N'" & TextBox1.Text & "', N'', N'" & DataGridView4.Rows(i).Cells("部件").Value &
                   "', N'', N'', N'', N'', N'" & TextBox3.Text & "', N'" & 新流水號5 & "')"
            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 (PAO 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 (PAO 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 (PAO 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 (PAO 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 預告訂單部件表 (PAO, 部件, 材料名稱, 材料說明, 備註, 廠商, 版次, 流水號) 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 (PAO 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 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
            列印_預告訂單.ShowDialog() : 列印用SQL = "" : 列印用SQL2 = ""
        End If
    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 DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        DateTimePicker2.Format = DateTimePickerFormat.Short
    End Sub
    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        DateTimePicker1.Format = DateTimePickerFormat.Short
    End Sub
End Class