设计访问表单以使用不同的查询

时间:2022-01-19 12:08:17

I had an Access Form that works very well for my customer, however they want 3 identical forms, but with different underrunning queries for different types of searches. I could copy and paste the same form and attach the different queries, but this can become a huge maintenance and maintainability issue. Is it possible to use multiple queries on the same form, or change out the query before opening the form?

我有一个适用于我的客户的访问表单,但他们需要3个相同的表单,但对不同类型的搜索具有不同的欠载查询。我可以复制并粘贴相同的表单并附加不同的查询,但这可能会成为一个巨大的维护和可维护性问题。是否可以在同一表单上使用多个查询,或者在打开表单之前更改查询?

1 个解决方案

#1


It sounds like your different queries all return the same SELECT field expressions, but have different WHERE clauses.

听起来你的不同查询都返回相同的SELECT字段表达式,但具有不同的WHERE子句。

If that is true, use a base query without a WHERE clause as the form's Record Source. Then you can essentially add a WHERE clause "on the fly" by using the DoCmd.OpenForm method's WhereCondition option when you open the form.

如果是这样,请使用不带WHERE子句的基本查询作为表单的记录源。然后,当您打开表单时,通过使用DoCmd.OpenForm方法的WhereCondition选项,您实际上可以“动态”添加WHERE子句。

Const cstrForm As String = "YourForm"
Dim strWhereCondition As String

Load strWhereCondition based on which dataset you want the form to display. These 3 examples assume the form's Record Source includes fields named proj id, proj name, and owner id whose datatypes are numeric, text, and numeric respectively:

根据您希望表单显示的数据集加载strWhereCondition。这3个示例假设表单的Record Source包含名为proj id,proj name和owner id的字段,其数据类型分别为numeric,text和numeric:

strWhereCondition = "[proj id]=" & SomeNumber
strWhereCondition = "[proj name]='" & SomeText & "'"
strWhereCondition = "[owner id]=" & SomeOtherNumber

Then open your form filtered by strWhereCondition:

然后打开由strWhereCondition过滤的表单:

DoCmd.OpenForm FormName:=cstrForm, WhereCondition:=strWhereCondition

#1


It sounds like your different queries all return the same SELECT field expressions, but have different WHERE clauses.

听起来你的不同查询都返回相同的SELECT字段表达式,但具有不同的WHERE子句。

If that is true, use a base query without a WHERE clause as the form's Record Source. Then you can essentially add a WHERE clause "on the fly" by using the DoCmd.OpenForm method's WhereCondition option when you open the form.

如果是这样,请使用不带WHERE子句的基本查询作为表单的记录源。然后,当您打开表单时,通过使用DoCmd.OpenForm方法的WhereCondition选项,您实际上可以“动态”添加WHERE子句。

Const cstrForm As String = "YourForm"
Dim strWhereCondition As String

Load strWhereCondition based on which dataset you want the form to display. These 3 examples assume the form's Record Source includes fields named proj id, proj name, and owner id whose datatypes are numeric, text, and numeric respectively:

根据您希望表单显示的数据集加载strWhereCondition。这3个示例假设表单的Record Source包含名为proj id,proj name和owner id的字段,其数据类型分别为numeric,text和numeric:

strWhereCondition = "[proj id]=" & SomeNumber
strWhereCondition = "[proj name]='" & SomeText & "'"
strWhereCondition = "[owner id]=" & SomeOtherNumber

Then open your form filtered by strWhereCondition:

然后打开由strWhereCondition过滤的表单:

DoCmd.OpenForm FormName:=cstrForm, WhereCondition:=strWhereCondition