vba 如何引用连续子窗体控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4513929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:27:21  来源:igfitidea点击:

How To Refer To Continuous Subform Contol

ms-accessvbaaccess-vbams-access-2003

提问by Taptronic

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.

我有一个 Access 2003 表单,其中一个子表单设置为连续表单,位于子表单控件中。对于主表中的一条记录,子表中会出现一对多的记录。数据显示正常。

The main form is named Widgetsand the sub form is named Transactions. There are 5 textbox controls that display the data in the subform. The one in question is ReceiptDate.

主窗体名为Widgets,子窗体名为Transactions。有 5 个文本框控件可以显示子窗体中的数据。有问题的是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..

我想要做的是查看这些值并确定是否有 2009 年的收据,如果有,则将该行的背景更改为黄色,以便在用户遇到该情况时突出显示。甚至可能将日期字段的字体更改为粗体。

I tried many ways of referencing the subform's controls. When I have tried Me.Transactions.ReceiptDateI 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.

我尝试了多种引用子窗体控件的方法。当我尝试时,Me.Transactions.ReceiptDate我只收到了该子表单中的第一条记录。我希望能够遍历它们并查看是否满足条件。我试着Me.Transactions.ReceiptDate(1)Me.Transactions.ReceiptDate(0)等等。

I tried the For Each ctl In Form.Controlsroute 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".

我也试过这For Each ctl In Form.Controls条路线。它运行了几次迭代,然后我收到了一个运行时错误 2455“您输入了一个对属性表单/报告具有无效引用的表达式”。

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.

有没有办法在子表单中引用特定的“行”并根据找到的值做一些事情?另外,我在 On Current 事件中执行此操作,因为我不知道将代码放在哪里。子窗体在父窗体之前加载,因此这些控件甚至可能还没有完全“在那里”,但是当我在立即窗口中尝试它时,我确实得到了第一行的日期。

UPDATE 12.23.2010:

2010 年 23 月 12 日更新:

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:

在@Remou 的慷慨帮助下,我能够从 RecordSet 中调试.打印 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

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.

所以现在我能够知道我的子表单中哪一行有 2009 年的收据,我需要能够在我在 for 循环中遇到它们时突出显示整行或多行。如何引用实际行?数据表视图或连续表单视图 - 我都尝试过。

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.

条件格式很好,但它只允许我突出显示一个特定的记录,我更愿意通过 VBA 来做到这一点,因为......从这里我想给用户点击任何记录的能力在子表单中并获取收据详细信息并可能打印它们。

Any ideas?

有任何想法吗?

采纳答案by Fionnuala

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:

要引用子表单上的控件,请按名称引用子表单控件,然后是表单属性以获取包含的表单,然后按名称引用控件:

Me.MySubformControlName.Form.MyControl

See: http://www.mvps.org/access/forms/frm0031.htm

请参阅:http: //www.mvps.org/access/forms/frm0031.htm

回答by Taptronic

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:

我终于明白了。frm.SelTop = x 将设置选定的记录,然后我可以设置背景或字体样式等。非常酷。2009 年的简单测试并设置所选记录:

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 0

' Enumerate the list of selected records presenting
' the CompanyName field in a message box.
For i = 1 To rs.RecordCount
    If Year(rs![ReceiptDate]) = 2009 Then
        frm.SelTop = i '<-----------------------------
    End If
    rs.MoveNext
Next i

回答by Norman

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)

为了让我从连续表单的底部获取电子邮件,我使用了这个更简单的代码(因为我避免了 RecordsetClone 代码)

    Me.[email subform].Form.SelTop = Me.[email subform].Form.Count 'selects the last row
    str = Me.[email subform].Form.Email 'capture the value of the last row
    MsgBox str