MS Access: How to Supply Parameters to a Query-Based Subform Control
Given the following setup, how do you supply parameters to the query?:
- FormA contains SubformA.
- SubformA uses QueryA, a saved query, as its SourceObject.
- QueryA uses a PARAMETERS declaration
- FormA contains Controls that supply the parameters to QueryA.
If FormA's Controls have the same name as QueryA's parameters, this generally works. The exception is when the form loads, at which time Access prompts the user for QueryA's parameters. I am trying to get around this by leaving SubformA's SourceObject blank in Design View and defining the SourceObject in FormA's Form_Load event. I am setting the values of FormA's controls and then setting SubformA's SourceObject. However, Access is still prompting for the parameters. My next thought was to get a reference to QueryA as a QueryDef and define the QueryDef's parameters, but there doesn't appear to be a way to assign the Query开发者_如何转开发Def as SubformA's SourceObject. Any thoughts on how to obtain the objective of not prompting the user for the parameters on form load?
Have you defined the controls as parameters in the Recordsource of the subform? I'm not sure it would work even then, since I think subforms load before the main form, and that they are requeried a second time after loading.
I'd suggest having an empty recordsource in the subform, and set the Recordsource SQL when you apply the filter. I wouldn't define any parameters and I would use no references to form controls in the Recordsource SQL at all.
If you are somehow concerned about SQL injection and think you must use parameters, likely your concerns are overblown, particularly if none of the filter controls allow free text.
精彩评论