Sub SaveItemHeader()
'///////////////////////////////////////////////////
Dim sb As New StringBuilder
Dim com As New OleDbCommand
Me.Cursor = Cursors.WaitCursor
Try
Me.ConnectDB()
'///////////////////////////////////////////////////
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Buy(Code_Buy,Cat,Date1,Date2,Reason,Checks)")
sb.Append(" VALUES (@CodeBuy,@Cat,@Date1,@Date2,@Reason,@Checks)")
Dim sqlsave As String
sqlsave = sb.ToString()
With com
.CommandText = sqlsave
.Parameters.Clear()
.Parameters.AddWithValue("@CodeBuy", OleDbType.VarChar).Value = Me.TextBox3.Text
.Parameters.AddWithValue("@Cat", OleDbType.VarChar).Value = Me.ComboBox1.SelectedValue
.Parameters.AddWithValue("@Date1", OleDbType.Date).Value = Me.DateTimePicker1.Value.Date
.Parameters.AddWithValue("@Date2", OleDbType.Date).Value = Me.DateTimePicker2.Value.Date
.Parameters.AddWithValue("@Reason", OleDbType.VarChar).Value = Val(Me.TextBox1.Text)
.Parameters.AddWithValue("@Checks", OleDbType.VarChar).Value = Val(Me.TextBox2.Text)
.CommandType = CommandType.Text
.Connection = Conn
.ExecuteNonQuery()
End With
Me.ClearItemDetial()
Me.Cursor = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error ไม่สามารถบันทึกข้อมูลได้")
Exit Try
Exit Sub
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
เป็นการ error เมื่อคลิกที่ปุ่มแก้ไขค่ะ
Code (VBScript) โค้ดตรงส่วนของการUpdate
Sub UpdateItem()
'///////////////////////////////////////////////////
Dim sb As New StringBuilder
Dim com As New OleDbCommand
Me.Cursor = Cursors.WaitCursor
Try
Me.ConnectDB()
Dim sqlSave As String
sqlSave = sb.ToString()
'///////////////////////////////////////////////////
sb.Remove(0, sb.Length)
sb.Append("UPDATE Buy")
sb.Append(" SET Cat=@Cat,")
sb.Append(" Date1=@Date1,")
sb.Append(" Date2=@Date2,")
sb.Append(" Reason=@Reason,")
sb.Append(" Checks=@Checks,")
sb.Append(" Code_Buy=@CodeBuy,")
sqlSave = sb.ToString()
With com
.CommandText = sqlSave
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.AddWithValue("@Cat", OleDbType.VarChar).Value = Me.ComboBox1.SelectedValue
.Parameters.AddWithValue("@Date1", OleDbType.Date).Value = Format(Me.DateTimePicker1.Value, "dd/MM/yyyy")
.Parameters.AddWithValue("@Reason", OleDbType.VarChar).Value = Val(Me.TextBox1.Text)
.Parameters.AddWithValue("@Date2", OleDbType.Date).Value = Format(Me.DateTimePicker2.Value, "dd/MM/yyyy")
.Parameters.AddWithValue("@Checks", OleDbType.VarChar).Value = Val(Me.TextBox2.Text)
.Parameters.AddWithValue("@CodeBuy", OleDbType.VarChar).Value = Trim(Me.TextBox3.Text)
.ExecuteNonQuery()
End With
Me.Cursor = Cursors.Default
Me.ClearItemDetial()
Me.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error ไม่สามารถปรับปรุงรายการได้zzzzzzzzzzzz")
Exit Try
Exit Sub
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Dim sb As New StringBuilder
Dim com As New SqlCommand
Dim Conn As New SqlConnection()
Dim strCon As String = "Data Source=Database\SQLEXPRESS;Initial Catalog=DatabaseName;User ID=xx;Password=xxx;Integrated Security=false"
Dim sqlSave As String
sb = New StringBuilder
sb.Append("UPDATE Buy")
sb.Append(" SET Cat=@Cat,")
sb.Append(" Date1=@Date1,")
sb.Append(" Date2=@Date2,")
sb.Append(" Reason=@Reason,")
sb.Append(" Checks=@Checks,")
sb.Append(" Code_Buy=@CodeBuy")
sqlSave = sb.ToString()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strCon
.Open()
End With
Try
With com
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.CommandText = sqlSave
.Transaction = tr
.Parameters.Add("@Cat", SqlDbType.VarChar).Value = ComboBox1.SelectedValue
.Parameters.Add("@Date1", SqlDbType.Date).Value = Format(Me.DateTimePicker1.Value, "dd/MM/yyyy")
.Parameters.Add("@Reason", SqlDbType.VarChar).Value = Val(Me.TextBox1.Text)
.Parameters.Add("@Date2", SqlDbType.Date).Value = Format(Me.DateTimePicker2.Value, "dd/MM/yyyy")
.Parameters.Add("@Checks", SqlDbType.VarChar).Value = Val(Me.TextBox2.Text)
.Parameters.Add("@CodeBuy", SqlDbType.VarChar).Value = Trim(Me.TextBox3.Text)
.ExecuteNonQuery()
MsgBox("บันทึกข้อมูลเรียร้อยแล้ว", MsgBoxStyle.Information, "ระบบแจ้งว่า")
End With
Catch ex As Exception
MsgBox("พบข้อผิดพลาดระหว่างบันทึกข้อมูล", MsgBoxStyle.Exclamation, "ระบบแจ้งว่า")
End Try