Solved

Parsing text in VB6

Posted on 2003-11-20
2
183 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

777 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