VB代码无法用于.CSV导入访问2007

时间:2014-02-20 14:40:10

标签: vba ms-access csv ms-access-2007 access-vba

我正在尝试从空间文件夹中导入.CSV文件以进行访问。

我通过导入第一个CSV文件设置了自己的导入规范。然后我写了这段代码

Sub ImportCSVFiles()
    Dim strPathFile As String, strFile As String, strPath As String
    Dim strTable As String
    Dim blnHasFieldNames As Boolean
    Const IMPORT_SPEC = "Megger Readings Import Specification"
' Change this next line to True if the first row in EXCEL worksheet
' has field names
    blnHasFieldNames = True

' Replace C:\Documents\ with the real path to the folder that
' contains the csv files
    strPath = "C:\Users\arbmaint\Desktop\Jumbo start loading test results"

' Replace tablename with the real name of the table into which
' the data are to be imported
    strTable = "Test Results"

    strFile = Dir(strPath & "*.csv")
    Do While Len(strFile) > 0
    strPathFile = strPath & strFile
    DoCmd.TransferText acImportDelim, "Megger Readings Import Specification", strTable, strPathFile, blnHasFieldNames


 Kill strPathFile

strFile = Dir()
Loop

End Sub

然后我连接了一个按钮以运行此命令,即使它符合正常也没有任何反应。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

代码行

strFile = Dir(strPath & "*.csv")

在命令提示符处执行以下操作:

dir "C:\Users\arbmaint\Desktop\Jumbo start loading test results*.csv"

如果strPath是文件夹的名称,那么在没有反斜杠\分隔符的情况下,您将无法与文件夹内的文件进行任何匹配。试试这个:

strFile = Dir(strPath & "\*.csv")