vbasharepointado

ADO connection (in VBA) unable to INSERT to Sharepoint list


I'm using ADO because I want to write to a Sharepoint list using VBA in Excel. Right now I am getting "The Microsoft Access database engine could not find the object 'Isaac Test Excel To Sharepoint', and the code errs on the INSERT line. I suspect it is because of either my site reference being wrong, or my list ID being wrong.

I don't think my list ID is wrong, because I carefully followed the directions to extract the list ID from the URL that's exposed when you go to List Settings, carefully Replacing the 3 replaceable items as mentioned here: https://community.nintex.com/t5/Community-Blogs/Obtaining-a-list-id-in-SharePoint-2010-or-2013/ba-p/77664#:~:text=Navigate%20to%20the%20list%20and%20click%20List%20Settings.,Guid%20Format%20with%20URL%20encoding).

I am passing it in as:

strSharepointListID = "{3404D534–10CB–4F53–BB9D–37F5612155F1}"

I would like to have concluded, "the connection is correct because the code doesn't err until all the way to the INSERT statement", but unfortunately I've proved that to be false: If I pass in a totally non-existent Site value, gibberish, the code still doesn't err until all the way at the INSERT statement.

The name of my list is definitely Isaac Test Excel To Sharepoint

The site I am passing is like this, with me sanitizing this by replacing some text with "text": (I've tried all 3 of these):

  1. strSharepointSite = "https://text.text.text.com"
  2. strSharepointSite = "https://text.text.text.com/sites/text"
  3. strSharepointSite = "https://text.text.text.com/sites/text/_layouts/15/start.aspx#/"

Full code:

Sub Upd2KPIMember_SP()
    Dim cnt As ADODB.Connection
    Dim mySQL As String
    Dim strSharepointListID As String, strSharepointSite As String
    
    'https://community.nintex.com/t5/Community-Blogs/Obtaining-a-list-id-in-SharePoint-2010-or-2013/ba-p/77664#:~:text=Navigate%20to%20the%20list%20and%20click%20List%20Settings.,Guid%20Format%20with%20URL%20encoding).
    'list ID from sharepoint URL:
    '   %7B3404D534%2D10CB%2D4F53%2DBB9D%2D37F5612155F1%7D
    'list ID after replacing as follows:
    '   %7B3404D534%2D10CB%2D4F53%2DBB9D%2D37F5612155F1}
    strSharepointListID = "{3404D534–10CB–4F53–BB9D–37F5612155F1}"
    strSharepointSite = "[sanitized for SO post]"
    
    Set cnt = New ADODB.Connection
    With cnt
        .ConnectionString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=" & strSharepointSite & ";LIST=" & strSharepointListID & ";"
        .Open
    End With

        mySQL = "insert into [Isaac Test Excel To Sharepoint] (column1,column2) values ('col1_val1','col2_val1');"
        cnt.Execute (mySQL)

    If CBool(cnt.State And adStateOpen) = True Then cnt.Close
    Set cnt = Nothing
End Sub

I'm also fairly sure the SQL syntax is good, because the code DID tell me when it was wrong--When at first I used INSERT TABLE instead of INSERT INTO TABLE.


Solution

  • Partial credit to KeshavSharma (see comments) for

    1. correctly mentioning LIST ID not required, use LIST NAME instead
    2. inspiring me to keep focusing on THAT line of code - that it was the problem

    The final code that worked was precisely identical to the first code I posted, EXCEPT, inside the connection string, instead of:

    I need to use:

    Very happy this got solved - hopefully it helps someone else some day.