MS Access - 添加记录时出现 #DELETED

MS Access - 添加记录时出现 #DELETED

我正在使用连接到 SQL Server 2016 数据库的 MS Access 2010 .accdb 数据库。我的 SQL 表和视图在 MS Access 中作为链接表进行管理。我正在使用无 DSN 连接和 SQL Server Native Client 11.0 驱动程序(不知道这是否重要)。

我有一个查看/搜索表单,可让用户查看数据库中的记录。该表单有一个“编辑此记录”按钮和一个“添加新记录”按钮。

我有一个使用可更新 SQL Server 查询的编辑/创建表单。此表单有许多必填组合框字段和一个可选组合框字段。该表单还有许多其他字段 - 文本和日期。

查看/搜索表单上的添加新记录按钮的代码如下;

DoCmd.OpenForm "Application Edit - Template", acNormal, "", "", acAdd, acDialog

编辑/创建表单有一个保存并关闭按钮,其代码如下;

Private Sub btnSaveClose_Click()
On Error GoTo btnSaveClose_Click_Err

' Save the edits to the form i.e. the main form
'DoCmd.RunCommand acCmdSaveRecord

If Me.Dirty Then
    Me.Dirty = False
End If

'Close the form

DoCmd.Close acForm, "Application Edit - Template"

btnSaveClose_Click_Exit:
Exit Sub

btnSaveClose_Click_Err:
MsgBox Error$
Resume btnSaveClose_Click_Exit

End Sub

为了在查看/搜索表单中显示新创建的记录,已将以下代码添加到编辑/创建表单的 After_Update 事件中。

Private Sub Form_AfterUpdate()
Dim rst As DAO.Recordset

'Refresh the record to get the latest data that has been saved
'note that this is primarily to refresh the record details
'i.e. last updated by and Last update date time.

Me.Refresh

'If it's a new record...
If boolNewRecord Then
    'Refresh the browse form so that the newly saved record is in the recordset
    'We set boolBypassFormCurrent = True so we can bypass the redundant
    'calls to the Form_Current event on the view/search form

    boolBypassFormCurrent = True
    [Forms]![Application Browse - Template].Requery

    'Navigate the browse form to the newly saved record
    Set rst = Forms("Application Browse - Template").RecordsetClone
    With rst
        .FindFirst "[DB_Key]  = " & Me.txtDBKey  <=== Failure Here!
        If Not .NoMatch Then
            Forms("Application Browse - Template").Bookmark = .Bookmark
        End If
    End With
    Set rst = Nothing
End If
boolNewRecord = False
boolBypassFormCurrent = False

btnSaveClose.Enabled = False

End Sub

一旦显示编辑/创建表单,如果用户为所有组合框(包括可选组合框)提供值并保存记录,则一切都会按预期工作(记录被保存,查看/搜索表单被更新以显示新记录,并且编辑/创建表单被关闭)。如果可选组合框留空并且记录被保存,则一切都会按预期工作。如果最初为可选组合框提供了一个值,然后在保存之前清除可选值,然后保存记录,则会引发以下错误;

运行时错误:表达式中出现“3077”语法错误(缺少运算符)。

调试器停止在该行;

.FindFirst "[DB_Key]  = " & Me.txtDBKey

在表单中,所有非组合框的字段都显示以下值 -#已删除

在立即窗口中输入?Me.txtDBKey 会发现它的值是一个空字符串,即空白(非空)。

我相信因为 Me.txtDBKey 是空的所以代码实际上被解释为;

rst.FindFirst [DB_Key] =

这可以解释错误信息。

当调试器终止并且编辑/创建表单关闭时 - 将控制权返回到查看/搜索表单,将显示用户尝试添加的记录!

就好像保存的记录以某种方式飞走了从保存记录时的编辑/创建表单 - 在 After_Update 事件之前,仅在设置然后取消设置可选组合框的情况下。

有人能解释一下这种非常不友好的行为吗?有人有什么办法可以消除它吗?

值得注意的是,如果用户编辑现有记录(在视图/搜索表单上选择编辑此记录按钮)并从可选组合框中删除该值并保存记录,则一切都会按预期工作。

我对此错误进行了大量搜索,但未能找到与我的问题相匹配的内容。SQL 视图下方的表有一个整数主键和一个 RowVersion 列,它们都包含在视图中。MS Access 检测到主键并正确识别 RowVersion 列。

在此先感谢您对这个非常棘手的问题所提供的帮助。

答案1

您是否不能简单地测试是否Me.txtDBKey为空或空字符串并据此进行分支?

例如:

If IsNull(Me.txtDBKey) Or Me.txtDBKey = vbNullString Then
    ' Notify user?
Else
    'Navigate the browse form to the newly saved record
    Set rst = Forms("Application Browse - Template").RecordsetClone
    With rst
        .FindFirst "[DB_Key]  = " & Me.txtDBKey
        If Not .NoMatch Then
            Forms("Application Browse - Template").Bookmark = .Bookmark
        End If
    End With
    Set rst = Nothing
End If

相关内容