AddNew 函数 - 如果链接到 SQL 后端,我会收到运行时错误“3219”无效操作

问题描述 投票:0回答:1

我继承了非常旧的访问数据库。 多年前就从access后端表转换为sql后端表,再次继承工作。 用户报告说,当他们想要向列表添加新记录时,会出现运行时错误“3219”无效操作。 当我测试它时,我发现只有当后端表链接到 SQL 时才会发生这种情况。 如果我有与本地表相同的数据库,它会毫无问题地添加新记录。 这就是代码,也是它失败的地方。

我期待它向名为 People 的表添加一个名称

Private Sub Driver_ID_NotInList(NewData As String, Response As Integer)
Dim ctl As Control
Dim PeopleRecs, PeopStat
Dim FirstName, LastName As String
Dim Tmp_Space As Integer

    ' Return Control object that points to combo box.
    Set ctl = Me![Driver ID]
    ' Prompt user to verify they wish to add new value.
    If MsgBox("'" & NewData & "' is not in the list. Do you want to Add it?", vbOKCancel) = vbOK Then
        ' Set Response argument to indicate that data is being added.
        Response = acDataErrAdded
        ' Add string in NewData argument to row source.
        Tmp_Space = InStr(1, NewData, " ")
        If Tmp_Space > 0 Then
            FirstName = Left(NewData, Tmp_Space - 1)
            LastName = Mid(NewData, Tmp_Space + 1, Len(NewData) - Tmp_Space)
        Else
            FirstName = ""
            LastName = NewData
        End If
        sqlstr = "SELECT [Person ID],[First name],[Last Name] from [People]"
        Set PeopleRecs = Currentdb.OpenRecordset(sqlstr, dbOpenDynaset, dbSeeChanges)
        sqlstr = "SELECT [Person ID],[Status] FROM [People Status]"
        Set PeopStat = Currentdb.OpenRecordset(sqlstr, dbOpenDynaset, dbSeeChanges)
        With PeopleRecs
            .AddNew
            ![Person ID] = NextID("Person")
            ![First Name] = FirstName
            ![Last Name] = LastName
           ** PeopStat.AddNew**
            PeopStat.[Person ID] = PeopleRecs.[Person ID]
            PeopStat.[Status] = "Car Driver"
            .Update
            .Close
            PeopStat.Update
            PeopStat.Close
        End With
    Else
    ' If user chooses Cancel, suppress error message and undo changes.
        Response = acDataErrContinue
        ctl.Undo
    End If

End Sub
sql ms-access
1个回答
0
投票

它可能不喜欢混合插入。尝试:

Dim NewID As Long
NewID = NextID("Person")

With PeopleRecs
    .AddNew
    ![Person ID] = NewID
    ![First Name] = FirstName
    ![Last Name] = LastName
    .Update
    .Close
End With
With PeopStat
    .AddNew
    ![Person ID] = NewID
    ![Status] = "Car Driver"
    .Update
    .Close
End With
© www.soinside.com 2019 - 2024. All rights reserved.