简体   繁体   English

每个表单记录的VBA / Access RecordSet问题

[英]VBA/Access RecordSet per form record problem

I'm new to VBA and Access in general and ran into this problem whilst trying to use the proposed alternate implementation from another question I'd asked ( DLookup in Access not running until textBox clicked on in Form ) 我是VBA和Access的新手,在尝试使用我提出的另一个问题( 在Access中的DLookup直到在Form上单击textBox之前不运行 )中尝试使用提议的替代实现时遇到了此问题

The code below runs, the issue is that Me.Key is different for each record being displayed in the form and running this in the form open event means it grabs only the first value assigned to Me.Key from the first record. 下面的代码运行,问题在于Me.Key对于表单中显示的每个记录都是不同的,并且在表单打开事件中运行它意味着它仅从第一条记录中获取分配给Me.Key的第一个值。 How can i make this run so that Me.Key is different for each record/line being displayed? 我该如何运行,以使Me.Key对于正在显示的每个记录/行都是不同的?

Dim rs As DAO.Recordset
Dim db As Database
Dim qdf As QueryDef
Dim prm As Parameter

Set db = CurrentDb
Set qdf = db.QueryDefs("[MF INCOME - STREAM MONTHLY]")
For Each prm In qdf.Parameters
    prm.Value = Eval(prm.Name)
Next prm

Set rs = qdf.OpenRecordset(dbOpenDynaset)
rs.FindFirst "[MyMonth]=10 AND [Org_Type]='" & Me.Key & "'"
Me.Oct = rs!SumVal
'...other month assignments

您可以按照前面的建议尝试表单的当前事件:)

I guess the Me.Key refers to a control located in the details section of your form. 我猜Me.Key指的是位于表单详细信息部分中的控件。 In this case, and in order to list all values taken by the control, you will need to browse all the records. 在这种情况下,为了列出控件使用的所有值,您将需要浏览所有记录。 One of the ways to do so can be: 这样做的方法之一可以是:

Dim m_position as Long
for m_position = 1 to Me.recordset.recordcount
   me.seltop = m_position
   debug.print me.key
next m_position

Unfortunately your will see your screen blincker while browsing all the lines. 不幸的是,浏览所有行时,您都会看到屏幕闪烁。 You can off course find some 'screenFreezer' utilities for VBA on the net (there is one called LockWindowUpdate, as long as I can remember). 当然,您可以在网上找到一些VBA的“ screenFreezer”实用程序(据我所知,有一个名为LockWindowUpdate的实用程序)。

Another solution is to browse the clone of the underlying recordset (browsing the recordset will provoke the same screen behaviour as before). 另一个解决方案是浏览基础记录集的克隆(浏览记录集将引起与以前相同的屏幕行为)。 Supposing that the Me.Key control is bound to the "Key" column of the recordset, code could be: 假设Me.Key控件绑定到记录集的“ Key”列,则代码可以是:

Dim rsClone as DAO.recordset
set rsClone = Me.recordsetclone
if rsClone.EOF and rsClone.BOF then
Else
    rsClone.moveFirst
    Do while not rsClone.EOF
        debug.print rsCLone.fields("Key")
        rsClone.moveNext
    Loop
Endif
set rsClone = nothing

My favorite is the first one, with the "freeze"option added. 我最喜欢的是第一个,添加了“冻结”选项。 Your code can manage the seltop and selheight values of the form. 您的代码可以管理表单的seltop和selheight值。 This means you can browse specifically records selected by users and/or, once all records browsed, go back to the original record selection. 这意味着您可以专门浏览用户选择的记录,和/或浏览所有记录后,返回到原始记录选择。

EDIT: 编辑:

Following @Ben's comment, I shall add that if your "myControl" control is in the details section and is unbound, you then will not be able to manage one value per row. 在@Ben评论之后,我要补充一点,如果您的“ myControl”控件在详细信息部分中并且未绑定,则您将无法管理每行一个值。 The control will have the same value for all lines when the form is displayed as "continuous". 当窗体显示为“连续”时,控件的所有行的值均相同。

If your "myControl" control is bound to the "myField" field of a recordset, any of the following codes will increment "myControl" control value and "myField" field value at the same time. 如果您的“ myControl”控件绑定到记录集的“ myField”字段,则以下任何代码将同时增加“ myControl”控件值和“ myField”字段值。 You will be than able to have a different value on each row: 您将能够在每一行上具有不同的值:

Solution 1: 解决方案1:

Dim m_position as Long
for m_position = 1 to Me.recordset.recordcount
   me.seltop = m_position
   me.controls("myControl") = m_position
next m_position

Solution 2: 解决方案2:

Dim rsClone as DAO.recordset, _
    i as long

set rsClone = Me.recordsetclone
if rsClone.EOF and rsClone.BOF then
Else
    rsClone.moveFirst
    i = 1
    Do while not rsClone.EOF
        rsClone.fields("myField") = i
        rsClone.update
        rsClone.moveNext
        i = i+1
    Loop
Endif
set rsClone = nothing

It is not at all clear what you need the parameters for in that query. 根本不清楚在该查询中需要什么参数。 I would suggest that you simply build a query without ANY parameters that includes all of the columns you need. 我建议您仅构建一个不包含任何需要的所有列的任何参数的查询。

Then build small form based on this query. 然后根据此查询构建小型表单。 You can then drop this small form into your existing form and display the several fields of data based on the key value setting. 然后,您可以将此小型表单放到现有表单中,并根据键值设置显示几个数据字段。 (just ensure that you setup the link master and child settings for the sub-form). (只需确保为子表单设置了链接主设置和子设置)。 Here what a form looks like: 表单如下所示:

替代文字
(source: shaw.ca ) (来源: shaw.ca

So, in the above the bill to displays the customer info based on customer id and is a related table to in invoice. 因此,在上面的帐单中根据客户ID显示客户信息,并且是与发票相关的表。

In other words to display several fields of data based on a key value from another table you don't need to write one line of code. 换句话说,根据另一个表中的键值显示多个数据字段,您无需编写任何代码。 So this whole process and goal of yours can be done by drag and drop with the mouse. 因此,您的整个过程和目标都可以通过鼠标拖放来完成。 I often have something like a invoice, or purchase order and I ONLY have the customer ID. 我经常有发票或采购单之类的东西,而且只有客户ID。 By using a sub-form I can display the whole address and several fields of data without writing any code. 通过使用子窗体,我可以显示整个地址和多个数据字段,而无需编写任何代码。 As you move from record to record, this whole set of fields will update and always display the correct related data. 当您从一个记录移动到另一个记录时,这整个字段集将更新并始终显示正确的相关数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM