在 VBA 中访问数据项目导入 CSV 文件 [英] Access Data Project Importing CSV File In VBA
问题描述
我时不时会遇到一个问题,我的同事开发的旧系统会出现问题.他们往往有数千行代码来做一个简单的事情,比如导入一个 csv 文件.
Every now and then I come across a problem with an old system one of my colleagues has developed. They tend to have thousands of lines of code to do a simple thing like importing a csv file.
目前vba进程为:
- 打开excel应用程序
- 创建新工作表
- 填充csv文件
- 进入excel,将标题名称添加到文件中
- 将工作表另存为新的 Excel 文件
- 将文件导入到访问数据项目 sql 表中.
- 处理数据
我想用它做的是:
- 将 csv 导入表格(如获取外部数据功能)
- 处理数据
我进行了快速搜索,但找不到任何简单的方法将文件吸入表格中.
I have had a quick search and cannot see any easy methods of just sucking the file into the table.
任何帮助将不胜感激.
谢谢
保罗
推荐答案
导入 CSV 的方法更简单!您可以使用 Microsoft Text Odbc 驱动程序.
There is an easier way to import a CSV! You can use the Microsoft Text Odbc Driver.
Sub Import()
Dim conn as new ADODB.Connection
Dim rs as new ADODB.Recordset
Dim f as ADODB.field
conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};DBQ=c: emp;"
rs.Open "SELECT * FROM [test.txt]", conn, adOpenStatic, adLockReadOnly, adCmdText
While Not rs.EOF
For Each f In rs.Fields
Debug.Print f.name & "=" & f.Value
Next
Wend
End Sub
您从 Select 更改为 INSERT INTO 与 SELECT 组合,然后就可以了.
You change from a Select to an INSERT INTO combined with a SELECT and there you are.
您可以在注册表中进行一些设置,在键 \HKEY_LOCAL_MACHINESOFTWAREMicrosoftJet4.0EnginesText
中:
There are some settings you can do in the registry, in the key \HKEY_LOCAL_MACHINESOFTWAREMicrosoftJet4.0EnginesText
:
格式:TabDelimited、CSVDelimited、Delimited(X) 其中 X=某些字符
Format: TabDelimited, CSVDelimited, Delimited(X) where X=some char
FirstRowHasNames: 0,1
FirstRowHasNames: 0,1
字符集:OEM、ANSI
CharacterSet: OEM, ANSI
这篇关于在 VBA 中访问数据项目导入 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!