Solved

Parsing text in VB6

Posted on 2003-11-20
2
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 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