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: - Option Compare Database
-
-
Option Explicit
-
-
Private Sub btnSearchit_Click()
-
'Purpose: Build up the criteria string form the non-blank search boxes, and apply to the form's Filter.
-
'Notes: 1. We tack " AND " on the end of each condition so you can easily add more search boxes; _
-
we remove the trailing " AND " at the end.
-
' 2. The date range works like this: _
-
Both dates = only dates between (both inclusive. _
-
Start date only = all dates from this one onwards; _
-
End date only = all dates up to (and including this one).
-
Dim strWhere2 As String 'The criteria string.
-
Dim lngLen2 As Long 'Length of the criteria string to append to.
-
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
-
-
'***********************************************************************
-
'Begin the filter codes to create the query
-
'***********************************************************************
-
-
-
'------------------------------------------------
-
'Begin coding for date search of date occured
-
'------------------------------------------------
-
-
'Date field example. Use the format string to add the # delimiters and get the right international format.
-
If Not IsNull(Me.txtfilterDateStart) Then
-
strWhere2 = strWhere2 & "([DteOccur] >= " & Format(Me.txtfilterDateStart, conJetDate) & ") AND "
-
End If
-
-
'Another date field example. Use "less than the next day" since this field has times as well as dates.
-
If Not IsNull(Me.txtfilterDateEnd) Then 'Less than the next day.
-
strWhere2 = strWhere2 & "([DteOccur] < " & Format(Me.txtfilterDateEnd + 1, conJetDate) & ") AND "
-
End If
-
-
-
-
'------------------------------------------------
-
' End coding for date searching of date occured
-
'------------------------------------------------
-
-
'------------------------------------------------
-
' Start coding for list box to see department raised
-
'------------------------------------------------
-
-
'Listbox example.
-
If Me.lstfilterDeptRaised.ItemsSelected.Count > 0 Then
-
Dim itm As Variant
-
Dim strList As String
-
For Each itm In Me.lstfilterDeptRaised.ItemsSelected
-
If strList = "" Then
-
strList = "([DeptRaisedBy] = " & Me.lstfilterDeptRaised.Column(0, itm) & ")"
-
Else
-
strList = strList & " OR " & "([DeptRaisedBy] = " & Me.lstfilterDeptRaised.Column(0, itm) & ")"
-
End If
-
Next
-
strWhere2 = strWhere2 & "(" & strList & ") AND "
-
End If
-
-
'------------------------------------------------
-
' End coding for list box to see department raised
-
'------------------------------------------------
-
-
-
-
'------------------------------------------------
-
' Start coding for list box to see department responsible
-
'------------------------------------------------
-
-
'Listbox example.
-
If Me.lstfilterDeptResp.ItemsSelected.Count > 0 Then
-
Dim itm2 As Variant
-
Dim strList2 As String
-
For Each itm2 In Me.lstfilterDeptResp.ItemsSelected
-
If strList2 = "" Then
-
strList2 = "([DeptResp] = " & Me.lstfilterDeptResp.Column(0, itm2) & ")"
-
Else
-
strList2 = strList2 & " OR " & "([DeptResp] = " & Me.lstfilterDeptResp.Column(0, itm2) & ")"
-
End If
-
Next
-
strWhere2 = strWhere2 & "(" & strList2 & ") AND "
-
End If
-
-
'------------------------------------------------
-
' End coding for list box to see department responsible
-
'------------------------------------------------
-
-
-
'***********************************************************************
-
'Chop off the trailing " AND ", and use the string as the form's Filter.
-
'***********************************************************************
-
'See if the string has more than 5 characters (a trailng " AND ") to remove.
-
lngLen2 = Len(strWhere2) - 5
-
If lngLen2 <= 0 Then 'Nah: there was nothing in the string.
-
MsgBox "No criteria", vbInformation, "Nothing to do."
-
Else 'Yep: there is something there, so remove the " AND " at the end.
-
strWhere2 = Left$(strWhere2, lngLen2)
-
'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
-
'Debug.Print strWhere2
-
-
'Finally, apply the string as the form's Filter.
-
Forms!frmlogforfiltering.Form.Filter = strWhere2
-
Forms!frmlogforfiltering.Form.FilterOn = True
-
End If
-
End Sub
-
-
Private Sub btnResetit_Click()
-
'Purpose: Clear all the search boxes in the Form Header, and show all records again.
-
Dim ctl As Control
-
-
'Clear all the controls in the Form Header section.
-
For Each ctl In Me.Section(acHeader).Controls
-
Select Case ctl.ControlType
-
Case acTextBox, acComboBox
-
ctl.Value = Null
-
Case acCheckBox
-
ctl.Value = False
-
End Select
-
Next
-
-
'Remove the form's filter.
-
Me.Filter = "(False)"
-
Me.FilterOn = True
-
End Sub
-
-
Private Sub Form_BeforeInsert(Cancel As Integer)
-
'To avoid problems if the filter returns no records, we did not set its AllowAdditions to No.
-
'We prevent new records by cancelling the form's BeforeInsert event instead.
-
'The problems are explained at http://allenbrowne.com/bug-06.html
-
Cancel = True
-
MsgBox "You cannot add new clients to the search form.", vbInformation, "Permission denied."
-
End Sub
-
-
Private Sub Form_Open(Cancel As Integer)
-
'Remove the single quote from these lines if you want to initially show no records.
-
Me.Filter = "(False)"
-
Me.FilterOn = True
-
End Sub
-
-
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
Updated: managed to put in the recorsource change.. if you viewed this before the edit I must have been having a retarded mind block. - 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
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. - SELECT TOP 3
-
[Score]
-
,[Name]
-
FROM [Results]
-
ORDER BY [Score] ASC
Table=[Results] - [Score] [Name]
-
5 Angela
-
2 Bob
-
2 Charlie
-
1 Denise
-
2 Eric
The results for this query would be : - 1, Denise
-
2, Charlie
-
2, Bob
-
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. - SELECT TOP 3
-
[Score]
-
,[Name]
-
FROM [Results]
-
ORDER BY [Score] ASC
-
,[Name] ASC
The results for this query would be : - 1, Denise
-
2, Bob
-
2, Charlie
NeoPa 32,564
Recognized Expert Moderator MVP munkee: - 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.
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
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.
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. - '------------------------------------------------
-
' Start coding to be able to select the number of records to display
-
'------------------------------------------------
-
Dim topN As String
-
Dim topinsert As String
-
-
topN = comboNrecords.Value
-
-
If topN = "All" Then
-
topinsert = "All"
-
-
Else
-
-
topinsert = "TOP " & topN
-
-
End If
-
-
'------------------------------------------------
-
' End coding to be able to select the number of records to display
-
'------------------------------------------------
-
-
-
-
'Finally, apply the string as the form's Filter.
-
'Me.RecordSource = "Select * FROM logforfiltering WHERE " & strWhere2
-
Me.RecordSource = "Select " & topinsert & " * FROM logforfiltering WHERE " & strWhere2
-
' Forms!frmlogforfiltering.Form.FilterOn = True
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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:
-----------------...
|
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...
|
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...
| |
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...
|
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...
|
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!
|
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:
|
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...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |