VBA程序将CSV文件导入到Access

VBA程序将CSV文件导入到Access

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

问题描述

我需要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 somesentence which not belong table date

A1这是一​​些句子标题.A2标题
A3 .......
A7 DATA DATA DATA DATA DATA
A8行DATA DATA DATA DATA DATA..........
A256 DATA DATA DATA DATA
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

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

08-01 22:07