Private Sub btnAddBank_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBank.Click
Dim fm2 As AddTransactiondata = New AddTransactiondata
fm2.ValueToPassBetweenAddSupplier = Me.txtID.Text()
fm2.ShowDialog()
End Sub
โค้ดจากฟอร์มที่ 1
Code (VB.NET)
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.OleDb
Public Class AddTransactiondata
Inherits Form
Dim Conn As New OleDbConnection
Dim strSQL As String
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim Check As Boolean
Dim Action As String
Dim IsFind As Boolean
Dim IsFinda As Boolean = False
Dim cmdTEST As OleDbCommand
Dim dr As OleDbDataReader
'*************************************************************
'***********************************************************
Private Sub SaveCancel()
cmdAdd.Enabled = True
cmdEdit.Enabled = True
cmdClear.Enabled = True
cmdSave.Enabled = False
cmdCancel.Enabled = False
End Sub
Private Sub AddEdit()
txtAccountName.Enabled = True
txtAccountNum.Enabled = True
txtBranch.Enabled = True
cmbBank.Enabled = True
txtID2.Enabled = False
cmdAdd.Enabled = False
cmdEdit.Enabled = False
cmdClear.Enabled = False
cmdSave.Enabled = True
cmdCancel.Enabled = True
End Sub
Private Sub ClearAllData()
txtAccountName.Text = ""
txtAccountNum.Text = ""
txtBranch.Text = ""
cmbBank.Text = "กรุณาเลือกธนาคาร"
txtBranch.Focus()
End Sub
Private Function CheckDataAdd() As Boolean
Check = False
If cmbBank.Text = "กรุณาเลือกธนาคาร" Or txtAccountName.Text = "" Or txtBranch.Text = "" Or txtAccountNum.Text = "" Then
MessageBox.Show("กรุณากรอกข้อมูลให้ครบ ", "ข้อมูลไม่ครบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
Else
Return True
End If
End Function
Private Function CheckDataEdit() As Boolean
Check = False
If cmbBank.Text = "กรุณาเลือกธนาคาร" Or txtAccountName.Text = "" Or txtBranch.Text = "" Or txtAccountNum.Text = "" Then
MessageBox.Show("กรุณาเลือกรายการเพื่อทำการแก้ไข ", "ยังไม่เลือกรายการ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
Else
Return True
End If
End Function
Private Sub CheckData()
Try
Dim sql As String = ""
sql = "SELECT BranchName,AccountName,AccountNum FROM Bank WHERE BranchName ='" & txtBranch.Text & "'"
sql &= "AND AccountName ='" & txtAccountName.Text & "'AND AccountNum ='" & txtAccountNum.Text & "'"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
cmdTEST = New OleDbCommand
With cmdTEST
.CommandType = CommandType.Text
.CommandText = sql
.Connection = Conn
.Parameters.Clear()
dr = .ExecuteReader()
If dr.Read Then
MessageBox.Show("มีข้อมูลแล้ว", "information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Dim Add As String = ""
Add = "INSERT INTO BankList(BankID,BranchName,AccountName,AccountNum,SuppID )"
Add &= "VALUES ('" & CStr(cmbBank.SelectedValue) & "',"
Add &= "'" & txtBranch.Text & " ',"
Add &= "'" & txtAccountName.Text & " ',"
Add &= "'" & txtAccountNum.Text & " ',"
Add &= "'" & txtID2.Text & " ')"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
cmdTEST = New OleDbCommand
With cmdTEST
.CommandType = CommandType.Text
.CommandText = Add
.Connection = Conn
.ExecuteNonQuery()
End With
MessageBox.Show("บันทึกลงฐานข้อมูลเรียบร้อยแล้ว ", "บันทึก", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
Catch ErrorToAdd As Exception
MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ เนื่องจาก " & ErrorToAdd.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
cmbBank.Focus()
cmbBank.SelectAll()
End Try
End Sub
Public Sub New()
MyBase.New()
InitializeComponent()
AddHandler Load, AddressOf Me.Form2PassValue_Load
End Sub
Sub comboSearch()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
Dim sqlProductType As String = ""
sqlProductType = "SELECT * FROM Bank"
sqlProductType &= " ORDER BY BankName"
da = New OleDbDataAdapter(sqlProductType, Conn)
da.Fill(ds, "Bank")
If ds.Tables("Bank").Rows.Count <> 0 Then
With cmbBank
.DataSource = ds.Tables("Bank")
.DisplayMember = "BankName"
.ValueMember = "BankID"
End With
End If
End Sub
Private Sub Form2PassValue_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.txtID2.Text = Me._valueToPassBetweenAddSupplier
End Sub
Private _valueToPassBetweenAddSupplier As String
Public Property ValueToPassBetweenAddSupplier() As String
Get
Return Me._valueToPassBetweenAddSupplier
End Get
Set(ByVal value As String)
Me._valueToPassBetweenAddSupplier = value
End Set
End Property
Private Sub AddTransactiondata_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
SaveCancel()
comboSearch()
cmbBank.Text = "กรุณาเลือกธนาคาร"
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
AddEdit()
txtAccountName.Focus()
txtAccountName.SelectAll()
Action = "ADD"
End Sub
Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
If CheckDataEdit() = True Then
AddEdit()
txtID2.Enabled = False
txtAccountName.Focus()
txtAccountName.SelectAll()
Action = "EDIT"
End If
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
Dim Delete As String = ""
Delete = "DELETE FROM BankList WHERE BankID = '" & cmbBank.Text & "'"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
cmdTEST = New OleDbCommand
With cmdTEST
.CommandType = CommandType.Text
.CommandText = Delete
.Connection = Conn
.ExecuteNonQuery()
End With
MessageBox.Show("ลบออกจากฐานข้อมูลเรียบร้อยแล้ว ", "บันทึก", MessageBoxButtons.OK, MessageBoxIcon.Information)
ClearAllData()
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
If CheckDataAdd() = True Then
Select Case Action
Case "ADD"
CheckData()
Case "EDIT"
Dim Update As String = ""
Dim tmpRowsAffected As Long = 0
Update = "UPDATE BankList "
Update &= " SET BranchName='" & txtBranch.Text & "'"
Update &= " SET AccountName='" & txtAccountName.Text & "'"
Update &= " SET AccountNum='" & txtAccountNum.Text & "'"
Update &= " SET BankID='" & cmbBank.Text & "'"
Update &= " WHERE (SuppID='" & txtID2.Text & "')"
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strcon
.Open()
End With
cmdTEST = New OleDbCommand
With cmdTEST
.CommandType = CommandType.Text
.CommandText = Update
.Connection = Conn
tmpRowsAffected = .ExecuteNonQuery()
End With
If tmpRowsAffected = 0 Then
MessageBox.Show("รหัสที่คุณป้อนไม่มีในระบบ", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtID2.Focus()
txtID2.SelectAll()
Exit Sub
End If
Catch ErrorToEdit As Exception
MessageBox.Show("ไม่สามารถแก้ไขข้อมูลได้ เนื่องจาก " & ErrorToEdit.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtID2.Focus()
txtID2.SelectAll()
Exit Sub
End Try
MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว ", "บันทึก", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Select
SaveCancel()
ClearAllData()
txtAccountName.Enabled = False
txtAccountNum.Enabled = False
txtBranch.Enabled = False
cmbBank.Enabled = False
End If
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
SaveCancel()
ClearAllData()
txtAccountName.Enabled = False
txtAccountNum.Enabled = False
txtBranch.Enabled = False
cmbBank.Enabled = False
End Sub
End Class