简体   繁体   English

VBA while循环不起作用-访问表格

[英]vba while loop not working - access form

I have a main form with a subform below it. 我有一个主窗体,下面有一个子窗体。 The main form has a "overwrite" option where I can type in a value within txtComments and this will then input a value in the subform.comments field only if its check box has a true value (eg. its checked) 主窗体具有“覆盖”选项,在该选项中,我可以在txtComments中键入一个值,然后仅在其复选框具有真实值(例如,已选中)的情况下,才在subform.comments字段中输入一个值。

As it stands I'm using this code which works only for the select row of data not the entire subform dataset: 就目前而言,我正在使用此代码,该代码仅适用于选定的数据行,而不适用于整个子表单数据集:

(note: I've simplified the names of the fields and values) (注意:我已经简化了字段和值的名称)

Private Sub Command118_Click()

    Dim rst As Recordset, i As Integer

    Set rst = Subform.RecordsetClone
    i = 0
    rst.MoveFirst
    Do While Not rst.EOF
        i = i + 1
        rst.Edit

        If [SubformCheckbox] = True Then
            [SubformComments] = [txtComments]
        Else
            [SubformComments] = 0
        End If

        rst.Update
        rst.MoveNext
    Loop

    rst.Close
    Set rst = Nothing
End Sub

You used wrong variables. 您使用了错误的变量。 Try this: 尝试这个:

    If rst![SubformCheckbox] = True Then
        rst![SubformComments] = [txtComments]
    Else
        rst![SubformComments] = 0
    End If

If you want to do not touch not checked records, change the loop like this: 如果您不想触摸未检查的记录,请更改循环,如下所示:

With rst
    Do While Not .EOF
        If ![SubformCheckbox] = True Then
            .Edit
            ![SubformComments] = [txtComments]
            .Update
        End If
        .MoveNext
    Loop
End With

For performance improvement you can filter the recordset leaving checked rows only, then update all of them in the loop, but best performance will be reached if you'll use UPDATE SQL query for updating the table directly, without using recordset at all. 为了提高性能,您可以过滤记录集,仅保留选中的行,然后在循环中更新所有记录,但是如果您直接使用UPDATE SQL查询直接更新表而不使用记录集,则将达到最佳性能。

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

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