首页 > 解决方案 > ADO 连接(在 VBA 中)无法插入到 Sharepoint 列表

问题描述

我使用 ADO 是因为我想在 Excel 中使用 VBA 写入 Sharepoint 列表。现在我得到“Microsoft Access 数据库引擎找不到对象'Isaac Test Excel To Sharepoint',并且 INSERT 行上的代码错误。我怀疑这是因为我的站点引用错误,或者我的列表 ID错了。

我不认为我的列表 ID 有误,因为我仔细按照说明从您转到列表设置时暴露的 URL 中提取列表 ID,小心地替换此处提到的 3 个可替换项目: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 )。

我将其传递为:

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

我想得出的结论是,“连接是正确的,因为代码直到一直到 INSERT 语句才会出错”,但不幸的是,我已经证明这是错误的:如果我传入一个完全不存在的站点值,胡言乱语,代码直到 INSERT 语句一直都不会出错。

我的名单的名字肯定是 Isaac Test Excel To Sharepoint

我通过的网站是这样的,我通过用“文本”替换一些文本来清理它:(我已经尝试了所有这 3 个):

  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#/"

完整代码:

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

我也相当确定 SQL 语法很好,因为代码确实告诉我什么时候出错了——当我最初使用 INSERT TABLE 而不是 INSERT INTO TABLE 时。

标签: vbasharepointado

解决方案


部分归功于 KeshavSharma(见评论)

  1. 不需要正确提及 LIST ID,请改用 LIST NAME
  2. 激励我继续关注那行代码——这就是问题所在

工作的最终代码与我在连接字符串中发布的第一个代码完全相同,除了:

  • 列表={8F7FEF30–C868–4480–8AC8–4FE4FDB3921A};

我需要使用:

  • LIST=Isaac 测试 Excel 到 Sharepoint;(尽管对象名称中有空格 - 我需要使用 NO 单引号,NO 括号)。

很高兴这得到了解决 - 希望有一天它可以帮助其他人。


推荐阅读