gpt4 book ai didi

sql - 如何在保存到 OneDrive 的 Excel 工作簿中运行 SQL 查询?

转载 作者:行者123 更新时间:2023-12-04 19:51:22 25 4
gpt4 key购买 nike

我想对所有包含在单个 Excel 工作簿中的表运行 SQL 查询。我的 VBA 代码使用 ADODB 来运行这些 SQL 查询。

当工作簿保存在 OneDrive 时打开连接失败,但当工作簿保存到本地驱动器时打开连接。

如何在保存在 OneDrive 上的单个 Excel 工作簿中的表上运行 SQL?

当书籍保存在本地而不是 OneDrive 上时,代码有效。唯一的变化是在每种情况下看起来都相当不同的文件路径:

OneDrivePathExample = "https://d.docs.live.net/....xlsb"

LocalPathExample = "C:\My Documents\....xlsb"

我在连接字符串中的文件路径周围试验了一些东西,但不出所料,它们没有用:

  1. 原创

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=https://d.docs.live.net/.../Documents/Financial Tracker.xlsb;Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";
  2. 用“\”替换“/”

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=https:\\d.docs.live.net\...\Documents\Financial Tracker.xlsb;Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";`
  3. 在路径两边加上方括号

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[https://d.docs.live.net/.../Documents/Financial Tracker.xlsb];Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";
  4. 在路径周围添加引号

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source="https://d.docs.live.net/.../Documents/Financial Tracker.xlsb";Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";

我意识到我可以通过在运行此代码时将其保存在本地并随后将其保存回 OneDrive 来避免这种情况,但我希望尽可能避免这种情况。

我还意识到我可以编写 VBA 代码来执行我想用 SQL 执行的操作,但是我最初是这样做的,但后来切换到 SQL 方法,因为 SQL 的速度要快得多。

这是我的代码:

Function OpenRST(strSQL As String) As ADODB.Recordset
''Returns an open recordset object

Dim cn As ADODB.Connection
Dim strProvider As String, strExtendedProperties As String
Dim strFile As String, strCon As String

strFile = ThisWorkbook.FullName

strProvider = "Microsoft.ACE.OLEDB.12.0"
strExtendedProperties = """Excel 12.0;HDR=Yes;IMEX=1"";"


strCon = "Provider=" & strProvider & _
";Data Source=" & strFile & _
";Extended Properties=" & strExtendedProperties

Set cn = CreateObject("ADODB.Connection")
Set OpenRST = CreateObject("ADODB.Recordset")

cn.Open strCon ''This is where it fails

OpenRST.Open strSQL, cn

End Function

cn.Open strCon行,出现如下错误:

Run-time error '-2147467259 (80004005)';
Method 'Open' of object '_Connection' failed

谢谢!

最佳答案

这是我获取文件路径的解决方案。

'This Function search root folder as C: ,D: ...
'Search into all OneDrive folders
Option Explicit
Private Const strProtocol As String = "Http"
Private Const pathSeparator As String = "\"

Function MainFindFile(ByRef NullFilePath As String, Optional FileName As String) As Boolean

Dim fso As FileSystemObject 'Necessary enable microsoft scripting runtime in references
Dim UserRootFolder As Folder
Dim SecondSubFolders As Folder
Dim ThirdSubFolders As Folder
Dim InitialPath As String
Dim OneDriveFolderName As String

Set fso = New Scripting.FileSystemObject

InitialPath = ActiveWorkbook.FullName
If FileName = vbNullString Then FileName = ActiveWorkbook.Name

If InStr(1, InitialPath, strProtocol, vbTextCompare) > 0 Then
InitialPath = Environ("SystemDrive")
InitialPath = InitialPath & Environ("HomePath")

'Gets all folders in user root folder
Set UserRootFolder = fso.GetFolder(InitialPath)

For Each SecondSubFolders In UserRootFolder.SubFolders
'Searches all folders of OneDrive, you may have how many Onedrive's folders as you want
If InStr(1, SecondSubFolders.Name, "OneDrive", vbTextCompare) > 0 Then
OneDriveFolderName = InitialPath & pathSeparator & SecondSubFolders.Name
'Verifies if file exists in root of Onedrive Folder
MainFindFile = SearchFile(OneDriveFolderName, FileName, NullFilePath)
If MainFindFile Then Exit For

'Uses recursive function to percur all subfolders in root of OneDrive
For Each ThirdSubFolders In fso.GetFolder(OneDriveFolderName).SubFolders
MainFindFile = RecursiveFindFile(ThirdSubFolders, FileName, NullFilePath)
If MainFindFile Then Exit For
Next ThirdSubFolders
End If
If MainFindFile Then Exit For
Next SecondSubFolders

End If

MsgBox NullFilePath

End Function
Private Function RecursiveFindFile(Folder As Folder, FileName As String, ByRef NullFilePath As String) As Boolean

Dim fso As FileSystemObject
Dim objFolder As Folder
Dim Result As Boolean

Set fso = New Scripting.FileSystemObject

'Verifies if file exists in root of Onedrive Folder
RecursiveFindFile = SearchFile(Folder.Path, FileName, NullFilePath)
If RecursiveFindFile Then Exit Function

For Each objFolder In Folder.SubFolders
If Not SearchFile(objFolder.Path, FileName, NullFilePath) Then
RecursiveFindFile = RecursiveFindFile(objFolder, FileName, NullFilePath)
If RecursiveFindFile Then Exit For
Else
RecursiveFindFile = True
Exit For
End If
Next objFolder

End Function
Private Function SearchFile(Path As String, FileName As String, ByRef NullFilePath As String) As Boolean

'NullFilePath is a byref variable to be filled by this function
Dim fso As New Scripting.FileSystemObject

If fso.FileExists(Path & pathSeparator & FileName) Then
NullFilePath = Path & pathSeparator & FileName
SearchFile = True
End If

End Function

关于sql - 如何在保存到 OneDrive 的 Excel 工作簿中运行 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54081879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com