Excel VBA to query MS Access database on Sharepoint with ADODB - “Not a valid file name”

余生颓废 提交于 2019-12-25 08:24:01

问题


any chance someone could enlighten me on why I'm getting an "Not a valid file name" error when trying to connect to MS Access database stored on Sharepoint? I have no issues with connecting to file on C:\

    Set cnn = New ADODB.Connection
    MyConn = "C:\somelocation\database.accdb"

    With cnn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open MyConn
    End With

but when I change MyConn to Sharepoint address it doesn't work :/

     MyConn = "https://some.website.com/somelocation/database.accdb"

I'm getting a "Not a valid file name". I'd greatly appreciate your help!


回答1:


Your MyConn is just an URL, it is not an SMB file share which is what the ADODB connection expects.

You can download the file to a local folder:

Option Compare Database
Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long

Public Function DownloadFile( _
    ByVal strURL As String, _
    ByVal strLocalFilename As String) _
    As Long

' Download file or page with public access from the web.
' 2004-12-17. Cactus Data ApS, CPH.

' Usage, download a file:
' lngRet = DownloadFile("http://www.databaseadvisors.com/Graphics/conf2002/2002ConferencePicsbySmolin/images/dba02smolin27.jpg", "c:\happybassett.jpg")
'
' Usage, download a page:
' lngRet = DownloadFile("http://www.databaseadvisors.com/conf2002/conf200202.asp", "c:\dbaconference.htm")

' Returns 0 if success, error code if not.
' Error codes:
' -2146697210 "file not found".
' -2146697211 "domain not found".

' Limitation.
' Does not check if local file was created successfully.

    Dim lngRetVal As Long

    lngRetVal = URLDownloadToFile(0, strURL & vbNullChar, strLocalFilename & vbNullChar, 0, 0)

    DownloadFile = lngRetVal

End Function

Then connect to the local file.




回答2:


use connection string below, while the file path should be in this format

\YourSiteDomain\DavWWWRoot\YourFilePath\YourDBase.accdb (open you SharePoint site in Explorer view, right click the database file select properties, then you can see the file path)

connection string: Provider=Microsoft.ACE.OLEDB.12.0;Data Source=YourFilePath;Jet OLEDB:Database



来源:https://stackoverflow.com/questions/40864673/excel-vba-to-query-ms-access-database-on-sharepoint-with-adodb-not-a-valid-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!