0

I am trying to seperate the text box list I have using csv. I am saving it to excel and the titles that I have go into their individual cell, but the text boxes go into one. I want them to be in their seperate cell also.

Also, how can I add new information without overwriting the previous info saved?

Thanks

Dim csvFile As String = My.Application.Info.DirectoryPath & "\HoseData.csv"

Dim outFile As IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(csvFile, False)

outFile.WriteLine("job number, sales order number, date")

outFile.WriteLine(TextBox1.Tex & TextBox2.Text & DateTimePicker1.Text)

outFile.Close()

Console.WriteLine(My.Computer.FileSystem.ReadAllText(csvFile))
3
  • 2
    OpenTextFileWriter(csvFile, True) the last param is whether to append or overwrite. Intellisence will provide you with such tidbits as you type the code. Commented Jun 19, 2015 at 12:56
  • 3
    outFile.WriteLine(TextBox1.Tex & TextBox2.Text & DateTimePicker1.Text), where's the commas it is CSV afterall... Commented Jun 19, 2015 at 12:56
  • CSV files are not random access - so you cant "update" only certain rows/items. You either load and rewrite the whole file or append items Commented Jun 19, 2015 at 13:14

1 Answer 1

1

You need to add commas in your output:

outFile.WriteLine(TextBox1.Text & "," & TextBox2.Text & "," & DateTimePicker1.Text)

Per the additional requirement of quotes around the DateTimePicker data that was fleshed out in the comments below:

outFile.WriteLine(TextBox1.Text & "," & TextBox2.Text & "," & """" & DateTimePicker1.Text & """")

To append instead of overwrite, as Plutonix mentioned above, use

OpenTextFileWriter(csvFile, True)
Sign up to request clarification or add additional context in comments.

8 Comments

thank yall so much. I just learned how to do C# this past spring and my job through me a curve ball with visual basic. So its like a whole new world to me. Ok so how do I get the program to not rewrite the titles (job number, sales order number, ect) and just rewrite the data (textbox1.text...ect)
It depends--if the file doesn't exist to begin with, then you can check for the file existing in an IF statement, and if it doesn't exist, you write the header. If My.Computer.FileSystem.FileExists(csvFile) Then outFile.WriteLine("job number, sales order number, date"). If the file already exists but is blank, then you could check the size, read the first line to see if is the header, check for line count > 1, etc. Lots of ways--you'll need to provide more details about the state of things when the header needs to be written. I recommend that you ask that in a separate question.
awesome..I figured out a way to do it. might not be the right way, but it works lol.. ok so last question and ill stop bugging yall. is there anyway to size the cell before saving my data. I go into excel and resize the cells how i want them. but then when I go back and run my program and hit save, then go check on the data to see if it inserted right, the cells go back to the original way (all crammed and overlapping)
Not if you are saving it as a .csv.
can i specifiy a certain column for, lets say date, to go it. bc I have the datatimepicker, and its going into 3 different cells instead of 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.