Sample: Search multiple fields with one textbox???
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 327   1
URL.หัวข้อ / URL
Sample: Search multiple fields with one textbox???

มีคนถามไว้ที่ 
http://www.utteraccess.com/forums/showflat.php?Cat=&Board=access_design&Number=108997&page=0&view=collapsed&sb=5&o=7&fpart=1 
 
ดังนี้ 
Hello All, 
 
I have a form in which I would like to search multiple fields. 
Is it possible to do this with one textbox? 
 
As an example: 
 
This is a database for a computer shop. We would like  
to search for the for the following criteria: 
Repair ticket # 
Customer name 
Date item was brought to the shop 
 
Could this be done with maybe a Combobox which  
would allow the user to select the field to search on, 
and then a textbox for entry of the actual search  
string? It 'seems' it MIGHT be able to work like that,  
or perhaps a similar method. I just have no idea how 
to implement it. I can get the Combobox with no trouble. 
How would I tie it to the textbox for the entered data  
though? 
 
Thanks a bunch!  
 
ผมเข้าไปแสดงความคิดเห็นโดยการเปลี่ยน Recordsource ดังนี้ 
... 
Dim strFieldName As String, strSQL As String 
Dim strPrefix As String, strSuffix As String 
 
strFieldName = Me.combo0 
Select Case strFieldName 
Case Is = "TicketNO" 
strPrefix = " =" 
strSuffix = "" 
Case Is = "CustName" 
strPrefix = " Like *" 
strSuffix = "*" 
Case Is = "DateIn" 
strPrefix = " =#" 
strSuffix = "#" 
End Select 
 
strSQL = "SELECT * FROM YourTableOrQuery Where " & strFieldName & strPrefix & Me.txtSearchText & strSuffix 
 
Me.RecordSource = strSQL 
 
รู้สึกว่าเจ้าของกระทู้จะไม่ปิ้งแนวทางผมเท่าไร แต่มีอีกท่านหนึ่งได้เข้ามาแนะนำ โดยใช้ ApplyFilter แทน ลองไปเอาตัวอย่างที่เจ้าของกระทู้ฝากไว้ให้ดู และอีกท่านหนึ่งได้แก้ไขมาดูที่ http://www.utteraccess.com/forums/uploads/109654-109602-ComputerShop97.mdb 
 
แต่ผมว่าต้องปรับโค้ดให้เป็นดังนี้ ถึงจะใช้ได้สมบูรณ์ครับ 
 
Private Sub SearchButton_Click() 
Dim strPrefix As String, strSuffix As String 
DoCmd.OpenForm "qryCustomerData" 
If Me.SearchField = "ServiceDate" Then 
    strPrefix = "#" 
    strSuffix = "#" 
Else 
    strPrefix = "'" 
    strSuffix = "'" 
End If 
DoCmd.ApplyFilter , "[" & Me.SearchField & "] = " & strPrefix & Me.SearchText & strSuffix 
End Sub 
 
โดยลองเลือก LastName ใน Combo Box แล้วพิมพ์คำว่า Younds ลงไปในช่อง Select Search Text: 
แล้วลองเลือก ServiceDate แล้วใส่ 15/8/2002 ไป จะเกิดปัญหาถ้าใช้โค้ดตัวเดิม 
ต้องปรับตามที่ผมแนะนำจะแก้ปัญหานี้ได้ครับ

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

1 @R00597
หรือจะดูจากของ Microsoft โดยตรง ในหัวข้อ How to Use a Bound Control to Find a Record (95/97) ที่ http://support.microsoft.com/default.aspx?scid=kb;en-us;q136123 เป็นตัวอย่างการเขียนฟังก์ชันแบบ GENERIC ด้วยครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0878s