CSV in visual Basic - vb.net

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))

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)

Related

How can I get the value of a combobox into a string in vba module?

I have a form that example exports do xml based on query. That query is build with the conditions of combobox.
The combobox have only one field.
What I want is to, in vba module, name de xml file with the conditions of the combobox.
Example: value of combobox: 03, Lucas, Roger, 23.
file: myXML.Save "C:\teste\03_Lucas_Roger_23.xml"
You can use this expression:
FileName = "C:\teste\" & Replace(Replace(Me!YourCombobox.Value, ",", "_"), " ", "") & "xml"
file: myXML.Save FileName
You can concatenate the values like this.
I've added an additional test for null, because if no value is selected on one or more of the ComboBoxes it will throw an error.
If IsNull(ComboBox1.Value) Or IsNull(ComboBox2.Value) Or IsNull(ComboBox3.Value) Or IsNull(ComboBox4.Value) Then
MsgBox ("Not all values selected!")
Else
Dim myFileName As String
myFileName = "C:\teste\" & ComboBox1.Value & "_" & ComboBox2.Value & "_" & ComboBox3.Value & "_" & ComboBox4.Value & ".xml"
myXML.Save myFileName
End If

how to use variable containing strings in formula vba

I ask the user input for a string and then need to incorporate this into a formula. I searched on other questions but didn’t get the desired output.
Sname = InputBox("Enter name")
Cells(2, 32).FormulaR1C1 = _
=CONCATENATE(J2,""-"",K2,""-"",L2,""-"" "" & Sname & "" -"",T2,U2,V2,W2,X2,Y2,""-"",AB2,""-"",AC2)
suppose i enter AAA
i want the formula on cell(2,32) to be
=CONCATENATE(J2,"-",K2,"-",L2,"-" & "AAA" & "-",T2,U2,V2,W2,X2,Y2,"-",AB2,"-",AC2)
Try this:
Cells(2,32).Formula = _
"=CONCATENATE(J2,""-"",K2,""-"",L2,""-"" & """ & Sname & """ & ""-"",T2,U2,V2,W2,X2,Y2,""-"",AB2,""-"",AC2)"
There was a slight mixup with your quotation marks around the variable.
This one works for me.
swap " for ' Tip: you can use CTRL+U
=CONCATENATE(J2,'-',K2,'-',L2,'-' & 'AAA' & '-',T2,U2,V2,W2,X2,Y2,'-',AB2,'-',AC2)
Add a replace function
Replace("=CONCATENATE(J2,'-',K2,'-',L2,'-' & 'AAA' & '-',T2,U2,V2,W2,X2,Y2,'-',AB2,'-',AC2)","'", Chr(34))
Chr(34) equal to caracter "

Write text to log file Columns

How do I write to the columns in a .log file?
I.e there are columns for "Log Text","Component","Date/Time"
How do I specify these when writing to a file?
I've got half of it working:
dim str As String ="<![LOG[" & message & "]LOG]!><time=""" & Now.ToLongTimeString & """" & " date=""" & Now.ToShortDateString & """ component=""" & component.ToString & """" & " type=""1""" & " Thread=""" & t & """" & ">"
File.AppendAllText(logfile, str & vbCrLf)
But the component, date / time and thread values arent displaying properly.
What am I missing ?
*edited
The file path is "C:\Programdata\server.log"
So some of the text is getting into it in the right place, just not all of it.
So the log text column will get populated with "message" and thread comes in with the number but the date/time and component are empty.
I'd attach a pic but i dont have enough rep :/
In a sentance, Im trying to replicate this:
http://www.jetico.com/web_help/bcwipe6_enterprise/img/log_viewer.jpg
but not all of my columns are displaying data.
Try dividing your data into columns using commas as suggested by #Blackwood, but use string.format as you seem to have lots of extra "" in there
Dim str As String = String.Format("![LOG[{0}]LOG]!,time={1}, <date= {2}, component= {3}, type=1, Thread={4}", Message, Now.ToLongTimeString, Now.ToLongDateString, component.ToString, t)
File.AppendAllText(logfile, Str() & vbCrLf)
I'm not sure what you are doing with the <> tags.
Solved.
This is to do with the format of the data I was trying to write.
Time has to be in the format
Now.ToLongTimeString & "." & Now.Millisecond & "-60"
and date has to be seperated with - instead of /
Dunno why - when viewed the time doesnt go to that length and the date is displayed with "/"
The viewer im using for the log was cmtrace.
This was the line that got it working was ..
<![LOG[" & message & "]LOG]!><time=""" & Now.ToLongTimeString & "." & Now.Millisecond & "-60" & """" & " date=""" & d & """ component=""" & component.ToString & """" & " type=""1" & """ Thread=""" & t &""">

Removing duplicated lines from a textbox and getting the count of the removed lines. (VB.NET)

I am developing a bar management software, i did the most of the job but now i don't know the code to remove the same lines from a textbox and getting the number of the removed lines after they are removed.
My code till now:
For Each saveitem As ListViewItem In Form1.ListView1.Items
RichTextBox1.AppendText(saveitem.Text & vbNewLine)
TextBox3.AppendText(saveitem.SubItems(1).Text & vbNewLine)
Next
RichTextBox1.AppendText(vbNewLine & vbNewLine & "TOTALI:" & " " & Form1.TextBoxX5.Text & vbNewLine & "TOTALI pa TVSH:" & " " & TextBox4.Text)
One possible way :
'get array of individual line of text'
Dim textLines = TextBox3.Text.Split(New String(){vbNewLine}, StringSplitOptions.RemoveEmptyEntries)
Dim totalCount = textLines.Length
Dim noDuplicateCount = textLines.Distinct().Count()
'store number of lines removed'
Dim removedLinesCount = totalCount - noDuplicateCount
'join distinct line of texts by new line into single string'
TextBox3.Text = String.Join(vbNewLine, textLines.Distinct())

How to print text lines in a textbox before using Append.Text in windows form vb.net

I am trying to add the contents of my five text boxes in the form to a multi line text box named "TextBox1". However, everytime I change the content of my textboxes, instead of adding another line with the contents it replaces the pre-existing line. How do I make the next line print.
Dim Newline As String
Newline = System.Environment.NewLine
TextBox1.Text = "Plane Truss"
TextBox1.Text = TextBox1.Text & Newline & "JointCoordinates"
TextBox1.AppendText(Newline & Joint# & " " & coordinates1 & " " & bc1 & " " & jointloads1 & " " & settlements1 & " " & jointrotation1)
Is there a way I can convert the whole multiline text box into a .txt
file?
Sure...use the Lines() property of the TextBox and File.WriteAllLines():
System.IO.File.WriteAllLines("c:\some path\folder\file.txt", Result1.Lines)