我有一些工作代码循环遍历一个完整的Excel文件的文件夹,并将每个表导入到Access表中。我所要做的只是在名为的文件名的表格末尾添加一个名称为源Excel文件的字段。访问VBA:导入多个文件时添加'文件名'字段
我做了一些谷歌搜索,发现此解决方案: How to add file name when importing multiple Excel files to one Access table
我试图解决方案纳入我的代码,但是当我到达执行语句,我得到:
运行时错误'3061'参数太少。预计2.
我认为问题只是与strSQL
声明和/或我在最后执行它的方式。
Public Sub Command0_Click()
Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim filename As String
Dim path As String
Dim qdf As DAO.QueryDef
Set db = CurrentDb()
'make the UPDATE a parameter query ...
strSQL = "UPDATE Test SET FileName=[pFileName] WHERE FileName Is Null OR
FileName='';"
Set qdf = db.CreateQueryDef(vbNullString, strSQL)
path = "C:\Users\u005984\Desktop\Test\"
'Loop through the folder & build file list
strFile = Dir(path & "*.xlsx")
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If
'cycle through the list of files
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferSpreadsheet acImport, 9, "Test", filename, True
'Add filename field
qdf.Parameters("pFileName").Value = strFileList(intFile)
qdf.Execute dbFailOnError
Next intFile
End Sub
我是Access VBA和SQL的新手,无法弄清楚它为什么期待2个参数。感谢任何帮助。
关闭我的头顶,尝试从结束移除分号strSQL查询。除此之外,在执行之前,我很想知道'qdf.Parameters.Count'是什么。 – DHW