การเปลี่ยน RecordSource ของ Form หรือ การใช้ Filter กับ Form
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 514   5
URL.หัวข้อ / URL
การเปลี่ยน RecordSource ของ Form หรือ การใช้ Filter กับ Form

มีคนถามไว้ที่
http://www.quicktechusa.com/msgboard/wwwboard.pl?read=16995

ดังนี้


Hi All...
I have a tabular form showing hundreeds of guest name records ordered alphabetically.
I have a unbound field to restrict the search by letters.
When open I want to show all the records.
When I write in UnBound Field: H , I should get the records started by H**** etc.. and if I write Ha it should filter accordingly.
If I do child - Master, when open tabular form no data diplayed.
How can I do so... hope my point is clear and hope someone can help me..
Thanks a lot


ผมได้ตอบเขาไปดังนี้


เปลี่ยน RecordSource ของ Form หรือ จะใช้ Filter ก็ได้


ตัวอย่างอยู่ที่
http://agserver.kku.ac.th/basiceng/filter2.zip

ลองดูจากตัวอย่างนะครับ จะเข้าใจได้ง่ายขึ้น

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

1 @R00523
มีคนถามคำถามคล้ายๆ กันนี้ไว้ที่ http://board.quicktechusa.com/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=1;t=002029#000001 คำถามมีดังนี้ Hi all. Beginner here at vb access. What I have is a form with a search box that looks up a value in a field in my list box. When I hit enter, it finds the field and Highlights the data in my list box. This works fine. The problem is, when I tab out of the list box on the form where I can edit the record, it does not go to that record. If I double click on the record that my search box found, I am able to edit the record in my other list boxes. Also when I am in my editing area, when I tab out of the last field, the highlighted record does not change accordingly. Any help would be appreciated. Thx Dan This is the code in my search box: Private Sub Text30_AfterUpdate() Dim intListRow As Integer intListRow = 1 Do If List32.ItemData(intListRow) = Text30.Value Then List32.Selected(intListRow) = True Exit Do End If intListRow = intListRow + 1 Loop Until intListRow = List32.ListCount End Sub This is the Code in my Look up record listbox, the gets highlighted. Private Sub List32_AfterUpdate() Dim rs As Object Set rs = Me.Recordset.Clone rs.FindFirst "[CODE] = " & Str(Me![List32]) Me.Bookmark = rs.Bookmark End Sub The fields that I eded have no code in them. Thx again Dan ผมเลยแนะนำให้เขาทำ Filter จะง่ายกว่าครับ
2 @R00524
และที่ http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=32553 มีคำถามคล้ายๆ กันคือ I would like to make a "filter" to generate a table / query / or a form that contains values from multiple space or comma delimited entries. any input / ideas on how to make this "filter" the simpler the better / The values of my filter would be SKU so i would copy a series of cells from excel in looking like this. "sk600 sk555 sk452 vl325" and would like to produce a table with these sku's thank you และมีคนตอบ 1 คน คือ Possible Solution: Have a Text Box for entering the SKU numbers. Create the Where part of the SQL using the following: Dim arSKU As Variant Dim stReturn as String 'Split the SKU list into an Array arSKU = Split(Me.txtSKU, " ") 'Join them back together in the "WHERE" form stReturn = Join(arSKU, "' OR [SKU]='") stReturn = "[SKU]='" & Me.txtReturn & "'" Using the Split function will turn the SKU list into a single dimensional Array and then using the Join Function you put the strings back together using the "' OR [SKU]='" as the new deliminator. An other possible method would be to use the Replace function. This would allow you to replace the diliminating Character with "' OR [SKU]='" and skip the array. __________________ Travis L Abrahamson ผมเลยนำมาประยุกต์แล้วเขียนเป็นฟังก์ชันสำเร็จให้ดู ได้ 3 แบบดังนี้ Function TestFilterArray(strString As String) Dim arSKU As Variant Dim stReturn As String 'Split the SKU list into an Array arSKU = Split(strString, " ") 'Join them back together in the "WHERE" form stReturn = Join(arSKU, "' OR [SKU]='") stReturn = "[SKU]='" & stReturn & "'" TestFilterArray = stReturn End Function Function TestFilterArray2(strString As String) Dim arSKU As Variant Dim stReturn As String 'Replace " " with "', '" arSKU = Replace(strString, " ", "' Or [SKU]= '") ' Add ' ' before and after srSKU stReturn = "[SKU] ='" & arSKU & "'" TestFilterArray2 = stReturn End Function Function TestFilterArray3(strString As String) Dim arSKU As Variant Dim stReturn As String 'Replace " " with "', '" arSKU = Replace(strString, " ", "', '") 'Join them back together in the "WHERE In" form stReturn = "[SKU] In ('" & arSKU & "')" TestFilterArray3 = stReturn End Function เมื่อลองใช้งานดู จะได้ผลลัพธ์ดังนี้ ? testfilterarray3("sk600 sk555 sk452 vl325") [SKU] In ('sk600', 'sk555', 'sk452', 'vl325') ? testfilterarray2("sk600 sk555 sk452 vl325") [SKU] ='sk600' Or [SKU]= 'sk555' Or [SKU]= 'sk452' Or [SKU]= 'vl325' ? testfilterarray3("sk600 sk555 sk452 vl325") [SKU]='sk600' OR [SKU]='sk555' OR [SKU]='sk452' OR [SKU]='vl325' เป็นการใช้ Replace(), Join(), และ Split() เข้าช่วยครับ ให้ดูการเขียนฟังก์ชัน Replace() เพื่อใช้กับ 97 ได้ที่ http://www.thai-access.com/suphap.php?topic_id=250
3 @R00553
มีคนถามปัญหาเกี่ยวกับการเปิดฟอร์มแบบ Tabular ให้แสดงข้อมูลตาม ID ที่เลือกไว้ใน List Box แบบ Multiselect ของอีกฟอร์มหนึ่ง ที่ http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=32710 ผมได้ตอบเขาไว้อย่างไร ลองเข้าไปดูนะครับ
4 @R00570
และที่ http://www.utteraccess.com/forums/showflat.php?Cat=&Board=access_2000&Number=108179&page=0&view=&sb=&o=&fpart=1&vc=1&PHPSESSID= ก็ถามไว้ว่า gday... I am trying to apply a filter, which appears to be hanging the system. A few times running the filter will cause access to have an error and close. i have a form with peoples details, and i want to be able to add a simple filter or search to search the records by either last name or an id number... what would be the best way to do this.. I have created a macro, but as i said, it seems to hang/crash. Is there code for a module that will do this? ผมได้ตอบเขาไปไว้ดังนี้ครับ ให้สร้าง unbound text box ขึ้นมา 1 อัน เพื่อรับข้อความที่ต้องการจะกรอง จากโค้ดข้างล่างผมตั้งชื่อเป็น txtFilter จากนั้นให้ใช้การคลิก 2ทีที่ฟีลด์ LastName เพื่อกรองข้อมูลในฟอร์มตามเงื่อนไขที่ได้พิมพ์ลงไป โดยใช้โค้ดข้างล่างนี้ Private Sub LastName_DblClick(Cancel As Integer) Dim strSearch As String strSearch = Me.txtFilter If strSearch <> "" Or Not IsNull(strSearch) Then DoCmd.ApplyFilter , "LastName Like '*" & strSearch & "*'" End If End Sub และต้องมีปุ่ม command button อีกอันหนึ่ง เพื่อลบ filter ออก สมมติว่าชื่อ cmdReset ก็เพียงแต่ใส่โค้ดนี้ลงไปในในเหตุการณ์ On_Click ครับ Private Sub cmdReset_Click() DoCmd.ShowAllRecords End Sub และถ้าต้องการที่จะใช้วิธีเดียวกันนี้ไปกรอง ID ด้วย ก็ใช้ได้ ถ้าฟีลด์ชื่อ ID และเป็นแบบตัวเลข ก็ใช้โค้ดดังนี้ Private Sub ID_DblClick(Cancel As Integer) Dim strSearch As String strSearch = Me.ID If strSearch <> "" Or Not IsNull(strSearch) Then DoCmd.ApplyFilter , "ID Like *" & strSearch & "*" End If End Sub โค้ดที่ใช้กรองทั้ง 2 รายการจะเป็นแบบ wild card คือ ถ้าพิมพ์เพียงส่วนใดส่วนหนึ่งในช่อง txtFilter ถ้าในฟีลด์นั้นมีคำเหล่านั้น มันก็จะแสดงออกมาให้เห็นครับ
5 @R00593
มีคนถามไว้ที่ http://www.utteraccess.com/forums/showflat.php?Cat=&Board=AxxessXP&Number=109641&page=0&view=collapsed&sb=5&o=7&fpart=1 ถ้าจะทำ Filter by selection VBA หรือการกรองข้อมูลข้อมูลตามข้อความหรือตัวเลขที่อยู่ในฟีลด์ใดๆ จะทำอย่างไร ผมแนะนำเขาไป โดยการใช้โค้ดดังนี้ครับ ก็ใช้ DoCmd.ApplyFilter สมมติว่า ต้องการคลิก 2 ทีที่ Product แล้วให้ข้อมูลแสดงขึ้นมาเฉพาะที่มีเลข Product ตรงกับที่แสดงในช่องปัจจุบัน code:-------------------------------------------------------------------------------- Private Sub Product_DblClick(Cancel As Integer) DoCmd.ApplyFilter , "[Product] = " & Me.Product End Sub
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0434s