473,549 Members | 4,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allen Browne form filter top n records?

374 Contributor
Hi all,

I am using Allen's excellent form filter script to filter the results from a query.

I would now like to add some further functionality. How do I go about displaying say the top N costs where in my search form I would have a drop down to select the N value and use radio buttons to select the field the top N should be run against (e.g. cost).

Thanks for any help in advance my code is as follows:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Option Explicit
  4.  
  5. Private Sub btnSearchit_Click()
  6.     'Purpose:   Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
  7.     'Notes:     1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
  8.                     we remove the trailing " AND " at the end.
  9.     '           2. The date range works like this: _
  10.                         Both dates      = only dates between (both inclusive. _
  11.                         Start date only = all dates from this one onwards; _
  12.                         End date only   = all dates up to (and including this one).
  13.  Dim strWhere2 As String                  'The criteria string.
  14.     Dim lngLen2 As Long                      'Length of the criteria string to append to.
  15.     Const conJetDate = "\#mm\/dd\/yyyy\#"   'The format expected for dates in a JET query string.
  16.  
  17. '***********************************************************************
  18. 'Begin the filter codes to create the query
  19. '***********************************************************************
  20.  
  21.  
  22. '------------------------------------------------
  23. 'Begin coding for date search of date occured
  24. '------------------------------------------------
  25.  
  26. 'Date field example. Use the format string to add the # delimiters and get the right international format.
  27.     If Not IsNull(Me.txtfilterDateStart) Then
  28.         strWhere2 = strWhere2 & "([DteOccur] >= " & Format(Me.txtfilterDateStart, conJetDate) & ") AND "
  29.     End If
  30.  
  31.     'Another date field example. Use "less than the next day" since this field has times as well as dates.
  32.     If Not IsNull(Me.txtfilterDateEnd) Then   'Less than the next day.
  33.         strWhere2 = strWhere2 & "([DteOccur] < " & Format(Me.txtfilterDateEnd + 1, conJetDate) & ") AND "
  34.     End If
  35.  
  36.  
  37.  
  38. '------------------------------------------------
  39. ' End coding for date searching of date occured
  40. '------------------------------------------------
  41.  
  42. '------------------------------------------------
  43. ' Start coding for list box to see department raised
  44. '------------------------------------------------
  45.  
  46. 'Listbox example.
  47.     If Me.lstfilterDeptRaised.ItemsSelected.Count > 0 Then
  48.         Dim itm As Variant
  49.         Dim strList As String
  50.         For Each itm In Me.lstfilterDeptRaised.ItemsSelected
  51.             If strList = "" Then
  52.                 strList = "([DeptRaisedBy] = " & Me.lstfilterDeptRaised.Column(0, itm) & ")"
  53.             Else
  54.                 strList = strList & " OR " & "([DeptRaisedBy] = " & Me.lstfilterDeptRaised.Column(0, itm) & ")"
  55.             End If
  56.         Next
  57.         strWhere2 = strWhere2 & "(" & strList & ") AND "
  58.     End If
  59.  
  60. '------------------------------------------------
  61. ' End coding for list box to see department raised
  62. '------------------------------------------------
  63.  
  64.  
  65.  
  66. '------------------------------------------------
  67. ' Start coding for list box to see department responsible
  68. '------------------------------------------------
  69.  
  70. 'Listbox example.
  71.     If Me.lstfilterDeptResp.ItemsSelected.Count > 0 Then
  72.         Dim itm2 As Variant
  73.         Dim strList2 As String
  74.         For Each itm2 In Me.lstfilterDeptResp.ItemsSelected
  75.             If strList2 = "" Then
  76.                 strList2 = "([DeptResp] = " & Me.lstfilterDeptResp.Column(0, itm2) & ")"
  77.             Else
  78.                 strList2 = strList2 & " OR " & "([DeptResp] = " & Me.lstfilterDeptResp.Column(0, itm2) & ")"
  79.             End If
  80.         Next
  81.         strWhere2 = strWhere2 & "(" & strList2 & ") AND "
  82.     End If
  83.  
  84. '------------------------------------------------
  85. ' End coding for list box to see department responsible
  86. '------------------------------------------------
  87.  
  88.  
  89.      '***********************************************************************
  90.     'Chop off the trailing " AND ", and use the string as the form's Filter.
  91.     '***********************************************************************
  92.     'See if the string has more than 5 characters (a trailng " AND ") to remove.
  93.     lngLen2 = Len(strWhere2) - 5
  94.     If lngLen2 <= 0 Then     'Nah: there was nothing in the string.
  95.         MsgBox "No criteria", vbInformation, "Nothing to do."
  96.     Else                    'Yep: there is something there, so remove the " AND " at the end.
  97.         strWhere2 = Left$(strWhere2, lngLen2)
  98.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  99.         'Debug.Print strWhere2
  100.  
  101.         'Finally, apply the string as the form's Filter.
  102.         Forms!frmlogforfiltering.Form.Filter = strWhere2
  103.         Forms!frmlogforfiltering.Form.FilterOn = True
  104.     End If
  105. End Sub
  106.  
  107. Private Sub btnResetit_Click()
  108.     'Purpose: Clear all the search boxes in the Form Header, and show all records again.
  109.     Dim ctl As Control
  110.  
  111.     'Clear all the controls in the Form Header section.
  112.     For Each ctl In Me.Section(acHeader).Controls
  113.         Select Case ctl.ControlType
  114.         Case acTextBox, acComboBox
  115.             ctl.Value = Null
  116.         Case acCheckBox
  117.             ctl.Value = False
  118.         End Select
  119.     Next
  120.  
  121.     'Remove the form's filter.
  122.     Me.Filter = "(False)"
  123.     Me.FilterOn = True
  124. End Sub
  125.  
  126. Private Sub Form_BeforeInsert(Cancel As Integer)
  127.     'To avoid problems if the filter returns no records, we did not set its AllowAdditions to No.
  128.     'We prevent new records by cancelling the form's BeforeInsert event instead.
  129.     'The problems are explained at http://allenbrowne.com/bug-06.html
  130.     Cancel = True
  131.     MsgBox "You cannot add new clients to the search form.", vbInformation, "Permission denied."
  132. End Sub
  133.  
  134. Private Sub Form_Open(Cancel As Integer)
  135.     'Remove the single quote from these lines if you want to initially show no records.
  136.     Me.Filter = "(False)"
  137.     Me.FilterOn = True
  138. End Sub
  139.  
  140.  
Jul 5 '10 #1
7 5271
FishVal
2,653 Recognized Expert Specialist
You should assign SQL statement returning desired recordset to Form.RecordSour ce property, instead of assigning filter expression to Form.Filter property. To make the SQL expression to return a desired quantity of top records use "TOP n" predicate.

Regards,
Fish
Jul 5 '10 #2
munkee
374 Contributor
Updated: managed to put in the recorsource change.. if you viewed this before the edit I must have been having a retarded mind block.


Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = "Select * FROM logforfiltering WHERE" & strWhere2
I can just apply the TOP N clause in there then now I assume.

Thanks for the reply Fish
Jul 5 '10 #3
NeoPa
32,564 Recognized Expert Moderator MVP
This is not about a filter (which is essentially fully defined within the WHERE clause). This is about the SELECT clause of a SQL query.

If you are still talking about a subform (I had to reverse engineer to work this out. Please consider providing the information in future questions) then you would need the .RecordSource of the subform to be in SQL format. It's possible to use a QueryDef as the .RecordSource of a form, but if so, you would need to get the SQL from it and amend that before setting the .RecordSource to the resultant SQL.

Be that as it may, when you have the SQL you have to work on, you first need to check it doesn't already have the TOP predicate (of the SELECT clause) set. If so then it needs to be removed before adding your own. Adding this predicate is simple enough. You insert it into the existing SQL string at position 8 (after "SELECT "). The TOP predicate comes with an option to include the word PERCENT, which converts the value used to a percentage instead of absolute value.

NB. In sorted recordsets the Nth item may have equivalents. In this case all equivalents are included.
EG.
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 3
  2.          [Score]
  3.         ,[Name]
  4. FROM     [Results]
  5. ORDER BY [Score] ASC
Table=[Results]
Expand|Select|Wrap|Line Numbers
  1. [Score]  [Name]
  2.    5     Angela
  3.    2     Bob
  4.    2     Charlie
  5.    1     Denise
  6.    2     Eric
The results for this query would be :
Expand|Select|Wrap|Line Numbers
  1. 1, Denise
  2. 2, Charlie
  3. 2, Bob
  4. 2, Eric
Even though the query said TOP 3, 4 records resulted.

If it's important to avoid this then consider including some form of unique value in the SORT BY clause.
EG.
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 3
  2.          [Score]
  3.         ,[Name]
  4. FROM     [Results]
  5. ORDER BY [Score] ASC
  6.         ,[Name] ASC
The results for this query would be :
Expand|Select|Wrap|Line Numbers
  1. 1, Denise
  2. 2, Bob
  3. 2, Charlie
Jul 5 '10 #4
NeoPa
32,564 Recognized Expert Moderator MVP
munkee:
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = "Select * FROM logforfiltering WHERE" & strWhere2
I can just apply the TOP N clause in there then now I assume.
Yes, but you may want to add a space after "WHERE".

Personally I'd keep the filter away from the .RecordSource. That way the operator is told when the form is filtered and when not. I also find it more consistent, but it's your call of course.
Jul 5 '10 #5
munkee
374 Contributor
Thanks for all the info NeoPa and for the initial post Fish . I will give this a go now but it seems pretty straight forward now from the posts, thank you also for the help if the select starts returning more values than I wanted in my top N.. great stuff
Jul 5 '10 #6
NeoPa
32,564 Recognized Expert Moderator MVP
It's a pleasure :) I'm glad it helped.

If I may though, I'll switch the Best Answer posts as there's little of help for a new reader in the last one. Most of the information is in the previous one.
Jul 5 '10 #7
munkee
374 Contributor
No problem NeoPA I will also post the following code:

This allows the user to select a value for the TOP N records to show from a combo box. In my combo box (comboNrecords) I have set values of 1 , 5 , 10 , 100 , All . When you select from the combo box it will change the TOP N for the records to the selected. Working really well to be honest.

Expand|Select|Wrap|Line Numbers
  1. '------------------------------------------------
  2. ' Start coding to be able to select the number of records to display
  3. '------------------------------------------------
  4. Dim topN As String
  5. Dim topinsert As String
  6.  
  7. topN = comboNrecords.Value
  8.  
  9. If topN = "All" Then
  10. topinsert = "All"
  11.  
  12. Else
  13.  
  14. topinsert = "TOP " & topN
  15.  
  16. End If
  17.  
  18. '------------------------------------------------
  19. ' End coding to be able to select the number of records to display
  20. '------------------------------------------------
  21.  
  22.  
  23.  
  24.         'Finally, apply the string as the form's Filter.
  25.         'Me.RecordSource = "Select * FROM logforfiltering WHERE " & strWhere2
  26.        Me.RecordSource = "Select " & topinsert & " * FROM logforfiltering WHERE " & strWhere2
  27.        ' Forms!frmlogforfiltering.Form.FilterOn = True
Jul 5 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1782
by: Sunil Korah | last post by:
Hi, I want to update the data in the master table with data from another table. For example the following tables. Table1 - Account_No Name Last_Transaction_Date Table2 - Account_No
1
2386
by: Steven Britton via AccessMonster.com | last post by:
Follow Up question to the below posted by Allen Browne on 02/06/2005 - Could something like this work for a form that has a subform and the subform is a datasheet? _______________________________________________________________________ Re: Multiple Sort in a Continuous Form - Similiar to AutoFilter 06 Feb 2005 06:21 Allen Browne
15
3050
by: Jerry Alexander | last post by:
The Northwind Order Entry Application database is great! ----------------------------------------- But one thing is lacking: Real-time Stock Qty calculation! ----------------------------------------- I know that this topic has been discussed over & over in forums, but I have yet to find anyone that has shown the following: -----------------...
3
12483
by: Stewart | last post by:
Hi all! My (relatively small) database holds data on staff members and the projects (services) that they are assigned to. In my form frmStaff, I have a list of staff members - it is a continuous form. Each staff member is linked to a Service through a many-to-many relationship, using a junction table called jctStaffServices. I would like...
7
3369
by: Swinky | last post by:
Mr. Browne's copy code on his web site has saved me. I have been struggling to copy a record with several related sub-form tables. I found code on his web site that copies a sub-form table, implemented it and was successful to make it work. Thank you Mr. Browne! However, the copy code on his web site only updates one sub-form table. My...
3
2296
by: Photobug | last post by:
I have downloaded Allen Browne's function TableInfo() and am getting a ByRef Type Mismatch error when I try to execute it. I don't know if it is a reference problem or not, but my references for Access 2000 are set at: VB for Applications MS Access 9.0 Object Library OLE Automation MS ActiveX DataObjects 2.5 Library MS VB for Apps...
2
1644
by: sara | last post by:
I use Allen Browne's Audit Trail code in everything I do and I love it. Recently, I've run into a problem I can't figure out. I have a database with about 35 lookup tables. I am creating an "Admin" screen to allow the head social worker to maintain items in the list. (This is a volunteer project for a non-profit that is trying to help...
16
4134
by: blad3runn69 | last post by:
can anyone point me in the right direction re: handling multiple instances of a form opened from selected records in a search form I can get multiple forms thanks to allen browne & msdn howto get the new instance of the form to move to the record selected from the search form? thank you your help is muchly appreciated!
6
2201
by: OzairKhalid | last post by:
Hi, I have uploaded "ClientMultiSample2k.zip" at ... http://tech.groups.yahoo.com/group/MS_Access_Professionals/files/2_AssistanceNeeded/ClientMultiSample2k.zip The file is basically Allen Browne's sample. I have tried to get it customized for me. frmClient:
6
2733
by: babamc4 | last post by:
I have a main form (mainformlung) with 5 subforms (followupacute, followuplate, biochemresults, haemresults and pftresults). I have copied Allen Browne's Audit Trail code (thanks to Allen Browne) and this is working great, edit, insert etc is working bar when I try to delete a record in one of my subforms (I'm in test stage at the mo) I get a run...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7814
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5373
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.