Question : Access 2003 - Issue with .AddNew keywork after Access Migration to SQL Sever

Hi All,

I am getting the following error after migrating my back end databases to SQL Sever. The errors is as follows:
Runtime error '-21474259 (800004005)'
Invalid Operation
Has anyone got any idea, what I am trying to do is if the query doesn't have the record I want to add it to the table as a new record.

Thanks again in advance for all the help

Private Sub UpdateModelDim(field As String, val As Double, dimtype As Integer)
    'Dim rs As DAO.RecordSet
    Dim query As String
    Dim cn As ADODB.Connection
    Dim rs As New ADODB.RecordSet
   
    query = "SELECT crpProductModel_crpProductID, crpProductModel_DimType, crpProductModel_Model, crpProductModel_WidthDiam, crpProductModel_Height" _
            & " FROM crpProductModels" _
            & " WHERE crpProductModel_crpProductID=" & Me.AppProductId & " AND crpProductModel_DimType=" & dimtype & ";"
 
    'Set rs = CurrentDb.OpenRecordset(query)
    Set cn = CurrentProject.Connection
    rs.Open query, cn, adOpenDynamic, adLockPessimistic
    If rs.RecordCount = 0 Or rs.RecordCount = -1 Then
        rs.AddNew      'ERROR OCCURS HERE
        rs("crpProductModel_crpProductID").Value = Me.AppProductId
        rs("crpProductModel_DimType").Value = dimtype
        If dimtype = 1 Then
            rs("crpProductModel_Model").Value = Me.txtModel & " (min size)"
        Else
            rs("crpProductModel_Model").Value = Me.txtModel & " (max size)"
        End If
    Else
        rs.MoveFirst
        'rs.Edit
    End If
    If val < 0 Then
        rs(field).Value = Null
    Else
        rs(field).Value = val
    End If
    rs.Update
    rs.Close
    Set rs = Nothing
End Sub

Answer : Access 2003 - Issue with .AddNew keywork after Access Migration to SQL Sever

Try it as:

With rs
         .Add
        !crpProductModel_crpProductID = Me.AppProductId
        !crpProductModel_DimType = dimtype
        If dimtype = 1 Then
            crpProductModel_Model = Me.txtModel & " (min size)"
        Else
            !crpProductModel_Model = Me.txtModel & " (max size)"
         .Update
End With
Random Solutions  
 
programming4us programming4us