首页 > 解决方案 > 从现有记录创建新记录,更改 Access dB 中的两个字段

问题描述

我在 Access 中有一个“多个项目”表单,它向用户显示与所选案例相关的 1 到 n 个现有指控记录的集合。该表格基于显示来自三个基础表的链接信息的查询,“案例”表中的每个指控都有一条记录(应该是两个表,一个用于案件,一个用于指控,但它是几年前开发的,并且我无法改变它)。目前,我可以选择任何记录并打开一个包含该记录的表单来编辑案例信息。完成后,另一个例程会使用适当的信息更新所有 n 项指控(因此所有指控都具有相同的案件级别数据)。这一切都有效。

我希望能够添加额外的指控或删除额外的指控,最好是从同一个表单中,以使用户保持简单。

要添加指控,我想复制第一个指控记录并创建一个新记录。我假设基础查询使这变得复杂,因为它具有来自多个链接表的信息。因此,理想情况下,我会复制第一条记录的唯一 ID,并使用它来复制具有新唯一 ID 和新指控编号的基础表(案例)中的记录(由大约 50 个字段组成)。我需要唯一的密钥来自动递增,新的指控编号将从 Case# 和用户提供的字母(下面的 TempVars!tV_newAllegation)构建。案件编号(19001、19002...)和指控字母,因此完整的指控编号为 19001A、19001B、19001C... 用户将提供他们想要附加的字母(通过文本框),例如“E”,以及它将与 case# 结合起来,为记录创建一个新的指控#。

我对 [code here](MSAccess - Requery a Subform After Insert? > 用于添加到我的 Multiple Items 表单页脚的按钮的 onClick() 进行了轻微修改。当我运行它时,我得到“运行时错误“3027”:无法更新。数据库或对象是只读的。”调试屏幕显示“.AddNew”调用突出显示。我不知道是什么创建了只读锁。

我的代码:

Private Sub cmdInsert_Click()

Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld       As DAO.Field
Dim tV_newAllegation As TempVars

    'Create new allegation number.
TempVars!tV_newAllegation = [TempVars]![tV_selectIncidNum] & [Forms]![1frmAllegationChooseList]![txtAllegLetter]
    ' Begin copy and update of record.

If Me.NewRecord = True Then Exit Sub

Set rstInsert = Me.RecordsetClone
Set rstSource = rstInsert.Clone
With rstSource
    If .RecordCount > 0 Then
    ' Go to the current record.
    .Bookmark = Me.Bookmark
    With rstInsert
        .AddNew
            For Each fld In rstSource.Fields
              With fld
                If .Attributes And dbAutoIncrField Then
                    ' Skip Autonumber or GUID field.
                ElseIf .Name = "AltIntCaseNumber" Then
                    rstInsert.Fields(.Name).Value = TempVars!tV_newAllegation
                    ' Add other ElseIf statements for any other field needing adjustment.
                Else
                    ' Copy field content.
                    rstInsert.Fields(.Name).Value = .Value
                End If
              End With
          Next
        .Update
        'Go to the new record and sync form.
        .MoveLast
        Me.Bookmark = .Bookmark
        .Close
      End With
    End If
    .Close
End With

Set rstInsert = Nothing
Set rstSource = Nothing

End Sub

标签: vbams-access

解决方案


推荐阅读