简体   繁体   English

将 CSV 文件从 SharePoint Online 文档库导入 Access 数据库

[英]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.我在 SharePoint Online 文档库中上传了一个 CSV 文件,该文件每天更新​​。 I use this CSV file to create some reports with Access Database.我使用这个 CSV 文件来创建一些带有 Access 数据库的报告。 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.我想要实现的是将 CSV 文件自动导入 Access,而无需在本地下载和保存 CSV 文件。 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...但是,它要么要求我登录,然后说我无权访问该文件,要么不执行说它无法加载 HTML 页面...

Don't be fooled by the title of the link.不要被链接的标题所迷惑。

You cannot import/link a file directly via HTTP.您不能直接通过 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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