Question on Substring method of the String Class

Sadie

Member
Joined
Oct 31, 2005
Messages
7
Location
Cambridge
Programming Experience
1-3
I am struggling a little to find out how I can acheive the following:

I have saved some data to a file and from that file I need to create a report. The saved data is saved in the following format:
000123600020001201230156

I have used a text box to receive the data called txtData - this bit I have managed to accomplish. However, I need to split the data into columns under their individual field names i.e. the first 6 digits is a reference number. I am trying to use the 'SubString' method to acheive this together with the TAB function but can't get the logic right.

Here is my code:

VB.NET:
Dim strText, LineOfText As String
'print data from sales file.
OpenFileDialog1.Filter "Text files (*.TXT)|*.txt"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
Try
'open file and trap any errors.
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do Until EOF(1)
LineOfText LineInput(1)
strText LineOfText.Substring(0, 6)
Loop
frmReport.txtData.Text strText 'read sales file to txtData.
Catch ex As Exception
MsgBox("Error opening file.")
Finally
FileClose(1)
End Try
End If

Any ideas?

Many thanks,
Sadie
 
You have a couple of lines that are not quite right:

VB.NET:
Dim strText, LineOfText As String
'print data from sales file.
OpenFileDialog1.Filter "Text files (*.TXT)|*.txt"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
Try
'open file and trap any errors.
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
Do Until EOF(1)
LineOfText LineInput(1)
strText = LineOfText.Substring(0, 6) '<- this should be an assignment to the variable.
frmReport.txtData.Text = strText 'read sales file to txtData <- this needs to be IN the loop, and an assignment too.
'Assuming you want the above line to concatenate the items....it reall should be
frmReport.txtData.Text = frmReport.txtData.Text & strText & ControlChars.NewLine 'I think the new line bit is right.
Loop
Catch ex As Exception
MsgBox("Error opening file.")
Finally
FileClose(1)
End Try
End If

-tg
 
Back
Top