vba Access 2010 循环遍历记录集并更改子表单字段(如果为空)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15397981/
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
Access 2010 loop through recordset and change subform field if blank
提问by Scott Whittaker
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. 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.
我在主窗体上有一个命令按钮,单击该按钮时,在子窗体的显示记录上的字段中运行循环,并更改该字段中所有数据的值以匹配主窗体上未绑定组合框的值。这样用户就可以选择一次更新子表单上的多个记录。该代码工作正常,但如果可以将其更改为仅在每条记录为空白的情况下更新该字段,则会更有用。换句话说,我希望它检查每条记录是否有该字段的空白,然后根据组合框填充它,如果它不是空白或为空,则跳到下一条记录。这就是代码现在的样子。我不太擅长访问 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
回答by Fionnuala
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.
我建议您编辑您的表格并通过将文本数据类型的允许零长度属性设置为否来确保它不接受零长度字符串。
回答by Nick.McDermaid
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