Solved

Parsing text in VB6

Posted on 2003-11-20
2
182 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
2 Comments
 
LVL 15

Accepted Solution

by:
SRigney earned 80 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now