parse file

Posted on 2004-11-02
Medium Priority
Last Modified: 2010-05-02
I have a file that looks like ISA*00**00*27*00523*ZZ*M4788
Could you give me example code on how to parse this file that has * seperating elements?

Question by:lindapat
LVL 28

Expert Comment

ID: 12475351
Private Sub Command1_Click()
Dim ff As Integer, Ln As String, arrLn() As String
ff = FreeFile
Open "C:\Myfile.txt" For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
' split line into array by "*"
arrLn = Split(Ln, "*")

' loop through array
Dim i As Integer
For i = 0 To UBound(arrLn)
    If arrLn(i) <> "" Then MsgBox arrLn(i)
Next i

Close #ff

End Sub

Expert Comment

ID: 12475361
TheFile = your file

Dim tmp() as string

tmp() = split(TheFile, "*")

For a = 0 to ubound(TheFile)
   Debug.Print tmp(a)

Author Comment

ID: 12475604
I am new at VB.net so please excuse me.  Will this code work? If not would you correct it for me? I want to show each element in label I have created on a form.
Private Sub read835()
        Dim isaLine As String
        Dim elementSep, compositeSep, segmentSep As String
        Dim x As Integer
        Dim tmp() As String
        isaLine = "ISA*00*          *00*          *27*00523          *ZZ*M4788          *040414*2351*U*00401*410500028*0*P*:~"
        elementSep = isaLine.Substring(103, 1)

        For x = 0 To isaLine.Length
            tmp = Split(isaLine, "*")

        lblMsg.Text = tmp(x)

Accepted Solution

Allpax earned 2000 total points
ID: 12477547
For x = 0 To isaLine.Length
            tmp = Split(isaLine, "*")
This is wrong because the split function acts like a loop itself and parses the string.  All you would need is

    tmp() = Split(isaLine, "*")

Expert Comment

ID: 12478529
yes but then there will be blank lines.. for instance..

if you split it by * it will look like this:

tmp(0) = "ISA"
tmp(1) = "00"
tmp(2) = "          "
tmp(3) = "00"
tmp(4) = "          "
tmp(5) = "27"
tmp(6) = "00523"
tmp(7) = "ZZ"


so which lines do you want to get out??  if you do it like i had it, u could get all the information out and check if it's all spaces, and if it is skip it.. such as:

Dim tmp() as string

tmp() = split(TheFile, "*")

For a = 0 to ubound(TheFile)
   if Trim(tmp(a)) <> "" Then MsgBox tmp(a)

ubound fwould be the uppermost index in the array, and trim replaces spaces on the ends of the string, so trim("       ") would be the same as ""

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question