设计与动态SQL作为记录源Access窗体窗体、动态、SQL、Access

2023-09-08 11:11:35 作者:弑神者

我是pretty的新进入VBA编程。这里有一个问题,我在创建Access中的窗体时。我需要在窗体的记录链接到我已经定义了一个查询对象。比方说,我有FIELD1,FIELD2,FIELD3,等我的查询。最终的产品,我想present是

I am pretty new to Access VBA programming. Here is one problem I have when creating a form in Access. I need to link the form's recordsource to a query object which I have already defined. Let's say I have field1, field2, field3, etc. in my query. The end product I would like to present is

在窗体的顶部,有几个文本框供用户的字段1输入过滤条件,场2,FIELD3 ......和 在一个按钮的点击,在与筛选条件的形式底部的数据表显示适用 除了在我的查询现有的领域,我需要包括在数据表的基础上,从FIELD1和FIELD2一定的计算结果的自定义列的末尾。有些文字可以显示,例如迟到,早或准时

我已经提出了两种设计方案,但由于缺乏VBA编程经验,我没有能够做到其中任何一个的现在...

I have come up with two design approaches, but due to the lack of experience in VBA programming I am not able to accomplish either one of them right now...

设计1:

我有一个标准的输入文本框一种主要形式。这主要表现形式是不与任何数据源。然后,我有一个窗体底部的数据源动态地在主窗体读取文本框值设置的。

I have a main form with the criteria input text boxes. This main form is not linked to any data source. Then I have a subform at bottom whose data source is dynamically set by reading the text boxes' values in the main form.

设计2:

我有一个单一的形式,但我转移所有标准箱到窗体页眉部分。这种形式已经从我的查询对象中的记录源。细节部分用于显示设置为用户的标准窗体的筛选器属性的数据表。

I have a single form, but I shift all the criteria boxes to the Form Header section. This form has a record source from my query object. The detail section is used for displaying the data sheet with the form's filter property set to user criteria.

我不能够做到设计1,因为我不知道如何动态设置数据源为我的子表单。我也无法做到设计2,因为即使我在设计视图中窗体页眉/页脚启用已检查了我​​的窗体的标题部分不会出现。

I am not able to do design 1 because I don't know how to set the data source for my sub form dynamically. I am also not able to do design 2 because my form's header section does not appear even I have checked in the design view the form header/footer is enabled.

我能做些什么来实现我的外形设计?任何替代设计的建议?

What can I do to realize my form design? Any alternative design suggestions?

我使用Access 2003年这方面的发展,但我希望我的code可以升级到未来的Access 2010。

I am using Access 2003 for this development, but I hope my code can be upgradable to Access 2010 in future.

在此先感谢您的咨询。

推荐答案

通过这两种设计,你应该动态地建立数据源,是这样的:

With both designs you should build the datasource dynamically, something like this:

sql = "SELECT * FROM MyTableOrQuery WHERE 1=1"
If Not IsNull(textBox1) Then
  sql = sql & " And Field1 = '" & textBox1 & "'"
End If
If Not IsNull(textBox2) Then
  sql = sql & " And Field2 = '" & textBox2 & "'"
End If

最后,指定SQL到表单数据源。

And, finally, assign the sql to the form datasource.

设计1

下面code进去的主要形式,毗邻上面:

The below code goes in the main form, just next to the above:

SubFormControlName.Form.RecordSource = sql

设计2

要显示的控件在标题部分中,您需要将默认视图属性设置为运行形式的(我不知道extact翻译,我在西班牙访问工作) 在这种情况下,数据源被分配给主模

To show the controls in the header section you need to set the property DefaultView to running forms (I don't know the extact translation, I work with Access in spanish) In this case, the datasource is assigned to the main form

Me.recourdsource = sql

有关你的第三点: 您可以通过建立计算字段

For your third point: You can build calculated field using

Iif(fieldA<5,"Early",Iif(fieldA>10,"Late","OnTime"))

也可以使用条件格式。我觉得这个选择是最清晰的。

or you can use conditional formatting. I think this option is most clear