Another way to do this, which no one has mentioned to you before, is to associate your graph with one saved QueryDef, and then rewrite QueryDef at runtime. Now I do not recommend changing saved QueryDefs for most contexts, because it causes foreground bloating and is usually not required (most contexts in which you use saved QueryDef can be filtered anyway in the context in which they are used, for example, as a form Recordsource, you just pass one argument to DoCmd.OpenForm).
Graphs are different because the SQL that controls the graphs cannot be modified at runtime.
Some of them suggested parameters, but when you open a form with a graph on it that uses an SQL string with parameters, you will open the default parameter dialogs. One way to avoid this is to use the dialog form to collect criteria, and then set the links to the controls in the dialog form as parameters, for example:
PARAMETERS [Forms]![MyForm]![ID] Long;
If you use form links, it is important that you do this because from Access 2002 on, the Expression Expression service does not always correctly handle them when the controls are Null. Defining them as parameters eliminates this problem (which was not the case before Access XP).
One situation in which you must rewrite QueryDef for a graph is if you want to allow the user to select N in the TOP N SQL statement. In other words, if you want them to be able to select TOP 5 or TOP 10 or TOP 20, you will have to change the saved QueryDef, since N cannot be parameterized.
David-W-Fenton
source share