Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import CSV file into VB .dat file

Status
Not open for further replies.

Nickerzzzzz

IS-IT--Management
Oct 27, 2009
17
GB
Hi All
Can anyone give me direction or better still give me a piece of code that will read the contents of a csv file into a file created in VB e.g. a .dat file.

I have a corrupt .dat file containing the following example data...
2 12 20 33 35 45
15 33 39 40 45 48

I have the full set of records in csv/xls format and need to get it back into the .dat file format so the VB program can read All records again.

I have the option in the program to manually input the data a record at a time but this would take a while.

I wrote the program some time ago and have been adding data manually for some time, and now I have this file corruption! so if possible I want to re-add all the data in one go, I am well out of touch with VB code so need a few pointers to get me back into it.

Thanks in advance

Nick (erzzzzz)
 



Hi,

A .csv file is just a text tile, with text separated by commas.

So change the filename to .dat if you so care to. How is that going to help?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi Skip

Allready tried that and it didn't work :-( got some strange results with any of the test files I made.

Cheers


Nick(erzzzzz)
 

If you have a csv file that looks like:
[tt]
2,12,20,33,35,45
15,33,39,40,45,48
[/tt]
and you want it to look like:
[tt]
2 12 20 33 35 45
15 33 39 40 45 48
[/tt]
You may just do:
Code:
Dim strTextLine As String

Open "C:\MyFile.csv" For Input As #1
Open "C:\MyFile.dat" For Output As #2

Do While Not EOF(1)
   Line Input #1, strTextLine
   [green]'Replace a comma with a Space[/green]
   Print #2, Replace(strTextLine, ",", " ")   
Loop

Close #2
Close #1

Have fun.

---- Andy
 
2,12,20,33,35,45
15,33,39,40,45,48

and you want it to look like:

2 12 20 33 35 45
15 33 39 40 45 48

You may just do:
Or if you want to do this in 5 seconds, you could just open it in Notepad, and do a Search & Replace of all commas into spaces.
 



Allready tried that and it didn't work got some strange results with any of the test files I made.
Everyone is trying to GUESS at WHAT you tried and WHAT didn't work and WHAT strange results you got, because...

you NEVER disclosed this vital information. You just threw it out there.

Help yourself out!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hmm, my lack of knowledge/experience and info has obviously caused some confusion...appologies for that!
.
I have a basic form which takes a number of integer inputs via some text box's, these are then saved to a file from which the form/program opens and closes, the file is only readable via the VB program, if I try to open/edit it from within notepad or the like then I just get gobble de gook.
.
This is a few lines of code from my program, hopefully this will give you the pointers as to why my program does not save the .dat file in standard text format.
.
Option Explicit
Dim RecordNum As Long
Dim Record As Numbers
.
Open "C:\myfile.dat" For Random As #1 Len = 56
RecordNum = LOF(1) / 56
RecordNum = RecordNum + 2
Txt_DisNum.Text = RecordNum
Record.Num_1 = Txt_1.Text
Record.Num_2 = Txt_2.Text
Record.Num_3 = Txt_3.Text
Record.Num_4 = Txt_4.Text
Record.Num_5 = Txt_5.Text
Record.Num_6 = Txt_6.Text
Put #1, RecordNum, Record
Close #1
.
.Module.bas
Option Explicit
Type Numbers
Num_1 As Integer
Num_2 As Integer
Num_3 As Integer
Num_4 As Integer
Num_5 As Integer
Num_6 As Integer
End Type
 
All sorted!

I think it was my lack of understanding with regards to Open File For Random with 'Get'/'Put' and reading/writing a record, this seems to produce gobble de gook in the file created.

Using the Open File For Output and using Write Var1, Var2, Var3 etc etc etc worked a treat, I can now manipulate the files/data I have to get my records back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top