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.
Does this Dlookup have incorrect syntax?
How to combine two fields in Form to one field from Microsoft Access?
Report building from selection in datasheet
Multiple arguments IsNull function MS Access VBA
how to pass parameters to ExportWithFormatting using vbscript
Access 2010 cascading combo boxes runtime error 424
Access Month Calendar View
Shapefile to MDB with custom field structure [duplicate]
Query JET 4 database with SSIS
Microsoft Access 2010: Query Criteria moving
Is there a way to create an Access database from a Visio database model?
Access: insert multiselect box values into database
ms access extra table indexes
Recordsource vs Recordset for Unbound Forms
Access Database Remote Connection
How to programmatically convert Access 1997 .mdb to Access 2007 .accdb