将DAO转换为ADO

时间:2013-11-15 21:27:32

标签: access-vba ms-access-2013

我正在尝试创建一个函数,它将检查表单中的所有复选框 数据表子表格。 自从我们迁移到Office 2013后,此代码停止工作,似乎转移到ADO是唯一的方法。

Private Sub Toggle_Click()
    Dim sfrm
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    sfrm = Me.Subform
    Set cn = CurrentProject.Connection
    'Dim rs As DAO.Recordset
    'Set rs = Me.Subform.Form.RecordsetClone
  rs.Open Me.Recordset
    If (theTop = 0) Then
    Else
        Me.Subform.Form.AllowAdditions = False
        Dim i As Integer
        If (theHeight = 0) Then
            If (sfrm![Ready?] = -1) Then
                sfrm![Ready?] = 0
                sfrm![Timestamp] = Null
            Else
                sfrm![Ready?] = -1
                sfrm![Timestamp] = Date
            End If
        Else
            On Error Resume Next
            For i = theTop To rs.RecordCount
                'If (i = 1) Then
                'Else

                'End If
                'rs.MoveLast
             'Do While Not rs.BOF
                If (sfrm![Ready?] = -1) Then
                    sfrm![Ready?] = 0
                    sfrm![Timestamp] = Null
                Else
                    sfrm![Ready?] = -1
                    sfrm![Timestamp] = Date
                End If

                 'Me.Subform.Form.Recordset.MoveNext
                 rs.MoveNext
                 'Debug.Print sfrm![Routing Number]
               'Loop
            Next
        End If
        Me.Subform.Form.AllowAdditions = True
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

  

似乎转向ADO是唯一的方法

使用Access 2013,DAO仍然是从Access应用程序本身内部与Access数据库对象进行交互的首选方式。在我看来,在开始将DAO代码批量转换为ADO之前,最好先研究为什么以前的DAO代码失败(以及如何修复它)。

我刚刚在Access 2013中进行了测试。使用[ParentTable}

ParentTable.png

[ChildTable]

ChildTable.png

和这样的表格

enter image description here

我可以使用以下代码切换当前子窗体记录的[Selected?]状态:

Option Compare Database
Option Explicit

Private Sub cmdToggleChildren_Click()
    Dim rst As DAO.Recordset
    Set rst = Me.ChildSubform.Form.Recordset
    If Not (rst.BOF And rst.EOF) Then
        rst.MoveFirst
        Do Until rst.EOF
            rst.Edit
            rst![Selected?] = Not rst![Selected?]
            rst.Update
            rst.MoveNext
        Loop
    End If
    Set rst = Nothing
End Sub