简体   繁体   中英

Import a CSV file into Access database from SharePoint Online Document Library

I have a CSV file uploaded in a SharePoint Online Document Library that gets updated on a daily basis. I use this CSV file to create some reports with Access Database. What I would like to achieve is to import the CSV file into Access automatically without the need to download and save the CSV file locally. The code I'm using is as follows:

Sub ImportCSV()

   DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Daily_Report_Table", FileName:="https://mycompany.sharepoint.com/teams/Daily%20Reporting.csv", HasFieldNames:=True

End Sub

However, it either asks me to log in and then it says I don't have access to the file or doesn't execute saying it cannot load HTML page...

Don't be fooled by the title of the link.

You cannot import/link a file directly via HTTP. The code downloads the file for you, ready to be linked:

Sub ImportCSV()

    Dim Result As Long
    Dim Url As String
    Dim Filename As String

    Url = "https://mycompany.sharepoint.com/teams/Daily%20Reporting.csv"
    Filename = "C:\Imports\DailyReporting.csv"

    Result = DownloadFile(Url, Filename)
    If Result = 0 Then
        DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Daily_Report_Table", FileName:=Filename, HasFieldNames:=True 
    End If

End Sub

Supporting code module:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM