Private Sub Showcombotypedurg()
Connectiondata()
sb = New StringBuilder
sb.Append("SELECT id_typedrug,n_typedrug FROM typedrug")
sb.Append("SELECT id_drug,n_drug FROM inventory")
Dim sqlIni As String = sb.ToString()
com = New OleDbCommand()
With com
.CommandText = sqlIni
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
End With
If dr.HasRows Then
Dim dtCategories As DataTable
dtCategories = New DataTable()
dtCategories.Load(dr)
With chbntypedrug
.BeginUpdate()
.DisplayMember = "n_typedrug"
.ValueMember = "id_typedrug"
.DataSource = dtCategories
.EndUpdate()
End With
Dim dtShippers As DataTable
dtShippers = New DataTable()
dtShippers.Load(dr)
With chbndrug
.BeginUpdate()
.DisplayMember = "n_drug"
.ValueMember = "id_drug"
.DataSource = dtShippers
.EndUpdate()
End With
End If
dr.Close()
dr = .ExecuteReader() เออเร่อบรรทัดที่13ขึ้นว่า Syntax error in FROM clause.
ลอง messebox CommandText
With com
.CommandText = sqlIni
MessageBox.Show( .CommandText )
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
End With
Public Class Form3
Dim cnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PostCode.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091"
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_Province", cnn)
ComboBox2.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_PostCode", cnn)
End Sub
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
Dim conn As New Data.OleDb.OleDbConnection(strConn)
Dim ds As New System.Data.DataSet()
conn.Open()
Dim strQuery As String = SQL
Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_PostCode where Pnv = " & ComboBox1.SelectedValue, cnn)
End Sub
End Class
Private Sub CheckAnimal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
chbntypedrug.DataSource = GetTableOLEDB("SELECT * FROM typedrug", cnn)
chbndrug.DataSource = GetTableOLEDB("SELECT * FROM inventory", cnn)
end sub
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
Dim conn As New Data.OleDb.OleDbConnection(strConn)
Dim ds As New System.Data.DataSet()
conn.Open()
Dim strQuery As String = SQL
Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
Private Sub chbntypedrug_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chbntypedrug.SelectedIndexChanged
chbndrug.DataSource = GetTableOLEDB("SELECT * FROM inventory where n_drug= " & chbndrug.SelectedValue, cnn)
End Sub
Private Sub showcbdrug()
da = New OleDbDataAdapter("SELECT DISTINCT n_typedrug From inventory Order By n_typedrug", Conn)
da.Fill(ds)
Conn.Close()
chbntypedrug.DataSource = ds.Tables(0)
chbntypedrug.DisplayMember = "n_typedrug"
chbntypedrug.ValueMember = "n_typedrug"
chbntypedrug.SelectedIndex = -1
End Sub
Private Sub chbntypedrug_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chbntypedrug.SelectedIndexChanged
If chbntypedrug.SelectedIndex > -1 Then
da = New OleDbDataAdapter("SELECT DISTINCT n_drug From inventory WHERE n_typedrug='" + chbntypedrug.SelectedValue.ToString() + "'", Conn)
da.Fill(ds)
Conn.Close()
chbndrug.DataSource = ds.Tables(0)
chbndrug.DisplayMember = "n_drug"
chbndrug.ValueMember = "n_drug"
End If
End Sub
DataGridView1.DataSource = GetTableOLEDB("Select * form .... where roomID = " & cmb.SelectedValue)
..
แล้วก็จัดหน้า DataGridView
เป็นอันเส็จพิธี
ความจริง Code (VB.NET)
Private Sub showcbdrug()
da = New OleDbDataAdapter("SELECT DISTINCT n_typedrug From inventory Order By n_typedrug", Conn)
da.Fill(ds)
Conn.Close()
chbntypedrug.DataSource = ds.Tables(0)
chbntypedrug.DisplayMember = "n_typedrug"
chbntypedrug.ValueMember = "n_typedrug"
chbntypedrug.SelectedIndex = -1
End Sub
Private Sub chbntypedrug_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chbntypedrug.SelectedIndexChanged
If chbntypedrug.SelectedIndex > -1 Then
da = New OleDbDataAdapter("SELECT DISTINCT n_drug From inventory WHERE n_typedrug='" + chbntypedrug.SelectedValue.ToString() + "'", Conn)
da.Fill(ds)
Conn.Close()
chbndrug.DataSource = ds.Tables(0)
chbndrug.DisplayMember = "n_drug"
chbndrug.ValueMember = "n_drug"
End If
End Sub
Private Sub CheckAnimal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
chbntypedrug.DataSource = GetTableOLEDB("SELECT * FROM typedrug", cnn)
chbndrug.DataSource = GetTableOLEDB("SELECT * FROM inventory", cnn)
end sub
Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
Dim conn As New Data.OleDb.OleDbConnection(strConn)
Dim ds As New System.Data.DataSet()
conn.Open()
Dim strQuery As String = SQL
Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
Private Sub chbntypedrug_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chbntypedrug.SelectedIndexChanged
chbndrug.DataSource = GetTableOLEDB("SELECT * FROM inventory where n_typedrug= " & chbndrug.SelectedValue, cnn)
End Sub
Private Sub chbntypedrug_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chbntypedrug.SelectedIndexChanged
chbndrug.DataSource = GetTableOLEDB("SELECT * FROM inventory where n_typedrug='" & CStr(chbndrug.SelectedValue) & ")'", cnn)
End Sub
แก้แล้วขึ้นแบบนี้ครับ
พอคลิกเลือกไปเลือกมาก็เขึ้น เออเร่อ ตรง บันทัดที่ 2 Operator '&' is not defined for string "SELECT * FROM inventory where n_" and type 'DataRowView'.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim i As Integer
Dim sqlSale As String = ""
Dim comTmp As OleDbCommand = New OleDbCommand
'Dim drTmp As OleDbDataReader
Dim comsale As OleDbCommand = New OleDbCommand
For i = 0 To ListView1.Items.Count - 1
da = New OleDbDataAdapter("INSERT INTO drug(id_order,n_emp,id_drug,n_drug,price_drug,num_drug) VALUES ('" & txtidorder.Text & "'," & chbnemp.Text & "," & CStr(ListView1.Items(i).SubItems(0).Text) & "," & CStr(ListView1.Items(i).SubItems(1).Text) & "," & CInt(ListView1.Items(i).SubItems(3).Text) & "," & CInt(ListView1.Items(i).SubItems(2).Text) & ")", Conn)
Dim tmpqtybook As Integer
Dim codedrug As String
codedrug = ListView1.Items(i).SubItems(0).Text
sqlSale = "SELECT id_drug,qty FROM inventory Where id_drug ='" & codedrug & "'"
da = New OleDbDataAdapter(sqlSale, Conn)
da.Fill(ds, "Pay")
tmpqtybook = CInt(ds.Tables("Pay").Rows(0).Item("qty"))
ds.Tables("Pay").Clear()
sqlSale = "UPDATE inventory"
sqlSale &= " Set qty = " & tmpqtybook - CInt(ListView1.Items(i).SubItems(2).Text)
sqlSale &= " where id_drug = '" & codedrug & "'"
With comsale
.CommandText = sqlSale
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกการส่งจ่ายเรียบร้อยแล้ว", "การสั่งจ่ายเวชภัณฑ์และวัสดุ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub