vbasharepoint

VBA to Sharepoint


Could someone help me please. I keep on getting this error

Error: Cannot update 'registration'; field not updateable.

Private Sub Test()
    Dim conn As Object
    Dim url As String
    Dim table As String
    Dim cmd As Object
    Dim vehicle As ClsVehicle
    
    On Error GoTo errHandler
    Set conn = CreateObject("ADODB.Connection")
    Set cmd = CreateObject("ADODB.Command")
    
    url = "https://tenant.sharepoint.com/sites/Test/"
    table = "934798bc-046e-4bb8-a608-913c86a5fdbd"
    With conn
        .connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes;" & _
                            "DATABASE=" & url & _
                            ";LIST={" & table & "};"
        .Open
    End With
    
    Set vehicle = New ClsVehicle
    With vehicle
        .setRegistration = "JS50LKGP"
        .setPalletsCapacity = 0
        .setTareWeight = 980
        .setOwnerId = 1
    End With
    
    With cmd
        .ActiveConnection = conn
        .CommandText = "INSERT INTO [" & table & "] (registration, pallets_capacity, tare_weight, owner_id) " & _
                       "VALUES (?, ?, ?, ?)"
        .CommandType = 1
        .Parameters.Append .CreateParameter(, 200, 1, 255, vehicle.getRegistration)
        .Parameters.Append .CreateParameter(, 3, 1, 0, vehicle.getPalletsCapacity)
        .Parameters.Append .CreateParameter(, 5, 1, 0, vehicle.getTareWeight)
        .Parameters.Append .CreateParameter(, 3, 1, 0, vehicle.getOwnerId)

        .Execute
    End With
errHandler:
    Debug.Print "Error: " & Err.Description
End Sub

Solution

  • You need IMEX=0 for writes, according to https://www.connectionstrings.com/sharepoint/#:~:text=Use%20different%20SharePoint%20list%20connections%20for%20read%20(IMEX%3D2)%20and%20writes%20(IMEX%3D0)%2C%20do%20not%20mix%20them