0

我被使用 VBA 代码的 CSV 文件的 SQL 介导导入所阻碍。我正在使用第三个 EXCEL 宏/电子表格来分析 2 个文件的 LEFT JOIN,一个作为 XLXS,另一个作为 CSV。

我怀疑部分问题可能是如何使用 SQL 命令来获取对 Excel 文件的 FROM 引用。我正在使用 Excel VBA,2010,14 数据库访问引擎。

我想以从外部逗号分隔的 CSV 文件中提取的 SQL 语句结束

我预计在启用宏的独立 excel 文件中使用此伪代码标题宏:

dbEngine = CreateObject(DAO.engine ... )
set DB = dbEngine.OpenDatabase(theNormalExternalExcellFile,....)

对于 SQL 语句,在伪代码中,我想要这个:

SELECT fields 
    FROM [Table$]   ' a normal external excel file
    LEFT JOIN [an external CSV, comma delimited file]
    ON...
    GROUP...

我可以在一个简单的 SQL 语句中独立地成功导入 XLXS 或 CSV,但是当我将外部文件引用放在 SQL 的 FROM 子句中时,我会遇到两个错误之一,具体取决于我使用代码的方式:无效的文件路径,或 FROM 子句中的错误。路径是无效的。

错误显示在下面的记录集指令中发生的位置。

我还提供了替代的 SQL 字符串,我曾用它来测试代码中生成错误的位置。

'the Seating Chart
strPathSource = ThisWorkbook.Worksheets("Logic").Range("rngPathSource")
'strFileNameSource = ThisWorkbook.Worksheets("Logic").Range("rngFileNameSource")
'strFileNameSourceWOExt = Left(strFileNameSource, Len(strFileNameSource) - 4)

'the attendance
strPathAttendance = ThisWorkbook.Worksheets("Logic").Range("rngPathAttendance")
strFileNameAttendance = ThisWorkbook.Worksheets("Logic").Range("rngFileNameAttendance")
strFolderAttendance = ThisWorkbook.Worksheets("Logic").Range("rngFolderAttendance")
strFileNameAttendanceWOExt = Left(strFileNameAttendance, Len(strFileNameAttendance) - 4)

Set dbE = CreateObject("Dao.DBEngine.120")
Set db = dbe.OpenDatabase(strPathSource, True, False, "Excel 12.0;HDR=Yes")

''Set db = DAO.OpenDatabase(strFolderAttendance, True, False, "text;HDR=Yes;FMT=Delimited(,)")

'[Master$] is a tab on the spreadsheet at strPathSource
'[Attendance#csv]
'  This reference to the table at strPathAttendance which otherwise works: [Attendance#csv]
'     when not inside the FROM clause

strSQL = _
    "SELECT tM.Job, Count(tA.Name) AS CountOfName" _
    & " FROM [Master$] tM" _
    & " LEFT JOIN" _
    & " (SELECT * FROM [text;HDR=Yes;FMT=Delimited(,);Database='" _
        & strPathAttendance & "'].[" & strFileNameAttendanceWOExt & "#csv]) tA" _
    & " ON (tM.GivenName = tA.GivenName) AND (tM.SurName = tA.SurName)" _
    & " GROUP BY tM.Job" _
    & " ORDER BY tM.Job, Count(tA.Name)"

'Debug.Print strSQL
' This is the reported value for the string, strSQL, particularly the FROM clause:
' SELECT tM.Job, Count(tA.Name) AS CountOfName FROM [Master$] tM LEFT JOIN
'  (SELECT * FROM
'     [text;HDR=Yes;FMT=Delimited(,);Database=T:\Solutions Team Shared Folder\Seats -
'     Attendance\Attendance.csv].[Attendance#csv]) tA
'        ON (tM.GivenName = tA.GivenName) AND (tM.SurName = tA.SurName)
'        GROUP BY tM.Job ORDER BY tM.Job, Count(tA.Name)
'' putting a single or double quote, around the database path, does not change the error

Set rstR = db.OpenRecordset(strSQL)
'Error:
'  'T:\...\...\Attendance.csv' is not a valid path.  Make sure that
'  the path name is spelled correctly and that you are connected to the server
'  on which the file resides.

' ALT SQL strings, to test what's going on.
'strSQL = _
'   "Select * FROM [Attendance#csv]"

'strSQL = _
'   "Select * FROM (Select * FROM [Excel 12.0;HDR=Yes;Database=" & strPathSource & "].[Master$])"

'strSQL = _
'   "SELECT * FROM [text;HDR=Yes;FMT=Delimited(,);Database=" _
'   & strPathAttendance & "].[" & strFileNameAttendanceWOExt & "#csv]"

'strSQL = _
'   "Select * FROM [Excel 12.0;HDR=Yes;Database=" & strPathSource & "].[Master$]"
4

1 回答 1

0

当使用 Jet/ACE SQL 连接到文本文件时,数据库参数需要引用目录路径而不是任何特定的文本文件。然后,期间限定符将指定单个文件。

strPathAttendance因此,只需从(不带引号)中删除文件名和扩展名。所以查询应该如下所示:

SELECT tM.Job, Count(tA.Name) AS CountOfName 
FROM [Master$] tM 
LEFT JOIN
  (SELECT * FROM
    [text;HDR=Yes;FMT=Delimited(,);Database=T:\Solutions Team Shared Folder\Seats -
    Attendance].[Attendance#csv]) tA
ON (tM.GivenName = tA.GivenName) AND (tM.SurName = tA.SurName)
GROUP BY tM.Job 
ORDER BY tM.Job, Count(tA.Name)
于 2016-08-09T00:35:36.033 回答