Imports System.Data.OleDb
Public Class Form1
Dim constr As String = "Provider=Microsoft.ACE.oledb.12.0;Data source= " & Application.StartupPath & "\db.accdb"
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim adapter As OleDbDataAdapter
Dim ds As New DataSet
Private Sub connection()
con = New OleDbConnection(constr)
With con
If .State = ConnectionState.Open Then .Close()
con.Open()
End With
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowDataInCombobox1()
End Sub
Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
connection()
Dim sql As String = ""
sql = "INSERT INTO DetailPrinter VALUES ('" & TextBoxX1.Text & "','" & ComboBoxEx1.SelectedValue & "','" & ComboBoxEx2.SelectedValue & "','" & ComboBoxEx3.SelectedValue & "')"
cmd = New OleDbCommand
With cmd
.CommandType = CommandType.Text
.Connection = con
.CommandText = sql
.ExecuteNonQuery()
End With
MsgBox("Saved")
End Sub
Private Sub ShowDataInCombobox1()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx1
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
With ComboBoxEx2
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
With ComboBoxEx3
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
Private Sub ComboBoxEx1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxEx1.SelectedIndexChanged
ComboBoxEx2.SelectedValue = ComboBoxEx1.SelectedValue
ComboBoxEx3.SelectedValue = ComboBoxEx1.SelectedValue
End Sub
End Class
Private Sub ComboBoxEx1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxEx1.SelectedIndexChanged
ComboBoxEx2.SelectedValue = ComboBoxEx1.SelectedValue
ComboBoxEx3.SelectedValue = ComboBoxEx1.SelectedValue
End Sub
ถ้า
Private Sub ComboBoxEx2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxEx2.SelectedIndexChanged
ComboBoxEx1.SelectedValue = ?
ComboBoxEx3.SelectedValue = ?
เพราะ ComboxEx2 มันก็จะแสดงชื่อหมึก เหมือนกัน
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowDataInCombobox1()
ShowDataInCombobox2()
ShowDataInCombobox3()
End Sub
Private Sub ShowDataInCombobox1()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx1
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
Private Sub ShowDataInCombobox2()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx2
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
Private Sub ShowDataInCombobox3()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx3
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
Imports System.Data.OleDb
Public Class Form1
Dim constr As String = "Provider=Microsoft.ACE.oledb.12.0;Data source= " & Application.StartupPath & "\db.accdb"
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim adapter As OleDbDataAdapter
Dim ds As New DataSet
Private Sub connection()
con = New OleDbConnection(constr)
With con
If .State = ConnectionState.Open Then .Close()
con.Open()
End With
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowDataInCombobox1()
ShowDataInCombobox2()
ShowDataInCombobox3()
End Sub
Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
connection()
Dim sql As String = ""
sql = "INSERT INTO Ink VALUES ('" & TextBoxX1.Text & "','" & ComboBoxEx1.SelectedValue & "','" & ComboBoxEx2.SelectedValue & "','" & ComboBoxEx3.SelectedValue & "')"
cmd = New OleDbCommand
With cmd
.CommandType = CommandType.Text
.Connection = con
.CommandText = sql
.ExecuteNonQuery()
End With
MsgBox("Saved")
End Sub
Private Sub ShowDataInCombobox1()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx1
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
Private Sub ShowDataInCombobox2()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx2
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
Private Sub ShowDataInCombobox3()
connection()
Dim sql As String = ""
sql = "select * from Ink"
cmd = New OleDbCommand(sql, con)
adapter = New OleDbDataAdapter(cmd)
ds = New DataSet
adapter.Fill(ds, "kk")
With ComboBoxEx3
.DataSource = ds.Tables("kk")
.ValueMember = "InkID"
.DisplayMember = "InkName"
.SelectedIndex = 0
End With
End Sub
End Class