I have a ‘Multiple Items’ form in Access which shows the user the set of 1 to n existing allegation records related to a selected case. The form is based on a query showing linked information from three underlying tables, with one record for each allegation in the “Cases” table (which should have been two tables, one for cases, one for allegations, but it was developed years ago and I can’t change that). Currently, I can then select any record and bring up a form with that record for editing Case information. Once that is done another routine updates all n allegations with the appropriate info (so all allegations have the same case level data). All this works.
I want to be able to add additional allegations or delete extra ones, ideally from this same form to keep it simple for users.
To add allegations, I would like to copy the first allegation record and create a new record. I’m assuming the underlying query complicates this since it has info from multiple linked tables. Thus ideally I’d copy the unique ID for the first record, and use that to copy the record (consisting of approximately 50 fields) in the underlying table (Cases) with a new unique ID and a new allegation number. I need the unique key to automatically increment and the new allegation numbers will be built from Case# and user supplied letter (TempVars!tV_newAllegation below). Cases are numbered (19001, 19002…) and allegations are lettered, thus the full allegation number is 19001A, 19001B, 19001C… The user will supply the letter they want to append (via a text box), e.g., “E”, and it will be combined with the case# to create a new allegation# for the record.
I used a slight modification of the [code here](MSAccess - Requery a Subform After Insert?> for the onClick() of a button added to the footer of my Multiple Items form. When I run it I get "Run-time error '3027': Cannot update. Database or object is read-only." The debug screen shows the ".AddNew" call highlighted. I cannot figure out what is creating the read-only lock.
My code:
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