Solved

Import CSV file into database

Posted on 2003-11-28
6
911 Views
Last Modified: 2010-05-01
Hello to you all,

I want to import a csv file that has a  ^  character as the  end of line marker. The fields are seperated by a | sign. Is there someone outthere that knows how to import this file into a database (access 2000)?

The name of the database is Projects.mdb, the Table name is PRKENM and contains the following fields:
KENMERKID
PROJECTNUMMER
OPMERKING
VERWIJDER
INVOER_DATUM
INVOER
MUTATIE_DATUM
MUTATIE
AANTAL
BEDRAG
MUTTYD
Volgnummer

The Filename is prkenm.txt and contains for example the following code:
KENMERKID|PROJECTNUMMER|OPMERKING|VERWIJDER|INVOER_DATUM|INVOER|MUTATIE_DATUM|MUTATIE|AANTAL|BEDRAG|MUTTYD|Volgnummer^16|C00027244||N|20/10/98|LGE|20/10/98|LGE|0|0|16:58:14|5.571^05|B00031264||N|26/11/98|ANB|26/11/98|ANB|0|0|12:43:15|9.965^05|B00045770||N|28/10/02|AAH|28/10/02|AAH|0|0|13:53:01|94.088^07|B00045770||N|28/10/02|ANB|28/10/02|AAH|0|0|13:52:18|14.746^16|B00045770||N|28/10/02|ANB|28/10/02|AAH|0|0|13:52:19|14.747^19|B00045770||N|28/10/02|AAH|28/10/02|AAH|0|0|13:50:39|94.087^06|C00047911||N|29/12/99|HPA|29/12/99|AAH|0|0|08:54:38|37.759^


Regards,

Paul
0
Comment
Question by:Sector10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 9837899
Read the file into Word
Find and replace the ^ with a paragraph mark (^p)
Save the file.
You can now use the GetExternalData option of Access to import it.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9837919
You could try creating a temporary csv file and replace those characters with
| replace with a comma
^ replace with a carriage return/linefeed
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9837961
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 3

Accepted Solution

by:
ashoooo earned 500 total points
ID: 9839139
Split the string using ...

records = split(str, "^")

This will give you an array of records.

Now split each record using ...

fields = split(record(i), "|")


The source code should resemble something like...

Dim records, fields
dim record as string

records = split(str, "^")

for each record in records
  fields = split(record,"|")
  KENMERKID = fields(0)
  PROJECTNUMMER = fields(1)
  '...
  ' Then store this in the database using either ado or dao
next record

Hope this helps
0
 
LVL 1

Author Comment

by:Sector10
ID: 9840023
Ashoooo thank you very much!

Is it also possible to skip the first line and if so how?
This is what i made of it:

Private Sub Command1_Click()
Dim SourceFile As String
Dim records, fields
Dim record As Variant
Dim lngReadFileNum As Long
Dim strBuffer As String
lngReadFileNum = FreeFile

SourceFile = "prkenm.txt"
SourceFileLen = FileLen(SourceFile)
CurrLen = 0
ProgressBar1.Value = 0

Open SourceFile For Input As #lngReadFileNum
'Line Input #lngReadFileNum, strBuffer this is what i tried to skip the first line but it didn't work..
Do Until EOF(lngReadFileNum)
    Line Input #lngReadFileNum, strBuffer
    CurrLen = CurrLen + Len(strBuffer)
    PBarImport.Value = CurrLen / (SourceFileLen / 100)
Loop
Close #lngReadFileNum

records = Split(strBuffer, "^")
With rsPrKenM
    For Each record In records
        If record = "" Then Exit For
        fields = Split(record, "|")
        .AddNew
        !KENMERKID = fields(0)
        !PROJECTNUMMER = fields(1)
        'etcetera..
        .Update
    Next record
    .Requery
End With

rsPrKenM.Close
Set rsPrKenM = Nothing

MsgBox "Done!"
End Sub
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9840102
Sector, the Lbound and Ubound functions can return the lower and upper bound of an array. Also, the Split function returns an array of strings...

Your file consists of only one line... what you need is to skip the first record...
So,

Dim records() as String
Dim fields() as String
dim record as string
Dim i as Integer

' ... after you read the file

records = split(str, "^")


for i = LBound(records) + 1 to UBound(records) ' note that this skips the first element of the array
  fields = split(records(i),"|")
  KENMERKID = fields(0)
  PROJECTNUMMER = fields(1)
  '...
  ' Then store this in the database using either ado or dao
next record


Hope this helps
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run code from text file in vb 1 103
Access query that references subform 5 50
Excel Automation VBA 19 116
"Failed to convert parameter value from a String to a Decimal." 4 43
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question