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


0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

01 ต.ค. 62 , 15:47:29
อ่าน 1085 ครั้ง

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 แต่ไม่รู้จะใส่ตรงไหน ใส่ยังไงครับ

 

01 ต.ค. 62 , 18:45:02
ตอบกลับ #1

PNR

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


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

โค๊ด: [Select]
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 ไว้ตลอด ตัวอย่างโค้ด

โค๊ด: [Select]
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

ลองดูครับ ผิดตรงไหนแจ้งได้นะคับ
« แก้ไขครั้งสุดท้าย: 01 ต.ค. 62 , 19:35:26 โดย PNR »
Time to stop for me  :dizzy:
 
โพสต์นี้ได้รับคำขอบคุณจาก: Tongo

01 ต.ค. 62 , 20:01:51
ตอบกลับ #2

สันติสุข

ผมขอเสนอวิธีเขียนโค้ดที่รวม 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

02 ต.ค. 62 , 05:03:20
ตอบกลับ #3

Tongo

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

 

04 ต.ค. 62 , 09:31:39
ตอบกลับ #4

boreris

ขอบคุณมากครับ

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


บอร์ดเรียนรู้ Access สำหรับคนไทย


 

Sitemap 1 2 3 4 5