如何引用连续子窗体控件
我有一个 Access 2003 窗体,其中有一个子窗体,在子窗体控件中设置为连续窗体。主窗体中的一条记录,子窗体中将出现 1 到多条记录。数据显示正常。
主窗体名为Widgets,子窗体名为Transactions。有 5 个文本框控件用于显示子窗体中的数据。有问题的是ReceiptDate。
我想要做的是查看这些值并确定是否有 2009 年的收据,如果有,则将该行的背景更改为黄色,以便在用户遇到该情况时突出显示。甚至可能将日期字段的字体更改为粗体。
我尝试了多种引用子窗体控件的方法。当我尝试 Me.Transactions.ReceiptDate
时,我只收到了该子表单中的第一条记录。我希望能够循环遍历它们并查看是否满足条件。我尝试了 Me.Transactions.ReceiptDate(1)
和 Me.Transactions.ReceiptDate(0)
等等。
我也尝试了 For Each ctl In Form.Controls 路线。它工作了几次迭代,然后我收到运行时错误 2455“您输入的表达式对属性表单/报告的引用无效”。
我的子窗体处于“数据表”模式,但认为这导致我无法读取子窗体控件数组。所以我将其更改为“连续”模式。我得到同样的错误。
有没有办法引用子表单中的特定“行”并根据找到的值执行某些操作?另外,我在 On Current 事件中执行此操作,因为我不知道还可以将代码放在哪里。子表单在父表单之前加载,因此这些控件甚至可能没有完全“存在”,但当我在“立即窗口”中尝试时,我确实得到了第一行的日期。
更新12.23.2010:
在@Remou的慷慨帮助下,我能够从RecordSet中debug.print ReceiptDate字段。这很棒,因为现在我可以评估数据并根据值执行某些操作。@Remou 的代码帮助我将其放入 OnCurrent 事件中:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move frm.SelTop - 1
' Enumerate the list of selected records presenting the ReceiptDate field
For i = 1 To rs.RecordCount
Debug.Print rs![ReceiptDate]
rs.MoveNext
Next i
所以现在我能够知道子表单中的哪一行有来自2009 年,当我在 for 循环中遇到整行或几行时,我需要能够突出显示它们。我如何引用实际行?数据表视图或连续表单视图 - 我都尝试过。
条件格式很棒,但它只允许我突出显示一个特定记录,我更愿意通过 VBA 来完成此操作,因为……从这里开始,我希望让用户能够单击任何记录在子表单中获取收据详细信息并可能打印它们。
有什么想法吗?
I have an Access 2003 form with one subform, set to continuous forms, in a subform control. For one record in the main form, 1 to many records will appear in the sub form. The data displays properly.
The main form is named Widgets and the sub form is named Transactions. There are 5 textbox controls that display the data in the subform. The one in question is ReceiptDate.
What I would like to do is look at the values and determine if there was a receipt for the year 2009, and if so then change the background of that row to yellow so it stands out when the user encounters that condition. Maybe even change the date field's font to boldface..
I tried many ways of referencing the subform's controls. When I have tried Me.Transactions.ReceiptDate
I have only received the first record in that subform. I'd like to be able to loop through them and see if the condition is met. I tried Me.Transactions.ReceiptDate(1)
and Me.Transactions.ReceiptDate(0)
and so forth.
I tried the For Each ctl In Form.Controls
route as well. It worked for a few iterations and then I received a run-time error 2455 "You entered an expression that has an invalid reference to the property Form/Report".
I had the subform in "datasheet" mode but thought that was causing me not to be able to read through an array of subform controls. So I changed it to "continuous" mode. I get the same errors for either.
Is there a way to reference specific "rows" in the subform and do something based on a value found? Also, I am performing this in the On Current event as I dont know where else to put the code. The subform loads before the parent form so its possible that these controls arent even fully "there" but then I do get the first row's date when I try it in the Immediate Window.
UPDATE 12.23.2010:
With the gracious help of @Remou I am able to debug.print the ReceiptDate fields from the RecordSet. This is great because now I can evaluate the data and do certain things based on the values.. The @Remou's code helped me put this into the OnCurrent event:
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
' Get the form and its recordset.
Set frm = Me.Form
Set rs = frm.RecordsetClone
' Move to the first record in the recordset.
rs.MoveFirst
' Move to the first selected record.
rs.Move frm.SelTop - 1
' Enumerate the list of selected records presenting the ReceiptDate field
For i = 1 To rs.RecordCount
Debug.Print rs![ReceiptDate]
rs.MoveNext
Next i
So now that I am able to know which row in my subform has a receipt from 2009, I need to be able to highlight the entire row or rows as I come across them in that for loop. How can I reference the actual row? Datasheet view or Continuous Forms view - I have tried both.
Conditional Formatting is great but it only allows me to highlight one particular record and I'd much rather be able to do this via VBA because...... from here I will want to give the use the ability to click on any record in the subform and get the receipt details and potentially print them.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,最好使用条件格式。
要引用子窗体上的控件,请按名称引用子窗体控件,然后引用要获取包含的窗体的窗体属性,然后按名称引用控件:
请参阅:http://www.mvps.org/access/forms/frm0031.htm
In this situation, it is best to use conditional formatting.
To refer to a control on a subform, refer to the subform control by name, then the form property to get the form contained, then the control by name:
See: http://www.mvps.org/access/forms/frm0031.htm
我终于明白了。 frm.SelTop = x 将设置选定的记录,从那里我可以设置背景或字体样式等。非常酷。对2009年进行简单测试并设置所选记录:
I have finally got it. The frm.SelTop = x will set the selected record and from there I can set the background or font style, etc.. Very cool. A simple test for 2009 and setting the selected record:
为了让我从连续表单的底部获取电子邮件,我使用了这个更简单的代码(因为我避免了 RecordsetClone 代码)
In order for me to get the email off the bottom of my continuous form, I used this much simpler code (as I avoided the RecordsetClone code)