如何使用VBA代码将csv文件导入Access? [英] How to import csv file to Access with VBA code?
问题描述
我有一个csv文件,我需要使用VBA导入Access。
I have a csv file, that I need to import to Access using VBA.
我正在使用以下代码:
Call DoCmd.TransferText(acImportDelim, , TableName, SourceFile, HasFieldNames)
其中TableName,SourceFile和HasFieldNames是存储有关文件信息的变量。
Where TableName, SourceFile and HasFieldNames are variables that store information about the file.
导入有效,但我只导入了一列表格。
The import works but I have only one column imported in the table.
有人有想法吗?
该文件是带有分隔符的csv; 并且有多列。
The file is a csv with the separator ";" and has multiple columns.
谢谢。
推荐答案
CSV 代表逗号分隔值。因此,默认导入功能会查找逗号。
"CSV" stands for "Comma Separated Values". Hence, the default import functionality looks for commas.
如果您使用逗号以外的任何内容,请尝试以下操作:
If you're using anything but commas, try this:
-
手动开始导入文件。
Start importing the file manually.
在指定设置之后,在单击完成之前,单击高级。 ...
When you have specified the settings, just before clicking Finish, click Advanced...
点击另存为并指定一个名称(或接受Access提议的名称)。
Click "Save As" and specify a name (or accept the one proposed by Access).
记下您选择的名称,让我们说数据导入规范。
Make note of the name you choose, let's say "Data Import Specification".
单击确定两次,然后取消导入。
Click OK twice, then cancel the import.
现在使用您在代码中创建的导入规范:
Now use the import specification that you created in the code:
DoCmd.TransferText acImportDelim ,数据导入规范,表1,D:\ Data.csv,错误
DoCmd.TransferText acImportDelim, "Data Import Specification", "Table1", "D:\Data.csv", False
这篇关于如何使用VBA代码将csv文件导入Access?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!