Sector10
asked on
Import CSV file into database
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|OP MERKING|VE RWIJDER|IN VOER_DATUM |INVOER|MU TATIE_DATU M|MUTATIE| AANTAL|BED RAG|MUTTYD |Volgnumme r^16|C0002 7244||N|20 /10/98|LGE |20/10/98| LGE|0|0|16 :58:14|5.5 71^05|B000 31264||N|2 6/11/98|AN B|26/11/98 |ANB|0|0|1 2:43:15|9. 965^05|B00 045770||N| 28/10/02|A AH|28/10/0 2|AAH|0|0| 13:53:01|9 4.088^07|B 00045770|| 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|B000457 70||N|28/1 0/02|AAH|2 8/10/02|AA H|0|0|13:5 0:39|94.08 7^06|C0004 7911||N|29 /12/99|HPA |29/12/99| AAH|0|0|08 :54:38|37. 759^
Regards,
Paul
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|OP
Regards,
Paul
You could try creating a temporary csv file and replace those characters with
| replace with a comma
^ replace with a carriage return/linefeed
| replace with a comma
^ replace with a carriage return/linefeed
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
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
Find and replace the ^ with a paragraph mark (^p)
Save the file.
You can now use the GetExternalData option of Access to import it.