Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Parsing text in VB6

Posted on 2003-11-20
2
Medium Priority
?
190 Views
Last Modified: 2010-05-01
I'm new to VB and need a suggestion about how to parse this text file.
I'm using VB6.

The text file has multiple records,
Each record begins with "GIGISA"

The record has many fields with different names and formats but
each field ends with a Tilde ~
each field element is seperated by astricks  "*"
The fields that show  city state and Zip looks like this:

N1*CompanyName~
N2*Business 2ndName~
N3*Main Street~
N4*Carthage*MO*64836~


The record is not carriage returned, it all runs together.
Someone mentioned to me I may want to use a Regular expression.  Is that a good idea in this situation?


0
Comment
Question by:vb_learner
1 Comment
 
LVL 15

Accepted Solution

by:
SRigney earned 240 total points
ID: 9790273
You could use a regular expression, but that would get very complicated,  you'll probably be better parsing it yourself if its as consistent as your example makes it sound.

Private Sub ParseData()
   Dim strRecords As String
   strRecords = "GIGISA~N1*CompanyName~N2*Business 2ndName~N3*Main Street~N4*Carthage*MO*64836~GIGISA~N1*AnotherCompanyName~N2*2nd Business 2ndName~N3*Center Street~N4*Carthage*MS*64831"
   
   Dim aRecords
   Dim aFields
   Dim aSubFields
   Dim intRecords As Integer
   Dim intFields As Integer
   Dim intSubFields As Integer
   
   aRecords = Split(strRecords, "GIGISA")
   ' I use 1 to skip the empty first item.
   ' If the first GIGISA isn't until after the first record then use a 0 here.
   For intRecords = 1 To UBound(aRecords)
      Debug.Print aRecords(intRecords)
      aFields = Split(aRecords(intRecords), "~")
      ' I use a 1 to skip the empty first item.
      ' if there is no ~ between GIGISA and the first field then use 0.
      For intFields = 1 To UBound(aFields)
         Debug.Print aFields(intFields)
         aSubFields = Split(aFields(intFields), "*")
         For intSubFields = 0 To UBound(aSubFields)
            Debug.Print aSubFields(intSubFields)
         Next intSubFields
      Next intFields
   Next intRecords
End Sub
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

824 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