ms-access


VBA procedure to import csv file into access


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 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
My Acess shoud contain only rows between A7 to A256. Does anyone knows procedure or whatever in VBA who solves my problems ?
thanks a lot
Edit
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.
You can link the table either manually or with VBA like this
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tblImport", _
FileName:="C:\MyData.csv", HasFieldNames:=true
UPDATE
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
Your file seems quite small (297 lines) so you can read and write them quite quickly. You refer to Excel CSV, which does not exists, and you show space delimited data in your example. Furthermore, Access is limited to 255 columns, and a CSV is not, so there is no guarantee this will work
Sub StripHeaderAndFooter()
Dim fs As Object ''FileSystemObject
Dim tsIn As Object, tsOut As Object ''TextStream
Dim sFileIn As String, sFileOut As String
Dim aryFile As Variant
sFileIn = "z:\docs\FileName.csv"
sFileOut = "z:\docs\FileOut.csv"
Set fs = CreateObject("Scripting.FileSystemObject")
Set tsIn = fs.OpenTextFile(sFileIn, 1) ''ForReading
sTmp = tsIn.ReadAll
Set tsOut = fs.CreateTextFile(sFileOut, True) ''Overwrite
aryFile = Split(sTmp, vbCrLf)
''Start at line 3 and end at last line -1
For i = 3 To UBound(aryFile) - 1
tsOut.WriteLine aryFile(i)
Next
tsOut.Close
DoCmd.TransferText acImportDelim, , "NewCSV", sFileOut, False
End Sub
Edit re various comments
It is possible to import a text file manually into MS Access and this will allow you to choose you own cell delimiters and text delimiters. You need to choose External data from the menu, select your file and step through the wizard.
About importing and linking data and database objects -- Applies to: Microsoft Office Access 2003
Introduction to importing and exporting data -- Applies to: Microsoft Access 2010
Once you get the import working using the wizards, you can save an import specification and use it for you next DoCmd.TransferText as outlined by #Olivier Jacot-Descombes. This will allow you to have non-standard delimiters such as semi colon and single-quoted text.

Related Links

how to display specific records for editing in MS Access
creating more forms after exporting MS Access database
ms access - return records from sql Stored procedure
How to check if .Attachment.Add “filename” is successful before send
Access 2003 Select records with null time
Access: Shell cmd Open MDB
Append Records From Passthrough Query to Local Table
MS-Access library for performing many kinds of DDL operations on a database (to facilitate application version upgrades)?
How to Remove Stop Words from a string using Visual Basic?
Microsoft Access runtime error 2455 when trying to access grandchildren forms from child form
Microsoft Access - stored procedure with cursor
MS Access - Write to Table Immediately After Changing Value in Form
Unbind Access Project from source control
DAO access recordset not updating
No values given for one or more required parameters error only on some Windows 7 machines
In Memory, Stand-Alone, Disconnected ADO Recordset

Categories

HOME
pandas
osgi
ngrx
drivers
dictionary
mfc
comparison
hashmap
at-command
spring-cloud-stream
yarn
ckeditor
installshield
azure-storage-tables
contact
amazon-cloudformation
elasticsearch-hadoop
dacpac
google-translate
kibana-4
alignment
clearcase-ucm
opentracing
tostring
angular-ui
connection-string
arabic
angular2-aot
centos6.5
rst2pdf
greendao
subdomains
objectanimator
google-cloud-nl
r-raster
tinymce-4
junit5
karaf
swift3.0.2
greenrobot-eventbus
android-tabhost
ruby-on-rails-3.1
nand2tetris
isbn
logfiles
taffy
dotcover
theming
fancybox-2
csound
diagnostics
magma
windows-mobile-6.5
removechild
radtreelist
intrusion-detection
phishing
multi-level
underscore.js-templating
skype4py
energy
ora-00900
security-testing
nsight
ami
dereference
bigbluebutton
codeigniter-url
cdt
phalanger
nsbutton
socketexception
jquery-layout
reactfx
resty-gwt
message-driven-bean
blending
dotnetnuke-5
eventual-consistency
yui-compressor
bitsharp
dice
pysimplesoap
javaspaces
parametric-equations
work-stealing
adk
infobox
django-tagging
xmemcached
winbugs14
celltable
imac
h.323
nhibernate.search
forums
eqatec
aquaticprime
spec#
ugc

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App