Search ตามเพศ ช่วยด้วยครับ
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 32,767   11
URL.หัวข้อ / URL
Search ตามเพศ ช่วยด้วยครับ

ผมลองนำการ Search แบบ Dynamic มาประยุกต์ใช้ดู ได้ผลดีมากครับ แต่ผมมีปัญหาบางประการแก้ไม่ตกอยากเรียนถามท่าน อ.สุภาพ และ อ. BADMan หรือท่านผู้รู้ทุกท่านดังนี้ครับ
1. ผมต้องการ Search User ตามเพศครับ
2. ใน Filed Title มีข้อมูลเป็น Mr., Mrs, และ Miss เพื่อแบ่งเพศ
3. ผมต้องการสร้าง ComboBox (ใน Form เดียวกันกับ Dynamic Search) โดยทำเป็น Value
List เป็น Male และ Famale
4. ถ้าเลือก Male แล้วกดปุ่ม Show Data จะแสดงข้อมูล User ทุกคนที่มี Title เป็น Mr. แต่ถ้า
เลือก Female แล้วกดปุ่ม Show Data จะแสดงข้อมูล User ที่มี Title เป็น Mrs. และ Miss
(แสดงข้อมูลใน SubForm)
5. ไม่ทราบว่ามีวิธีการทำอย่างไรครับ
ขอบคุณมาก ๆ ครับ

11 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R02390
หลักการมีอย่างนี้ครับ

สมมติว่า Combo Box นี้ชื่อ cmbSex

ให้ลอง

If Me.cmbSex = "Male" Then
strWhere = " And [Title] = 'Mr.'"
Else
strWhere = " And [Title] <> 'Mr.'"
End If

ลองปรับใช้ดูนะครับ
2 @R02396
ทำได้แล้วครับ
ขอบคุณ อ.สุภาพมากครับ
3 @R02406
ผมมีปัญหาต่อเนื่องนะครับ
คือผมต้องการ Search Marital Status ด้วย โดยเก็บเป็น Value ดังนี้ครับ
Single = 1
Married = 2
Divorced = 3
Widow = 4
Separated = 5
ผมลองทำ ComboBox (cboMaritalStatus) โดยทำเป็น Value List เป็น Single, Married, Divorced, Widow และ Separated คู่กับ ComboBox (cmbSex) ของอาจารย์ แล้วใส่ code ดังนี้

If Me.cboMaritalStatus = "Single" Then
where = " And [Marital Status] = '1'"
ElseIf Me.cboMaritalStatus = "Married" Then
where = " And [Marital Status] = '2'"
ElseIf Me.cboMaritalStatus = "Divorced" Then
where = " And [Marital Status] = '3'"
ElseIf Me.cboMaritalStatus = "Widow" Then
where = " And [Marital Status] = '4'"
Else
where = " And [Marital Status] = '5'"
End If

ปัญหาคือ
1. ถ้าผม Search เฉพาะ Marital Status ตัวเดียว มันไม่มีปัญหาอะไรครับ
2. ถ้าผม Search Marital Status คู่กับ Sex ด้วย มันไม่ได้อย่างที่ต้องการนะ
ครับ เช่น ผมต้องการผู้ชายที่แต่งงานแล้ว ผมจะเลือก Male ใน cmbSex
และเลือก Married ใน cboMaritalStatus ข้อมูลจะออกมาเฉพาะคนที่แต่ง
งานแล้ว โดยไม่สนใจว่าเป็นผู้ชายหรือผู้หญิงนะครับ
3. มันอยู่ที่ตำแหน่งการวาง code ด้วยหรือเปล่าครับ คือ
3.1 ถ้าผมวาง code Search ของ Marital Status ก่อน code
Search ของ Sex เวลา Search มันจะสนใจเฉพาะ Sex โดยไม่
คำนึงถึง Marital Status
3.2 ถ้าผมวาง code Search ของ Sex ก่อน code Search ของ
Marital Status มันก็จะสนใจเฉพาะ Marital Status โดยไม่สนใจ
Sex

ขอความกรุณาด้วยครับ
ขอบคุณมากครับ
4 @R02407
ถ้ามีการเก็บค่า 1 2 3 .. 5 ไว้แล้ว ควรจะเลือกทั้ง 2 ฟีลด์มาไว้ใน cmbMaritalStatus เลยครับ จะได้ไม่ต้องเขียนโค้ดยาวอย่างนี้

โดยกำหนดให้ Bound Column ให้เป็น 1 และกำหนด Column Widths ให้เป็น 0cm;3cm และ Column Count ให้เป็น 2
เวลาวิวดูจะเห็นสถานภาพ แต่เก็บตัวเลขแทนครับ

เมื่อเขียนโค้ดจะได้

If Me.cboMaritalStatus <> "" Then
If where <> "" Then
where = where & " And [Marital Status] = '" & Me.cmbMaritalStatus & "'"
Else
where = " [Marital Status] = '" & Me.cmbMaritalStatus & "'"
End If
End If

ถ้ายังไม่ได้ ให้ลองเอาโค้ดทั้งหมดมาดูด้วยครับ

5 @R02412
ผมไม่ค่อยเข้าใจนะครับ เลยส่ง ไฟล์มาให้ดู ช่วยพิจารณาด้วยครับ
ขอบคุณครับ
6 @R02413
ลองเปลี่ยนให้เป็น

...
Dim strAnd As String
...
If Me.cboGender <> "" Then
If Me.cboGender = "Male" Then
where = " [Title] = 'Mr.'"
Else
where = " [Title] <> 'Mr.'"
End If
End If

If Me.cboMaritalStatus <> "" Then
If where <> "" Then
strAnd = " And "
Else
strAnd = ""
End If
If Me.cboMaritalStatus = "Single" Then
where = where & strAnd & " [Marital Status] = '1'"
ElseIf Me.cboMaritalStatus = "Married" Then
where = where & strAnd & " [Marital Status] = '2'"
ElseIf Me.cboMaritalStatus = "Divorced" Then
where = where & strAnd & " [Marital Status] = '3'"
ElseIf Me.cboMaritalStatus = "Widow" Then
where = where & strAnd & " [Marital Status] = '4'"
Else
where = where & strAnd & " [Marital Status] = '5'"
End If
End If
...
7 @R02414
ลองเปลี่ยน code ดูแล้วเกิด Error ดังนี้ครับ

สมมติถ้าเลือก Male กับ Single
Run-time error '3075':
Syntax error in query expression 'le] = 'Mr.' And [Marital Status] = '1".

ถ้าเลือก Male อย่างเดียว
Run-time error '3075':
Syntax error in query expression 'le] = 'Mr.".

ถ้าเลือก Single อย่างเดียว
Run-time error '3075':
Syntax error (missing operator) in query expression 'ital Status] = '1".

แล้วถ้ากด Debug จะ high light แถบสีเหลือง ที่
Set QD = db.CreateQueryDef("Query1", _
"Select * from qryEducationDetails " & (" where " + Mid(where, 6) & ";"))

ไม่ทราบเกิดจากอะไรครับ
ขอบคุณอีกครั้งครับ
8 @R02417
ดูจากตัวอย่างทั้งหมดเลยก็ได้ครับ
9 @R02448
ขอบคุณ อ.สุภาพมาก ๆ ครับ
10 @R02492
ผมขอถามอีกหน่อยนะครับ
คือผมต้องการนำ coding ด้านล่าง ไปประยุกต์ใช้งานอื่น ๆ ต่อไป เช่น ผมต้องการ Search Field ใด Field หนึ่ง เพียงอย่างเดียว หรือต้องการ Search รวมกันมากกว่า 1 Field ขึ้นไป
แต่ code ด้านล่างสามารถ Search Gender หรือ Marital Status อย่างใดอย่างหนึ่งหรือรวมกันได้ แต่ไม่สามารถ Search เฉพาะ FirstName (Thai) อย่างเดียว หรือ Field อื่น ๆ ได้ เป็นต้น
ผมต้องการที่จะเข้าใจ code ด้านล่างนะครับ เพื่อจะได้นำไปประยุกต์ใช้ได้ เช่น where คืออะไร, ทำไมทุกครั้งที่เกิด Error แล้วกด Debug จะต้องเกิด High Light สีเหลืองที่บรรทัดนี้ทุกครั้งเลย ไม่เข้าใจจริง ๆ ครับ
Set QD = db.QueryDefs("Query1")
QD.SQL = "Select * from qryEducationDetails" & where

Coding ที่กล่าวถึงข้างต้น ดังนี้

Private Sub cmdShowData_Click()
Dim db As Database
Dim QD As QueryDef
Dim where As String
Dim strAnd As String

Set db = CurrentDb

where = ""

If Me.cboGender <> "" Then
If Me.cboGender = "Male" Then
where = " [Title] = 'Mr.'"
Else
where = " [Title] <> 'Mr.'"
End If
End If
If Me.cboMaritalStatus <> "" Then
If where <> "" Then
strAnd = " And "
Else
strAnd = ""
End If
If Me.cboMaritalStatus = "Single" Then
where = where & strAnd & " [Marital Status] = '1'"
ElseIf Me.cboMaritalStatus = "Married" Then
where = where & strAnd & " [Marital Status] = '2'"
ElseIf Me.cboMaritalStatus = "Divorced" Then
where = where & strAnd & " [Marital Status] = '3'"
ElseIf Me.cboMaritalStatus = "Widow" Then
where = where & strAnd & " [Marital Status] = '4'"
Else
where = where & strAnd & " [Marital Status] = '5'"
End If
End If

If Left(Me![FirstName (Thai)], 1) = "*" Or Right(Me![FirstName (Thai)], 1) = "*" Then
where = where & " AND [First Name (Thai)] Like '" + Me![FirstName (Thai)] + "'"
Else
where = where & " AND [First Name (Thai)]='" + Me![FirstName (Thai)] + "'"
End If

If Left(Me![First Name (English)], 1) = "*" Or Right(Me![First Name (English)], 1) = "*" Then
where = where & " AND [First Name (English)] Like '" + Me![First Name (English)] + "'"
Else
where = where & " AND [First Name (English)]='" + Me![First Name (English)] + "'"
End If

If Left(Me![Field of Study], 1) = "*" Or Right(Me![Field of Study], 1) = "*" Then
where = where & " AND [Field of Study] Like'" + Me![Field of Study] + "'"
If Left(Me![Field of Study], 1) = "*" Or Right(Me![Field of Study], 1) = "*" Or Left(Me![Field of Study], 1) = "*" Or Right(Me![Field of Study], 1) = "*" Then
where = where & " AND [Field of Study] Like'" + Me![Field of Study] + "' Or [Field of Study] Like '" + Me![Field of Study2] + "'"
Else
where = where & " AND [Field of Study]='" + Me![Field of Study] + "'Or [Field of Study] Like '" + Me![Field of Study2] + "'"
End If
End If

If where <> "" Then
where = " Where " & where
End If

Set QD = db.QueryDefs("Query1")
QD.SQL = "Select * from qryEducationDetails" & where

'DoCmd.OpenQuery "Query1"
Me.sfrmSearch2.Form.RecordSource = "Query1"

End Sub

ขอบคุณในความเมตตาลูกนกตัวดำ ๆ ตัวนี้นะครับ
11 @R02502
อ่านจากกระทู้อื่น เลยรู้ว่า อ.สุภาพไม่อยู่จนถึงวันศุกร์
ขอความกรุณา อ.BADMan หรือผู้รู้ท่านอื่น ๆ ก็ได้ครับ
ขอบคุณมากครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0490s