Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
Dim sql As String
sql = "SELECT * FROM ServiceCharge"
Dim da As New OleDbDataAdapter(sql, cn)
Dim ds As New DataSet
da.Fill(ds, "ServiceCharge")
ComboBox1.DataSource = ds.Tables("ServiceCharge")
ComboBox1.DisplayMember = "ServiceCharge"
ComboBox1.ValueMember = "ServiceCharge"
Dim sql2 As String
sql2 = "SELECT * FROM ServiceCharge where ServiceCharge = '" & CStr(ComboBox1.Text) & "'"
Dim da2 As New OleDbDataAdapter(sql2, cn)
Dim ds2 As New DataSet
da2.Fill(ds2, "ServiceCharge")
TextBox4.Text = CStr(ds2.Tables("ServiceCharge").Rows(0).Item("Price"))
End Sub
Private Sub frmDailyWork_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Dim strPath As String = Application.StartupPath
strPath = strPath.ToLower()
Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|data\Database.accdb; Persist Security Info = false;"
Try
' เปิดการเชื่อมต่อไฟล์ฐานข้อมูล
Conn = New OleDb.OleDbConnection(strConn)
Conn.Open()
'// โหลดรายการจังหวัดเข้ามาก่อน
Call LoadProvice()
Catch ex As Exception
MsgBox(ex.Message)
Application.Exit()
End Try
End Sub
Sub LoadProvice()
' DISTINCT คือ หากชื่อรายการมันซ้ำ ต้องตัดให้เหลือเพียงรายการเดียว
Statement =
" SELECT * " &
" From ServiceCharge "
cmd = New OleDbCommand(Statement, Conn)
dr = cmd.ExecuteReader
While dr.Read()
ComboBox1.Items.Add(dr.Item("ServiceCharge").ToString)
End While
dr.Close()
cmd.Dispose()
End Sub
Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
TextBox4.Clear()
TextBox4.Text = ""
Statement =
" SELECT * " &
" From ServiceCharge " &
" WHERE " &
" ServiceCharge = " & "'" & ComboBox1.Text & "'" &
" ORDER BY id "
cmd = New OleDbCommand(Statement, Conn)
dr = cmd.ExecuteReader
While dr.Read()
TextBox4.Text = dr.Item("Price").ToString
End While
dr.Close()
cmd.Dispose()
End Sub