Abundant Technologies - IT Consulting Experts

 Source Code Library

 Code Details

     
'Get the test program with the "Test this code" link 'Put this section in a form with the controls 'txtString, txtParse, cmdParse, and lstResults Option Explicit Private Sub CmdParse_Click() 'Create an object Dim mSP As Object Dim x As Integer ' Assign the stringParser to the object. Set mSP = New StringParser ' Place the Parsing text into the Objects Parse Field mSP.Parse = TxtParse.Text ' Place the text to parse in the Objects Text Field. ' As Soon as this text in in the "Text" Field the object parses the text. mSP.Text = TxtString.Text ' Clear the list box LstResult.Clear ' For all the Fields add the Field to the List box. ' Note Field(0) contains all the string. (Read-Only) For x = 0 To mSP.NumberOfFields LstResult.AddItem mSP.Field(x) Next End Sub 'Put this section in a module Option Explicit ' This class is used to take a string and cut the string into several fields. ' ' Create an object in the main project and set the properties, Example: ' ' NOTE: the Default Parse is the pipe character "|" ' ' Example on how to use the StringParser: ' ' dim mSP as object ' set mSP = new StringParser ' ' mSP.Parse = "|" ' mSP.Text "Some|Text|Goes|Here" ' ' when you allocate the string to the Text property the class will cut the string into ' several fields depending on the Parse Property ' ' mSP.NumberOfFields returns the total number of fields in the above example (4) ' mSP.Field(x) returns each field depending on the value of x. if x is 0 ' it will return the whole string. ' eg. ' mSP.Field(0) = "Some|Text|Goes|Here" ' mSP.Field(1) = "Some" ' mSP.Field(2) = "Text" ' mSP.Field(3) = "Goes" ' mSP.Field(4) = "Here" 'local variable(s) to hold property value(s) Private m_NF As Integer 'local copy (Read Only) Private m_F() As String 'local copy (Read Only) Private m_Parse As String 'local copy Private m_Text As String 'local copy Public Property Let Text(ByVal vData As String) 'used when assigning a value to the property, on the left side of an assignment. 'Syntax: X.Text = 5 Dim x As Integer Dim p As Integer Dim p2 As Integer Dim Txt As String m_Text = vData If m_Text <> "" Then m_NF = 0 ReDim Preserve m_F(0 To m_NF) m_F(0) = m_Text If m_Parse <> "" Then p = 0 Do p2 = InStr(p + 1, m_Text & m_Parse, m_Parse) If p2 > 0 Then Txt = Mid$(m_Text, p + 1, (p2 - 1) - p) If Txt <> "" Then m_NF = m_NF + 1 ReDim Preserve m_F(0 To m_NF) m_F(m_NF) = Mid$(m_Text, p + 1, (p2 - 1) - p) End If End If p = p2 Loop Until p2 = 0 Else m_NF = 1 ReDim Preserve m_F(0 To m_NF) m_F(1) = m_Text End If Else m_NF = 0 ReDim Preserve m_F(0 To m_NF) m_F(0) = "" End If End Property Public Property Get Text() As String 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.Text Text = m_Text End Property Public Property Let Parse(ByVal vData As String) 'used when assigning a value to the property, on the left side of an assignment. 'Syntax: X.Parse = 5 m_Parse = Left$(vData, 1) End Property Public Property Get Parse() As String 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.Parse Parse = m_Parse End Property Public Property Get Field(index As Integer) As String 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.F Field = m_F(index) End Property Public Property Get NumberOfFields() As Integer 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.NF NumberOfFields = m_NF End Property Private Sub Class_Initialize() m_Text = "" m_Parse = "|" 'Pipe character m_NF = 0 End Sub

Mail To: info@3pc.com