 |
|
อยากให้โค้ดเงือนไข Combobox 2 อันดึงจากฐานข้อมูล MS Access |
|
 |
|
|
 |
 |
|
ถ้าเป็น Windows App ง่ายมากครับ ดับเบิ้ลคลิกที่ Combobox ตัวแรก แล้วสร้าง Event ได้เลยครับ 
|
 |
 |
 |
 |
Date :
2015-02-09 09:42:54 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แต่อยากได้แบบVB.Net ครับต้องทำโปรเจคจบช่วยหน่อยครับจนปัญญาจริงๆครับ
|
 |
 |
 |
 |
Date :
2015-02-09 11:09:37 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แค่ใช้ where เองนิครับ
แล้วอีกอย่างค้นหาใน google ก็มีออกเกลื่อนไป
1. SQL where
2. combobox binding vb.net
หลักการบ้านๆคือ ให้มัน refresh ผ่าน SQL where ทุกครั้งที่เลือกข้อมูลจาก combobox แค่นั้นเองครับ
ยิ่งถ้าใช้ datasource ยิ่งง่ายใหญ่เลยครับ
แค่ลากมาวาง binding มันก้มาแระ
|
 |
 |
 |
 |
Date :
2015-02-09 12:09:46 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากได้โค้ดดตัวอย่างอ่าครับ พอจะเขียนให้ได้ไหมครับ
ผมใช้Refreshไม่เป็นด้วยอ่าครับ
|
ประวัติการแก้ไข 2015-02-09 15:09:30
 |
 |
 |
 |
Date :
2015-02-09 12:20:15 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่เข้าใจจริง ๆ ครับ เขียนโค้ดตัวอย่างให้ดูเถอะครับ
|
 |
 |
 |
 |
Date :
2015-02-09 16:44:13 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ให้ไปไม่ใช่โค๊ดเรอะครับ
เอางี้ครับเอาโค๊ดล่าสุดมาดูครับ
เด๋วจะแนะวิธีแบบบ้านๆให้
|
 |
 |
 |
 |
Date :
2015-02-09 17:56:24 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
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.
|
ประวัติการแก้ไข 2015-02-09 19:34:03
 |
 |
 |
 |
Date :
2015-02-09 19:33:23 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 9 เขียนโดย : souyofnewerth เมื่อวันที่ 2015-02-09 19:33:23
รายละเอียดของการตอบ ::
การเขียนโปรแกรมที่ดีควรจะมีขั้นตอนเช็ค Err เป็นระยะๆครับ
ลอง messebox CommandText
With com
.CommandText = sqlIni
MessageBox.Show( .CommandText )
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
End With
ลอง messebox CommandText มาเด๋วก็อ๋อเองครับ
ดูท่าทางใช้ SQL เป็นพอสมควร
งั้นแนะอีกนิดครับ
ถ้าจะให้ได้ตามที่ต้องการยังขาด select .... where ไป
การ binding จะมี 2 รอบ คือ
1. รอบแรก เพื่อให้เห็นข้อมูลทั้งหมด ตัวนี้ไม่ต้องมี where
2. รอบที่มีการเลือก dtCategories ก็จะมี select .... where เป็นเงื่อนไข
|
 |
 |
 |
 |
Date :
2015-02-10 09:06:39 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับผมจะลองดูช่วงเย็นๆ ตอนนี้ไปเรียนครับ
|
 |
 |
 |
 |
Date :
2015-02-10 09:09:23 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมยังติดอีกอีกหลายจุด อีก2วันต้องส่งแล้วด้วย
|
 |
 |
 |
 |
Date :
2015-02-10 09:15:12 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
https://drive.google.com/file/d/0B-JZ2hYujzUAS2RHT3dPcVhTd0U/view?usp=sharing
Code (VB.NET)
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
บ้านๆ สั้นๆ ไม่ซับซ้อน
|
 |
 |
 |
 |
Date :
2015-02-10 10:16:13 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2015-02-10 10:27:52 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
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
adapter.Fill(ds) เออเร่อครับ Syntax error (missing operator) in query expression 'n_drug='.
นี่ไฟล์ฐานข้อมูลของผม https://www.dropbox.com/s/zddc73no00z6szi/Hospital.accdb?dl=0
|
ประวัติการแก้ไข 2015-02-10 11:30:39
 |
 |
 |
 |
Date :
2015-02-10 11:21:30 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
"SELECT * FROM inventory where n_drug= "
น่าจะเป็น id_drug รึป่าวครับ
|
 |
 |
 |
 |
Date :
2015-02-10 11:30:16 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เปลี่ยนแล้วก็Errorเหมือนเดิมครับ
|
 |
 |
 |
 |
Date :
2015-02-10 15:24:15 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
MessageBox.Show( chbndrug.SelectedValue )
ดูดีๆ
แล้วจะพบทางสว่างครับ
ถ้าไม่พบเด๋วผมจะบอกอีกที
|
 |
 |
 |
 |
Date :
2015-02-10 15:47:29 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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
ได้วิธีใหม่ละครับ แต่พอเลือกอันแรกอันหลังก็ขึ้นว่างแต่มันมีข้อมูล


|
 |
 |
 |
 |
Date :
2015-02-10 16:28:48 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากได้โค้ดที่เลือกห้องรักษาแล้วให้โชว์เฉพาะห้องรักษานั้น ๆ มีไหมครับ

|
 |
 |
 |
 |
Date :
2015-02-10 16:32:51 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 20 เขียนโดย : souyofnewerth เมื่อวันที่ 2015-02-10 16:28:48
รายละเอียดของการตอบ ::
ต้องดูใน database ว่ามีค่างว่างรึป่าว
ตอบความคิดเห็นที่ : 21 เขียนโดย : souyofnewerth เมื่อวันที่ 2015-02-10 16:32:51
รายละเอียดของการตอบ ::
ใช้หลักการเดียวกัน
ส่วน datagrid ก็แค่
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
ไม่จำเป็นต้องเขียนยาวขนาดนั้น
แค่แก้ตรง n_typedrug (อย่างที่บอกครับ คุณจะต้องพบทางสว่าง )
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2015-02-10 16:54:38 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ในDataBaseไม่มีค่าว่าง
Code (VB.NET)
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'.
|
ประวัติการแก้ไข 2015-02-10 17:06:26
 |
 |
 |
 |
Date :
2015-02-10 17:06:02 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (VB.NET)
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
หมดปัญญาแก้แล้วจริง ๆ ครับ
|
 |
 |
 |
 |
Date :
2015-02-10 22:51:29 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันแค่เขียนผิดครับ
ลอง messebox strQuery ออกมาดูครับแล้วจะเห็นทางกระจ่าง
ปล. chbndrug.SelectedValue มันเป็น string อยู่แล้ว
วิธีจัดระเบียบการการเขียนโปรแกรมคือ
1. ดักจับ Err บ่อยๆ เช่น messebox ค่าที่คิดว่าผิด
2. ดูความซ้ำซ้อนของ syntex ว่าควรตัดตัวไหนได้บ้าง เพราะถ้าเกิดต่อไปเขียนโปรแกรมที่ซับซ้อนมากกว่านี้
ความกระชับของโค๊ด มีผลต่อการตัดสินใจเป็นอย่างมากครับ
   
|
 |
 |
 |
 |
Date :
2015-02-11 08:37:32 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้เป็นชั่วโมงก็ไม่ได้ซักที

|
 |
 |
 |
 |
Date :
2015-02-11 19:13:39 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่.ToString ก็ขึ้นแบบนี้

ใส่ MessageBox.Show(chbndrug.SelectedValue) รันได้พอเลือกแล้วขึ้น MsgBox ว่าง ๆ
|
 |
 |
 |
 |
Date :
2015-02-11 19:17:13 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ถูกต้องเป็น
chbndrug.DataSource = GetTableOLEDB("SELECT * FROM inventory where n_typedrug=" & chbndrug.SelectedValue, cnn)
|
 |
 |
 |
 |
Date :
2015-02-12 08:27:42 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่แบบนั้นก็เอออร่อครับ
|
 |
 |
 |
 |
Date :
2015-02-12 08:30:12 |
By :
souyofnewerth |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|