【发布时间】:2014-01-08 16:48:43
【问题描述】:
我被分配了将大约 180 个 csv 文件导入 access 2007 数据库的任务。这些文件多年来一直放在一起,将被放入 3 个文件夹中的 1 个。我没有对这些表设置任何数据检查或限制(例如主键、验证规则或关系)。这将在数据导入后完成。这些文件中包含的数据来自多年来发生变化的调查。此更改导致字段更改。它们的顺序发生了变化,或者有时有一个字段,有时没有。我确实有一个所有可能字段的列表,以及每个 csv 文件应该导入到哪个表,并且知道所有这些字段都可以是文本。
这是我的问题:不知道列的顺序或列是否存在,是否可以运行一个函数,通过将文本文件中的每一列映射到其相关联来将这些文本文件导入到它们的相关表中访问表中的列?
每个文本文件都有标题,这有助于查看它们实际上是什么,但是没有文本限定符,这在处理完全由数字组成的 id 代码时会非常烦人。以下是我到目前为止所尝试的。它从其他地方的函数中获取文件位置,将该位置中的每个文件名添加到集合中,然后对于该集合中的每个文件,它会尝试将其导入到它的相关字段中。
'Get file names from the folder and store them in a collection
temp = Dir(location & "\*.*")
Do While temp <> ""
fileNames.Add temp
temp = Dir
Loop
'Go through each file in the collection and preccess it as needed
For Each temp2 In fileNames
If (temp2 Like "trip*") Then 'Import trip files
'Gets the data from a query 'DoCmd.RunSQL "SELECT * FROM [Text;FMT=Delimited;HDR=YES;IMEX=2;CharacterSet=437;DATABASE=" & location & "].[" & temp2 & "] As csv;"
DoCmd.TransferText acImportDelim, "Trips_Import", "tbl_Trips", location & "\" & temp2, -1
End If
If (temp2 Like "catch*") Then 'Import catch files
DoCmd.TransferText acImportDelim, "Catch_Import", "tbl_Catch", location & "\" & temp2, -1
End If
If (temp2 Like "size*") Then 'Import size files
DoCmd.TransferText acImportDelim, "Size_Import", "tbl_Size", location & "\" & temp2, -1
End If
Next temp2
【问题讨论】:
标签: ms-access csv import ms-access-2007