กรองข้อมูลด้วย Combo Box และ Check Box และการค้นหาข้อมูลที่กรองยังไงครับ

เริ่มโดย Tongo, 01 ต.ค 19 , 15:47:29

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

Tongo

ผมได้ศึกษาจากคลิป https://www.youtube.com/watch?v=0xMPyi4fQFI&t=790s และต้องการจะประยุกต์โดยใช้ 1 combo box กับ 1 check box จะต้องปรับโค้ดอย่างไรครับ

โค้ดที่ผมทำไว้ตอนนี้เป็นแบบนี้ครับ
อ้างถึงOption Compare Database

Private Sub Searchcombo()
Dim sql As String
sql = "select * FROM Qsaleh_cusprof WHERE [cus_name] ='" & Me.combonamesearch & "'"
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = sql
Forms!frmhistoryh!frmhistorylist.Form.Requery

End Sub
Private Sub checkbox()
Dim sql As String
sql = "select * from Qsaleh_cusprof where [acculate] = " & Me.acculatesearch & ""
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = sql
Forms!frmhistoryh!frmhistorylist.Form.Requery
   
End Sub

Private Sub acculatesearch_AfterUpdate()
checkbox
End Sub

Private Sub combonamesearch_AfterUpdate()
Searchcombo
End Sub


ซึ่งมันทำงานแยกกัน ผมต้องการให้มันทำงานด้วยกัน เข้าใจว่าต้องมี AND แต่ไม่รู้จะใส่ตรงไหน ใส่ยังไงครับ
  •  

PNR

ลองดูตัวอย่างนี้ครับ ทำงานแยกกันได้ ทำงานด้วยกันได้ ไม่รู้แบบที่ต้องการหรือเปล่าอยากได้ส่วนไหนเพิ่มก็บอกได้ครับ


แบบที่ 1 
checkbox ถ้าติ๊กถูก คือการเลือกเฉพาะที่ถูก และถ้าติ๊กออก คือแสดงข้อมูลทั้งหมด

Sub Search()
Dim StrWhere1, StrWhere2, GroupFilter As String

If Not IsNull(Me.combonamesearch) Then
StrWhere1 = "(((Qsaleh_cusprof.cus_name)=[forms]![frmhistoryh]![combonamesearch])"
End If
If Me.acculatesearch = True Then
StrWhere2 = "(((Qsaleh_cusprof.acculate)=[forms]![frmhistoryh]![acculatesearch])"
End If

GroupFilter = IIf(StrWhere1 = "", "", StrWhere1) & IIf(StrWhere2 = "", "", StrWhere2)
GroupFilter = Replace(GroupFilter, ")(((", ") and ((")

If IsNull(Me.combonamesearch) And Me.acculatesearch = False Then
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "Select * from Qsaleh_cusprof;"

ElseIf IsNull(Me.combonamesearch) And Me.acculatesearch = True Then
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "Select * from Qsaleh_cusprof where " & GroupFilter & ");"

ElseIf Not IsNull(Me.combonamesearch) And Me.acculatesearch = True Then
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "Select * from Qsaleh_cusprof where " & GroupFilter & ");"

ElseIf Not IsNull(Me.combonamesearch) And Me.acculatesearch = False Then
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "Select * from Qsaleh_cusprof where " & GroupFilter & ");"
End If

If Forms!frmhistoryh!frmhistorylist.Form.Recordset.RecordCount = 0 Then
MsgBox "ไม่พบข้อมูลที่ค้นหา", vbInformation, "สถานะการค้นหา"
End If

End Sub

Private Sub acculatesearch_AfterUpdate()
Call Search
End Sub

Private Sub combonamesearch_AfterUpdate()
Call Search
End Sub



แบบที่ 2
คือเงื่อนไขจะมาจากทั้ง 2 Control เช่นถ้าไม่ติ๊กถูก ข้อมูลที่ติ๊กถูกจะหายไป แบบนี้คือ Fix เงื่อนไขที่ตัว checkbox และ combobox ไว้ตลอด ตัวอย่างโค้ด


Sub Search()
Dim StrWhere1, StrWhere2, GroupFilter As String

If Not IsNull(Me.combonamesearch) Then
StrWhere1 = "(((Qsaleh_cusprof.cus_name)=[forms]![frmhistoryh]![combonamesearch])"
End If
StrWhere2 = "(((Qsaleh_cusprof.acculate)=[forms]![frmhistoryh]![acculatesearch])"

GroupFilter = IIf(StrWhere1 = "", "", StrWhere1) & IIf(StrWhere2 = "", "", StrWhere2)
GroupFilter = Replace(GroupFilter, ")(((", ") and ((")

If IsNull(Me.combonamesearch) And Me.acculatesearch = False Then
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "Select * from Qsaleh_cusprof;"
Else
Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "Select * from Qsaleh_cusprof where " & GroupFilter & ");"
End If
If Forms!frmhistoryh!frmhistorylist.Form.Recordset.RecordCount = 0 Then
MsgBox "ไม่พบข้อมูล", vbInformation, "การค้นหา"
End If
End Sub
Private Sub acculatesearch_AfterUpdate()
Call Search
End Sub

Private Sub combonamesearch_AfterUpdate()
Call Search
End Sub


ลองดูครับ ผิดตรงไหนแจ้งได้นะคับ
Time to stop for me  :dizzy:
  •  
    โพสต์นี้ได้รับคำขอบคุณจาก: Tongo

สันติสุข

ผมขอเสนอวิธีเขียนโค้ดที่รวม 2 เงื่อนไขด้วย AND แบบสั้นๆ

Dim Cond1 As String, Cond2 As String

If Nz(Me.combonamesearch,"") = "" Then Cond1 = " TRUE " Else Cond1 = " ([cus_name] = '" & Me.combonamesearch & "') "

If Nz([acculate],"") = "" Then Cond2 = " TRUE " Else Cond2 = " ([acculate] = " & Me.acculatesearch & ") "

Forms!frmhistoryh!frmhistorylist.Form.RecordSource = "select * from Qsaleh_cusprof where " & Cond1 & " AND " & Cond2
ช่วยพกถุงผ้า/ถุงพลาสติกใช้แล้วไปซื้อของเพื่อลดการใช้พลาสติก ขยะ รักษาสิ่งแวดล้อม และไม่ให้ภาวะโลกร้อนวิกฤติเร็วขึ้นกว่านี้
ช่วยคลิกโฆษณาข้างล่างนี้ เพื่อสนับสนุนเวปบอร์ดด้วยครับ
:nice day:
  •  
    โพสต์นี้ได้รับคำขอบคุณจาก: PNR, Tongo

Tongo

ขอบคุณทั้งสองท่านมากๆครับที่ทำให้โมดูลค้นหานี้สำเร็จลงด้วยดี ผมเก็บโค้ดของทั้งสองท่านไว้ทั้งหมดเพราะเห็นแววแล้วว่าต้องใช้ในอนาคตแน่ๆ ขอบคุณมากครับ ^_^
  •  

boreris

  •  
    โพสต์นี้ได้รับคำขอบคุณจาก: SakDa