473,419 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Access Project (adp) - Filter form and sub totals

Hello,

I'm using a Microsft Access 2000 Project (adp file) connected to MS SQL Server 2000 database.

I have created a continous form having a detail part (results of the query) and a footer part (aggregate functions).

My problem concerns the sub-totals calculated in the footer part that are not updated when the results are filtered in the form.

For exemple, if we have the following results

ID City Country Population
1 Paris France 2.100.000
2 New-York USA 8.100.000
3 Marseille France 800.000

The sub-total calculated is 11.000.000 which is correct.

However, when I filter the results in the form to the country "France" (right filter by selection), the sub-total calculated is still 11.000.000 which is wrong. :confused:

Anyone have the same problem ?

Anyone having an idea ?

I have tested this functionnality with a Access database (.mdb file) and the sub-total is correctly updated when the results are filtered. :eek:

Why a different behavior between an "adp" and a "mdb" file ?

Anyone having an idea to get the correct sub-totals with filtered results ?

Thank you for your help.

Terrybens
Jul 17 '06 #1

✓ answered by Didier Baille

Hello,
I come back on this subject more than 3 years after the last post, because I want to share my solution to this problem with anybody facing it, and because I didn’t find a full working solution anywhere in the web.

I have an Access project (ADP) using SQL Server 2005.
I have some statistics forms that are doing parameterized query, build specifically responding to forms controls. So, I can’t use a SQL Server view as a source for my form.

A first idea was to use SQL Server temporary table, to respond a Microsoft support article saying that sum can’t be done on calculated fields. This had for effect to make sum TextBox in footer working (no more #Error). But, unfortunately, when applying an Access filter, sums where not actualized, even with a Requery.

The second thing was to use the Form_ApplyFilter, to pass the local Access filter to Me.ServerFilter. And it’s works!

Here is bellow a summary of my code.

Very important: As Access can’t run more than one instance of a form, using a temporary table (in my code bellow, named #myTemporaryTable) will not be a problem. But, if you have to use this technique in more than one form of you application, be careful to use different names in each form for your temporary table. To avoid any problem, I use the form name, followed by “Data”, and of course prefixed by # (this is mandatory).
Also, note the closing of this temporary table, in Form_Close, but also just before to create it, just to be sure to avoid any error.
In Form_ApplyFilter, note the substitution of double cote by simple cote, and also the transformation of the access alike word in standard SQL like word.
Expand|Select|Wrap|Line Numbers
  1.  
  2. ‘ Opening form
  3. Private Sub Form_Open(Cancel As Integer)
  4.     …
  5.     LoadStatRecordSet
  6. End Sub
  7.  
  8. Private Sub Form_Close()
  9.     globalConnexion.Execute "IF OBJECT_ID('tempdb..#myTemporaryTable’) IS NOT NULL DROP TABLE #myTemporaryTable’"
  10. End Sub
  11.  
  12. ‘ This Sub construct the query based on user inputs, and open a SQL temporary table
  13. ‘ This SQL temporary table is private to user connexion, so no problem for multi users.
  14. ‘ note: globalConnexion is your ADODB.Connection to access SQL Server
  15. Private Sub LoadStatRecordSet()
  16.     Dim strSql As String
  17.  
  18.     strSql = "SELECT <here, your fields>
  19.         "INTO #myTemporaryTable " & _
  20.         "FROM <here, your source tables with join>" & _
  21.         "WHERE <here, your conditions, May responds to user inputs>"
  22.     strSql = strSql & " ORDER BY <here, your fields for sorting> "
  23.  
  24.     globalConnexion.Execute "IF OBJECT_ID('tempdb..#myTemporaryTable’) IS NOT NULL DROP TABLE #myTemporaryTable"
  25.     globalConnexion.Execute (strSql)
  26.  
  27.     Me.RecordSource = "#myTemporaryTable"   ‘ a SELECT * FROM #myTemporaryTable doesn’t works !
  28.  
  29. End Sub
  30.  
  31.  
  32. Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
  33.     On Error GoTo Form_ApplyFilter_Err
  34.  
  35.     If ApplyType = 1 Then   'If a filter is being applied...
  36.         Me.ServerFilter = Replace(Replace(Me.Filter, """", "'"), "Alike", "Like")     'Replace double-quote (") with single-quotes (')
  37.     Else    'The filter is being un-applied so remove it from the server filter.
  38.         Me.ServerFilter = ""
  39.         LoadStatRecordSet
  40.     End If
  41.     Me.Requery
  42.  
  43. Form_ApplyFilter_Done:
  44.     Exit Sub
  45.  
  46. Form_ApplyFilter_Err:
  47.     <here, your code to inform user, log error…, like a msgBox or anything else)
  48.     Resume Form_ApplyFilter_Done
  49.  
  50. End Sub
Hope this can help you!

13 11230
comteck
179 100+
How are you calculating the result?

comteck
Jul 17 '06 #2
=Sum([Database_field])

Terrybens
Jul 18 '06 #3
I thought about adding VBA on the filter event to recalculate the totals but this seems to be quite difficult. There should be another solution because there is no problem with an mdb file.

All my queries are in proc stocks so I cannot use an mdb file.

Terrybens.
Jul 19 '06 #4
Hi Terry

Perhaps you've solved this by now, but if not, the issue is with the way an ADP fetches the data from the server. The normal .Filter propery fetches the data and then applies a filter to it to choose which records to display. It seems that the totals on the form are calculated before this process on an ADP, which is what causes this problem.

The solution is actually dead simple:

Rather than using:
Form.Filter = "Your Filter"

You need to use:
Form.ServerFilter = "Your Filter"
Form.Requery (If the filter is being applied in VBA you'll need one of these)

The ServerFilter property for the form is right down at the bottom of the property list.

Using this method the filter is applied at the server end before the data is fetched, so not only will your totals now be correct, but the query will run more quickly aswell.

Hope this helps
Aug 4 '06 #5
Hi,

Thanx a lot Dave for your help.

However in my case the problem remains. I use a proc stock to get my data and a server filter cannot be applied to a proc stock :confused: .

My totals then remain wrong :( .

I need a proc stock to get my data.

Any other idea ? I thought about temporary tables but this is quite to complicated.

Thank you guys for your help.

Terry
Aug 25 '06 #6
comteck
179 100+
Terry,

Try doing your filtering in a query. Then do the calculation in the query itself as an expression.

The SQL statement would be sort of like this:

SELECT Sum([Population]) AS ExpressionName
FROM tblTableName
WHERE (((tblTableName.Country)="France"));

If you don't know SQL very well, that is ok. Just go into query design view, switch over to SQL view from the view icon in the upper left corner, and enter the code above (replace fieldname and tblTableName with your appropriate names - same for Population and Country. Replace these with whatever you named your table/fields). Also, use whatever ExpressionName that you like - It doesn't make any difference.

When you've entered the SQL, switch back over to design view, and the word "France" will be in the criteria box under Country. If you want to be prompted for a country instead, replace the word "France" with the following:

[Enter Country Name]

Try running the query by itself before assigning it to a form, to see if it works ok.

Hope this helps.
comteck
Aug 25 '06 #7
Thank you comteck for your reply.

I cannot use "sum" in the source of my form because the form displays a list of results while a "sum" function in a query will return an aggregated line.

I can however calculate the sub-total in another query using VBA code, which is your solution I guess. On the event "apply filter" of the form I can run a piece of code to calculate the sub-total.

I have tried this but the sub-totals fields in my footer are in "read only" because I have not specified a unique table in my form. However I cannot choose one (nothing in the listbox) because my record source is proc stock.

I have no idea how to solve this problem.

Thank you guys for your help.

Terry
Aug 25 '06 #8
MMcCarthy
14,534 Expert Mod 8TB
The calculated fields are unbound fields on your form as in they are not bound to the table or query which is the record source of the form. As such when you update the records the unbound fields are not automatically updated.

However, assuming the field are calculated based on values in the form update, simply requery each calculation. That is:

For each calculated field in the form footer:

Me. FieldName. Requery

If this does not solve your problem please let me know.

Thank you comteck for your reply.

I cannot use "sum" in the source of my form because the form displays a list of results while a "sum" function in a query will return an aggregated line.

I can however calculate the sub-total in another query using VBA code, which is your solution I guess. On the event "apply filter" of the form I can run a piece of code to calculate the sub-total.

I have tried this but the sub-totals fields in my footer are in "read only" because I have not specified a unique table in my form. However I cannot choose one (nothing in the listbox) because my record source is proc stock.

I have no idea how to solve this problem.

Thank you guys for your help.

Terry
Aug 25 '06 #9
Hi everybody,

The subtotals text field are unbound fields. I have coded the calculation on the "apply filter" event.

I solved my last difficulty which was a message like "The form is read only because no unique table has been indicated".

I don't know why but changing these parameters of the field : enabled = no, locked = yes solved the problem.

Thank you guys for your replies.

Terrybens
Aug 30 '06 #10
PEB
1,418 Expert 1GB
And maybe you can Requery all your form, using commands like:
Me.Requery
Me.Refresh
Me.Repaint
docmd.showallrecords
You can see more info in Access help for those methods!
Aug 30 '06 #11
Hi,
I'm struggling with the same problem now. What kind of code did you enter at the "apply filter" event?
Thanks,
Lana

Thank you comteck for your reply.

I cannot use "sum" in the source of my form because the form displays a list of results while a "sum" function in a query will return an aggregated line.

I can however calculate the sub-total in another query using VBA code, which is your solution I guess. On the event "apply filter" of the form I can run a piece of code to calculate the sub-total.

I have tried this but the sub-totals fields in my footer are in "read only" because I have not specified a unique table in my form. However I cannot choose one (nothing in the listbox) because my record source is proc stock.

I have no idea how to solve this problem.

Thank you guys for your help.

Terry
Dec 19 '06 #12
Hello,
I come back on this subject more than 3 years after the last post, because I want to share my solution to this problem with anybody facing it, and because I didn’t find a full working solution anywhere in the web.

I have an Access project (ADP) using SQL Server 2005.
I have some statistics forms that are doing parameterized query, build specifically responding to forms controls. So, I can’t use a SQL Server view as a source for my form.

A first idea was to use SQL Server temporary table, to respond a Microsoft support article saying that sum can’t be done on calculated fields. This had for effect to make sum TextBox in footer working (no more #Error). But, unfortunately, when applying an Access filter, sums where not actualized, even with a Requery.

The second thing was to use the Form_ApplyFilter, to pass the local Access filter to Me.ServerFilter. And it’s works!

Here is bellow a summary of my code.

Very important: As Access can’t run more than one instance of a form, using a temporary table (in my code bellow, named #myTemporaryTable) will not be a problem. But, if you have to use this technique in more than one form of you application, be careful to use different names in each form for your temporary table. To avoid any problem, I use the form name, followed by “Data”, and of course prefixed by # (this is mandatory).
Also, note the closing of this temporary table, in Form_Close, but also just before to create it, just to be sure to avoid any error.
In Form_ApplyFilter, note the substitution of double cote by simple cote, and also the transformation of the access alike word in standard SQL like word.
Expand|Select|Wrap|Line Numbers
  1.  
  2. ‘ Opening form
  3. Private Sub Form_Open(Cancel As Integer)
  4.     …
  5.     LoadStatRecordSet
  6. End Sub
  7.  
  8. Private Sub Form_Close()
  9.     globalConnexion.Execute "IF OBJECT_ID('tempdb..#myTemporaryTable’) IS NOT NULL DROP TABLE #myTemporaryTable’"
  10. End Sub
  11.  
  12. ‘ This Sub construct the query based on user inputs, and open a SQL temporary table
  13. ‘ This SQL temporary table is private to user connexion, so no problem for multi users.
  14. ‘ note: globalConnexion is your ADODB.Connection to access SQL Server
  15. Private Sub LoadStatRecordSet()
  16.     Dim strSql As String
  17.  
  18.     strSql = "SELECT <here, your fields>
  19.         "INTO #myTemporaryTable " & _
  20.         "FROM <here, your source tables with join>" & _
  21.         "WHERE <here, your conditions, May responds to user inputs>"
  22.     strSql = strSql & " ORDER BY <here, your fields for sorting> "
  23.  
  24.     globalConnexion.Execute "IF OBJECT_ID('tempdb..#myTemporaryTable’) IS NOT NULL DROP TABLE #myTemporaryTable"
  25.     globalConnexion.Execute (strSql)
  26.  
  27.     Me.RecordSource = "#myTemporaryTable"   ‘ a SELECT * FROM #myTemporaryTable doesn’t works !
  28.  
  29. End Sub
  30.  
  31.  
  32. Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
  33.     On Error GoTo Form_ApplyFilter_Err
  34.  
  35.     If ApplyType = 1 Then   'If a filter is being applied...
  36.         Me.ServerFilter = Replace(Replace(Me.Filter, """", "'"), "Alike", "Like")     'Replace double-quote (") with single-quotes (')
  37.     Else    'The filter is being un-applied so remove it from the server filter.
  38.         Me.ServerFilter = ""
  39.         LoadStatRecordSet
  40.     End If
  41.     Me.Requery
  42.  
  43. Form_ApplyFilter_Done:
  44.     Exit Sub
  45.  
  46. Form_ApplyFilter_Err:
  47.     <here, your code to inform user, log error…, like a msgBox or anything else)
  48.     Resume Form_ApplyFilter_Done
  49.  
  50. End Sub
Hope this can help you!
May 10 '10 #13
MMcCarthy
14,534 Expert Mod 8TB
Thanks for posting your solution. It's much appreciated.
May 10 '10 #14

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

Similar topics

5
by: kackson | last post by:
Hi. I would like to access my database outside of my company. I read many documents but they are all pertaining to accessing the database via ASP or some form of web application. Is there no...
7
by: rickcheney | last post by:
I just changed my Access 2002 database to a SQL Server ADP project. I had a form where the user entered a value into a text box and when a command button on the form was clicked a Report was...
7
by: damjanu | last post by:
Hi All; I need little help. I have a datasheet form. I allow user to do 'filter by selection'. My form contains a column with values. As user changes selections, I want to calculate totals....
0
by: M. Farrenkopf | last post by:
I am in the process of converting a Jet database to ADP using SQL Server 2000. Most of this conversation has been smooth, but now I'm running across a problem that has me stumped. Access 2000 SP3...
0
by: Malcolm Cook | last post by:
I've discovered: Using "File > Print Preview" in a form's datasheet view with a server Filter crashes access after previewing... ....that is, unless ServerFilterByForm has been turned off...
13
by: Greg Strong | last post by:
Hello All, Hello All, What are the ToDo's / Gotchas to convert an MDB Access 2K2 database to an Access Project (i.e. ADP) file for the front end using Microsoft SQL Server 2000 Desktop Engine...
1
by: ken | last post by:
Hi, Lets say we have a form that displays time card entries based on the calendar control date on the form. So the user clicks on a date and the form filters the table where the time card entries...
49
by: Allen Browne | last post by:
If you are looking for opinon on what's useful in Access 2007, there's a new article at: http://allenbrowne.com/Access2007.html Covers what's good (useful features), what's mixed (good and bad),...
8
by: elias.farah | last post by:
Hello Everyone, I'm having some very weird behavior on a couple of Access forms. (Not all forms, just some of them). The forms have been working for years, under Access XP/2003 etc, and last...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.