我是新手,非常感谢您的帮助和耐心。提前很多人。我需要正确的语法/代码来获取访问dbase表中的文件名。下面是代码和输出。输出是8列,前7列是数字,第8列是我想要的文件名。代码转到目录,检查那里的所有csv文件,然后将csv数据导入访问dbase表。我需要用csv文件名填充列'com'。目前它填充了一个字符串。
Sub Import_multiple_csv_files()
Const strPath As String = "C:\text1\" 'Directory Path
Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim SQL As String
'Loop through the folder & build file list
strFile = Dir(strPath & "*.csv")
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 & import to Access
'creating a new table called MyTable
SQL = " UPDATE Test SET Com = ""'strFile'"" WHERE Com IS NULL OR Com=''"
For intFile = 1 To UBound(strFileList)
DoCmd.TransferText acImportDelimi, , _
"Test", strPath & strFileList(intFile)
'DoCmd.RunSQL SQL
CurrentDb.Execute SQL
输出似乎不是
F1 F2 F3 F4 F5 F6 F7 com
20111128 2.6922 2.6922 2.6922 2.6922 3340 17696 'strFile'
20111129 2.7229 2.7229 2.7229 2.7229 5010 18141 'strFile'
20111130 2.7401 2.7401 2.7401 2.7401 3641 18723 'strFile'
20111201 2.7376 2.7376 2.7376 2.7376 8087 19321 'strFile'
20111202 2.7784 2.7784 2.7784 2.7784 0 0 'strFile'
20111128 2.6727 2.6727 2.6727 2.6727 3889 26111 'strFile'
20111129 2.7039 2.7039 2.7039 2.7039 4562 26647 'strFile'
20111130 2.722 2.722 2.722 2.722 3043 27099 'strFile'
20111201 2.7218 2.7218 2.7218 2.7218 9180 27037 'strFile'
20111202 2.7623 2.7623 2.7623 2.7623 0 0 'strFile'
答案 0 :(得分:1)
编辑原始问题比编写新问题更好,但答案是您混淆了引号而没有将变量连接到字符串中,应该是这样的:
SQL = "UPDATE Test SET Com = '''" & strFile & "''' WHERE Com IS NULL OR Com = ''"
使用引号括起strFile会导致它被视为字符串文字,而不是获取字符串变量本身的值。
答案 1 :(得分:0)
似乎你实际上并不想要数据中的单引号:
SQL = "UPDATE Test SET Com = '" & strFile & "' WHERE LEN(Com) = 0;"
...但我认为您需要更改后续循环中的SQL,例如
'cycle through the list of files & import to Access
'creating a new table called MyTable
SQL = "UPDATE Test SET Com = '{0}' WHERE LEN(Com) = 0;"
For intFile = 1 To UBound(strFileList)
DoCmd.TransferText acImportDelimi, , _
"Test", strPath & strFileList(intFile)
CurrentDb.Execute Replace$(SQL, "{0}", strPath & strFileList(intFile))
...但只有在更新null和空值时才会生效!