|
 |
 |
 |
 |
Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
Trademarks
Article ID: 285162 - Last Review: October 11, 2006 - Revision: 3.4 ACC2002: How to Use Server Filters on Data Access Pages Without a Web ServerThis article was previously published under Q285162 Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
For a Microsoft Access 2000 version of this article, see 237377Â
(http://kbalertz.com/Feedback.aspx?kbNumber=237377/EN-US/
)
.
This article demonstrates how to create script that filters a data access page by setting the ServerFilter property. You can use this script even when the page is not stored on a Web server.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following steps demonstrate how to create a page that is based on the Customers table. When you open the page or click the New Filter button, the script prompts you for a list of countries. The script then limits the number of records that are returned based on the countries that you entered. You can return all records by leaving the list empty.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database. - Open the sample database Northwind.mdb.
- Create a new page in Design view.
- If the field list is not displayed, on the View menu, click Field List.
- From the Customers table, drag the CustomerID, the CompanyName, the City, the Region, and the Country fields to the page.
- Add a command button to the page, and then set the following properties:
ID: cmdFilter
Inner Text: New Filter
- On the Tools menu, point to Macro, and then click Microsoft Script Editor.
- On the Edit menu, point to Insert Script Block, and then click Client. This will insert script tags as follows:
<script language=vbscript>
<!--
-->
</script>
- Type or paste the following function within the script tags:
Function SetServerFilter()
Dim strFilter, myFilter
strPrmt = "What country do you want to filter by?" & vbCrLf & _
"Separate the list of countries by a semicolon (;)." & _
vbCrLf & "To return all records, leave the box empty."
'Prompt the user for list of countries that are to be returned.
strFilter = InputBox(strPrmt)
If strFilter = "" Then
'Clear the filter criteria when nothing is typed in the input box.
'All records are returned in this case.
myFilter = ""
Else
'Loop through the user's list until there is only one country left.
Do Until InStr(strFilter, ";") = 0
'Add the first country from the list to the filter criteria.
myFilter = myFilter & "[Country] = " & Chr(39) & _
Left(strFilter, InStr(strFilter, ";") - 1) & _
Chr(39) & " OR "
'Remove the first country from the list.
strFilter = Right(strFilter, Len(strFilter) - InStr(strFilter, ";"))
Loop
'Add the final country from the user's list to the filter criteria.
myFilter = myFilter & "[Country] = " & Chr(39) & strFilter & Chr(39)
End If
'Set the server filter.
MSODSC.RecordsetDefs.Item(0).ServerFilter = myFilter
End Function
- In the Object list, click cmdFilter, and in the Event list, click onclick. This will insert script tags as follows:
<SCRIPT language=vbscript for=cmdFilter event=onclick>
<!--
-->
</SCRIPT>
- Add the following single line of script within the tags:
The script should now appear as follows:
<SCRIPT language=vbscript for=cmdFilter event=onclick>
<!--
SetServerFilter
-->
</SCRIPT>
- Save the page as dapSvrFilterEx, and then close the Microsoft Script Editor.
- On the View menu, click Page View.
- Click the Filter button, and then in the input box, type Canada; Mexico; USA. Click OK. Approximately 22 records should be returned.
APPLIES TO- Microsoft Access 2002 Standard Edition
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
|
 |
 |
 |
 |
 |
 |
 |
| |