Option Strict Off Imports System.Data.SqlClient Public Class 匯率修改 Dim conn As New SqlConnection : Dim conn1 As New SqlConnection : Dim conn2 As New SqlConnection Dim da As New SqlDataAdapter : Dim wsx As New SqlDataAdapter : Dim da2 As New SqlDataAdapter Dim cmd As New SqlCommand : Dim cmd1 As New SqlCommand : Dim cmd2 As New SqlCommand Dim dr As SqlDataReader : Dim dr1 As SqlDataReader Dim zaq As New DataSet : Dim ds2 As DataSet Private Sub Set_grid() DataGridView4.Columns(0).FillWeight = 60 : DataGridView4.Columns(1).FillWeight = 60 DataGridView4.Columns(2).FillWeight = 60 : DataGridView4.Columns(3).FillWeight = 60 DataGridView4.Columns(4).FillWeight = 60 : DataGridView4.Columns(5).FillWeight = 60 End Sub Private Sub Set_日期() TextBox4.Text = Format(DateTimePicker1.Value, "yyyy") : TextBox5.Text = Format(DateTimePicker1.Value, "MM") End Sub Private Sub Set_歷史匯率清單() DataGridView4.DataSource = Nothing : zaq.Clear() DataGridView4.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing DataGridView4.ColumnHeadersHeight = 25 DataGridView4.AllowUserToAddRows = False If conn2.State = ConnectionState.Closed Then : conn2.ConnectionString = ConString : conn2.Open() : End If SQL2 = "SELECT 年, 月, 買進匯率, 賣出匯率, 中心匯率, 美金匯率 FROM 歷史匯率表 ORDER BY 年 DESC, 月 DESC" cmd2.Connection = conn2 : cmd2.CommandText = SQL2 : wsx.SelectCommand = cmd2 : wsx.Fill(zaq) : DataGridView4.DataSource = zaq.Tables(0) : conn2.Close() End Sub Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellClick If e.RowIndex = -1 Then : Else TextBox2.Text = DataGridView4(2, e.RowIndex).Value.ToString TextBox3.Text = DataGridView4(3, e.RowIndex).Value.ToString TextBox1.Text = DataGridView4(4, e.RowIndex).Value.ToString TextBox6.Text = DataGridView4(5, e.RowIndex).Value.ToString End If End Sub Private Sub Set_日期寫入() Set_日期() If conn1.State = ConnectionState.Closed Then : conn1.ConnectionString = ConString : conn1.Open() : End If SQL1 = "SELECT 買進匯率, 賣出匯率, 中心匯率 FROM 歷史匯率表 WHERE (年 LIKE '" & TextBox4.Text & "%') AND (月 LIKE '" & TextBox5.Text & "%')" cmd1.Connection = conn1 : cmd1.CommandText = SQL1 : dr1 = cmd1.ExecuteReader If dr1.Read() Then If conn.State = ConnectionState.Closed Then : conn.Open() : End If SQL = "UPDATE 歷史匯率表 SET 買進匯率 = '" & TextBox2.Text & "', 賣出匯率 = '" & TextBox3.Text & "', 中心匯率 = '" & TextBox1.Text & "', 美金匯率 = '" & TextBox6.Text & "' WHERE (年 LIKE '" & TextBox4.Text & "%') AND (月 LIKE '" & TextBox5.Text & "%')" cmd.Connection = conn : cmd.CommandText = SQL : cmd.ExecuteNonQuery() conn.Close() Else If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If SQL = "INSERT INTO 歷史匯率表 (年, 月, 買進匯率, 賣出匯率, 中心匯率, 美金匯率) VALUES ('" & TextBox4.Text & "','" & TextBox5.Text & " ','" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox1.Text & "', '" & TextBox6.Text & "')" cmd.Connection = conn : cmd.CommandText = SQL : cmd.ExecuteNonQuery() conn.Close() End If conn1.Close() MsgBox("完成") End Sub Private Sub 匯率修改_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DateTimePicker1.Value = Today If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If SQL = "SELECT 匯率 FROM 匯率表 WHERE (對應 = '人民幣兌台幣')" cmd.Connection = conn : cmd.CommandText = SQL : dr = cmd.ExecuteReader If dr.Read() Then : TextBox1.Text = dr("匯率").ToString : End If If TextBox1.Text = "" Then : TextBox1.Text = "0.00" : End If conn.Close() : dr.Close() If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If SQL = "SELECT 買進匯率, 賣出匯率, 美金匯率 FROM 歷史匯率表 WHERE (年 LIKE '" & TextBox4.Text & "%') AND (月 LIKE '" & TextBox5.Text & "%')" cmd.Connection = conn : cmd.CommandText = SQL : dr = cmd.ExecuteReader If dr.Read() Then : TextBox2.Text = dr("買進匯率").ToString : TextBox3.Text = dr("賣出匯率").ToString : TextBox3.Text = dr("美金匯率").ToString : End If If TextBox1.Text = "" Then : TextBox1.Text = "0.00" : End If conn.Close() : dr.Close() Set_歷史匯率清單() : Set_grid() : Set_日期() End Sub Private Sub 登入閒置控制_MouseMove(sender As Object, e As EventArgs) Handles MyBase.MouseMove timeNow = 0 GCM_ERP_SYS.Timer1.Enabled = False GCM_ERP_SYS.Timer1.Enabled = True End Sub Private Sub 登入閒置控制_KeyPress(sender As Object, e As EventArgs) Handles MyBase.KeyPress timeNow = 0 GCM_ERP_SYS.Timer1.Enabled = False GCM_ERP_SYS.Timer1.Enabled = True End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Enabled = True : TextBox2.Enabled = True : TextBox3.Enabled = True : TextBox6.Enabled = True : Button4.Enabled = True : Button5.Enabled = True End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox6.Enabled = False : Button4.Enabled = False : Button5.Enabled = False End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then MsgBox("輸入資料有誤") Else If CheckBox1.Checked = False Then DateTimePicker1.Value = Today Set_日期寫入() If conn.State = ConnectionState.Closed Then : conn.Open() : End If SQL = "UPDATE 匯率表 SET 匯率 = '" & TextBox1.Text & "' WHERE (對應 = '人民幣兌台幣')" cmd.CommandText = SQL : cmd.ExecuteNonQuery() conn.Close() conn1.Close() : dr1.Close() Else Set_日期寫入() End If End If TextBox1.Enabled = False : TextBox2.Enabled = False : TextBox3.Enabled = False : TextBox6.Enabled = False : Button4.Enabled = False : Button5.Enabled = False Set_歷史匯率清單() : Set_grid() : Set_日期() End Sub Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged If TextBox2.Text <> "" And TextBox3.Text <> "" Then TextBox1.Text = (Val(TextBox2.Text) + Val(TextBox3.Text)) / 2 End If End Sub Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged If TextBox2.Text <> "" And TextBox3.Text <> "" Then TextBox1.Text = (Val(TextBox2.Text) + Val(TextBox3.Text)) / 2 End If End Sub End Class