简体   繁体   English

访问2010遍历记录集并更改子表单字段(如果为空)

[英]Access 2010 loop through recordset and change subform field if blank

I have a command button on a mainform that when clicked runs a loop through a field on the displayed records of a subform and changes the value of all the data in that field to match the value of a unbound combobox on the mainform. 我在主窗体上有一个命令按钮,单击该按钮可在子窗体显示的记录中的一个字段之间循环,并更改该字段中所有数据的值以匹配该主窗体上未绑定的组合框的值。 It is so that users can have the option to update several records on the subform at once. 这样,用户可以选择一次更新子表单上的多个记录。 The code works fine but would be more usefull if it could be changed to only update the field if it is blank for each record. 该代码可以正常工作,但是如果可以将其更改为仅对每个记录为空白的字段进行更新,则将更加有用。 In other words I want it to check if each record has a blank for that field and then populate it based on the combo box and skip to the next record if it in not blank or null. 换句话说,我希望它检查每个记录是否在该字段中都有空白,然后根据组合框填充它,如果它不是空白或为null,则跳至下一条记录。 This is what the code looks like right now. 这就是现在的代码。 I'm not very good with access VBA and am not sure if I should use "Case" or "If" or how exactly to use it with the code below. 我对Access VBA不太满意,并且不确定是否应该使用“ Case”或“ If”或如何在下面的代码中正确使用它。

Private Sub comm_1_Click()
Dim rs As DAO.Recordset

Set rs = Me.Skid1.Form.RecordsetClone
With rs
    .MoveFirst
    Do While Not .EOF
        .Edit
        ![Release Code] = Me.code_updater.Value
         .Update
         .MoveNext
     Loop
 End With
 Set rs = Nothing

End Sub

I have tried this but it seemed to only update some of the blank records (very strange), I'm pretty sure its close but not quite. 我已经尝试过了,但是它似乎只更新了一些空白记录(非常奇怪),我敢肯定它已经关闭但还不是很清楚。

Private Sub comm_1_Click()
Dim rs As DAO.Recordset
Dim fld As Field

Set rs = Me.Skid1.Form.RecordsetClone
With rs
    .MoveFirst
    Do While Not .EOF
     For Each fld In .Fields

      If IsNull(fld.Value) Then
        .Edit
        ![Release Code] = Me.code_updater.Value
        .Update
         End If
        .MoveNext
          Next

    Loop
End With
Set rs = Nothing

End Sub

I suspect you want 我怀疑你想要

Private Sub comm_1_Click()
Dim rs As DAO.Recordset
Dim fld As Field

Set rs = Me.Skid1.Form.RecordsetClone
With rs
    .MoveFirst
    Do While Not .EOF
      ''Anything, space filled, null, ZLS
      If Trim(![Release Code] & "") = "" Then
        .Edit
        ![Release Code] = Me.code_updater.Value
        .Update
      End If
      .MoveNext
    Loop
End With
Set rs = Nothing

End Sub

I suggest you edit your table and make sure it will not accept zero-length strings by setting the Allow Zero Length property to No for text data types. 建议您编辑表,并通过将文本数据类型的“允许零长度”属性设置为“否”,确保它不接受零长度的字符串。

Does this make any difference? 这有什么区别吗? it checks for blank fields as well as NULL fields - yes they are two different things! 它检查空白字段和​​NULL字段-是的,它们是两件事!

Private Sub comm_1_Click()
Dim rs As DAO.Recordset
Dim fld As Field

Set rs = Me.Skid1.Form.RecordsetClone
With rs
.MoveFirst
Do While Not .EOF
 For Each fld In .Fields

  If (IsNull(fld.Value) Or fld.Value = "")Then
    .Edit
    ![Release Code] = Me.code_updater.Value
    .Update
     End If
    .MoveNext
      Next

Loop
End With
Set rs = Nothing

End Sub

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

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