将 csv 文件导入访问的 VBA 程序 [英] VBA procedure to import csv file into access

查看:40
本文介绍了将 csv 文件导入访问的 VBA 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 VBA 中的程序将数据导入到来自 csv excel 文件的访问中,而没有一些记录,如页眉和页脚.例如,,, 我在 csv 文件中有一个表,其中包含一些不属于表日期的句子

i need procedure in VBA to import data into access from csv excel file without some records,, as header and footer. Example,,, i have table in csv file, which contains some sentence which not belong table date

A1 这是一些句子标题.......
A2标题
A3......
A7 数据数据数据数据数据数据
A8 行 DATA DATA DATA DATA DATA..........
A256 数据 数据 数据 数据
A257 这是一段话

A1 this is some sentence title.......
A2 title
A3.......
A7 DATA DATA DATA DATA DATA
A8 rows DATA DATA DATA DATA DATA ...... ....
A256 DATA DATA DATA DATA
A257 this is some sentence

我的 Acess 应该只包含 A7 到 A256 之间的行.有没有人知道解决我的问题的 VBA 中的程序或其他内容?

My Acess shoud contain only rows between A7 to A256. Does anyone knows procedure or whatever in VBA who solves my problems ?

非常感谢

编辑

推荐答案

最简单的方法是将 CSV 文件作为表链接到 Access 数据库中.然后,您可以像处理普通访问表一样处理该表,例如基于该表创建适当的查询,该查询返回您想要的内容.

The easiest way to do it is to link the CSV-file into the Access database as a table. Then you can work on this table as if it was an ordinary access table, for instance by creating an appropriate query based on this table that returns exactly what you want.

您可以像这样手动或使用 VBA 链接表格

You can link the table either manually or with VBA like this

DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tblImport", _
    FileName:="C:MyData.csv", HasFieldNames:=true


更新

Dim db As DAO.Database

' Re-link the CSV Table
Set db = CurrentDb
On Error Resume Next:   db.TableDefs.Delete "tblImport":   On Error GoTo 0
db.TableDefs.Refresh
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tblImport", _
    FileName:="C:MyData.csv", HasFieldNames:=true
db.TableDefs.Refresh

' Perform the import
db.Execute "INSERT INTO someTable SELECT col1, col2, ... FROM tblImport " _
   & "WHERE NOT F1 IN ('A1', 'A2', 'A3')"
db.Close:   Set db = Nothing

这篇关于将 csv 文件导入访问的 VBA 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆