首页 > 解决方案 > MS Access VBA Me.Recordset.Findfirst 运行时错误

问题描述

我的一个 vba 事件有问题。

首先,在字段发生更改后触发事件,该字段属于显示来自一个表的多个数据条目的表单,该表单位于 NavigationPane 内。

我的数据库是在 access 2016 32bit 中编程的,主要由我们的工作人员在具有 Access Runtime 2013 的服务器上使用

现在,当我在我的电脑上触发事件时,一切都按预期工作,但在服务器运行时版本上,它向我显示一个错误,我将尝试将其翻译成英文,因为我在德国:“该程序的执行无法继续,由于运行时错误” - “此程序无法继续运行并将被关闭”。

我已经尝试过查看鸭子,但找不到我需要的东西,主要是因为关键字“运行时”在这种情况下有两种不同的含义。

这是代码:

Private Sub isEnd_AfterUpdate()
Dim isEnd As Date
Dim rs As Recordset
Dim strFind As String
Dim test As Variant

'On Error Resume Next

Set rs = Recordset

With rs
If Not .EOF Then
        isEnd = Me!isEnd
    If Me!isStart > 0 Then
        Me!isDur = (Me!isEnd - Me!isStart) * 24
    End If

    strFind = "IdMa = '" & Me!IdMa & "' and OrderRow = " & Me!OrderRow + 1
    'test = MsgBox(strFind & " - " & rs.Name & " - '" & isEnd & "'", vbOKOnly, "test")
    Me.Recordset.FindFirst (strFind)
    Me!isStart = isEnd
    Me.isEnd.SetFocus
End If
End With
End Sub

您可能会忽略我尝试用于调试的错误处理和 MsgBox。

数据库用于规划我们的流程,该字段保留完成流程的结束时间,并将其输入到下一个流程的开始时间,以节省一点时间。

Edit1:我应该提到我将错误精确到了这一行:

Me.Recordset.Findfirst (strFind)

但无法弄清楚为什么这是一个问题,完全相同的行在同一表单上的某些按钮中使用得很好。

标签: ms-accessvbams-access-2013ms-access-2016

解决方案


尝试使用 RecordsetClone 并且不要重复控件名称:

Private Sub isEnd_AfterUpdate()

    Dim isEndDate As Date
    Dim rs As DAO.Recordset
    Dim strFind As String
    Dim test As Variant

    'On Error Resume Next

    Set rs = Me.RecordsetClone

    With rs
        If Not .EOF Then
            isEndDate = Me!isEnd
            If Me!isStart > 0 Then
                Me!isDur = (Me!isEnd - Me!isStart) * 24
            End If

            strFind = "IdMa = '" & Me!IdMa & "' and OrderRow = " & Me!OrderRow + 1
           'test = MsgBox(strFind & " - " & .Name & " - '" & isEnd & "'", vbOKOnly, "test")
            .FindFirst strFind
            ' and use the found record for something ..?

            Me!isStart.Value = isEndDate
            Me!isEnd.SetFocus
        End If
        .Close
    End With

End Sub

推荐阅读