在 Access VBA 中将第二个嵌套子表单复选框设置为 true
Posted
技术标签:
【中文标题】在 Access VBA 中将第二个嵌套子表单复选框设置为 true【英文标题】:Set second nested subform check box to true in Access VBA 【发布时间】:2021-10-09 17:33:12 【问题描述】:我有一个包含工单的表单,PK=OrderID。具有 OrderDetails 的子表单,PK=OrderDetailsID。该子表单上的一个子表单具有 OrderDetailAccessories,PK=OrderAccID。
当我将主窗体上的复选框设置为 True 时,我希望子窗体 OrderDetails 的所有复选框都更改为 True,并将 CompDate 设置为今天的日期,并将其子窗体 OrderDetailsAccessories 的所有复选框更改为 True并将 CompDate 设置为今天的日期。
在我的代码中,记录集 rs 返回预期的记录,但我得到 rs2 的空记录集。我单步执行了代码,然后 orddetid 返回了正确的值。
Private Sub IsComplete_AfterUpdate()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim strItemComp As String
Dim strAccComp As String
Dim ordid As Long
Dim orddetid As Long
ordid = Me.txtOrdID
strItemComp = "SELECT OrderDetailID, IsComplete, CompDate FROM tblOrderDetails WHERE OrderID = " & ordid
strAccComp = "SELECT OrderAccID, IsComplete, CompDate FROM tblOrderAcc WHERE OrderDetailID = " & orddetid
Set db = CurrentDb
Set rs = db.OpenRecordset(strItemComp)
If Me.IsComplete = True Then
If MsgBox("Marking main order complete will mark ALL items and accessories for this Order as complete!", vbYesNo, "Are you sure?") = vbYes Then
Me!txtCompletionDate = Date
rs.MoveFirst
Do Until rs.EOF
If rs!IsComplete = False Then
rs.Edit
rs!IsComplete = True
rs!CompDate = Date
rs.Update
End If
orddetid = rs.Fields("OrderDetailID").Value
Debug.Print orddetid
Set rs2 = db.OpenRecordset(strAccComp)
If rs2.RecordCount > 0 Then
rs2.MoveFirst
Do Until rs2.EOF
If rs2!IsComplete = False Then
rs2.Edit
rs2!IsComplete = True
rs2!CompDate = Date
rs2.Update
End If
rs2.MoveNext
Loop
End If
rs.MoveNext
Loop
Me.Dirty = False
Exit Sub
Else
Me.Undo
End If
Else
Me.txtCompletionDate = Null
Exit Sub
End If
Me.Dirty = False
End Sub
【问题讨论】:
【参考方案1】:您必须为每条记录更新strAccComp
,例如:
If Me.IsComplete = True Then
If MsgBox("Marking main order complete will mark ALL items and accessories for this Order as complete!", vbYesNo, "Are you sure?") = vbYes Then
Me!txtCompletionDate = Date
Me.Dirty = False
Set rs = Me!NameOfSubformControl.Form.RecordsetClone
rs.MoveFirst
Do Until rs.EOF
If rs!IsComplete = False Then
rs.Edit
rs!IsComplete = True
rs!CompDate = Date
rs.Update
End If
strAccComp = "SELECT OrderAccID, IsComplete, CompDate FROM tblOrderAcc WHERE OrderDetailID = " & rs!orddetid.Value
Set rs2 = CurrentDb.OpenRecordset(strAccComp)
If rs2.RecordCount > 0 Then
rs2.MoveFirst
Do Until rs2.EOF
If rs2!IsComplete = False Then
rs2.Edit
rs2!IsComplete = True
rs2!CompDate = Date
rs2.Update
End If
rs2.MoveNext
Loop
End If
rs2.Close
rs.MoveNext
Loop
rs.Close
End If
End If
【讨论】:
以上是关于在 Access VBA 中将第二个嵌套子表单复选框设置为 true的主要内容,如果未能解决你的问题,请参考以下文章