Parse a text string into the comment fields in SOP10106

On first glance, you might say 'that seems too easy to write about'... but give it a thought. 

If you have a custom text box:

And you're storing the content into SOP10106, you have to break it into 4 strings, (COMMENT_1, etc.) and then store the entire text in CMMTTEXT.

In the example above, we just parse on the vbCRLF chars and go on. But what about this one:

64 words, 419 characters. No vbCRLFs. What logic do we use now? We'll cover that. 

The logic is this:

1 Take the first 50 characters. 

2 If there is a vbCr in that group, break on it and continue

3 If the text is less than 50 characters, store that and exit

4 If not, work backward through the string for a 'new word' character (we only use the space character) and break on that

5 If all that fails (meaning that there were no spaces and no vbCRs), store the 50 chars and continue. 

Full, fully commented code below. 

Public Class SOPInquiryComment
    Dim mstrSopnumber As String
    Dim mintSoptype As Int16
    Sub New(strSopnumber As String, intSoptype As Int16)
 
        ' This call is required by the designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        mstrSopnumber = strSopnumber
        mintSoptype = intSoptype
    End Sub
    Private Sub SOPInquiryComment_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
 
            'data access code to load the data from the SOP10106.CMMTTEXT field into the text box
            Dim oSOP10106 As New SOP10106(mintSoptype, mstrSopnumber)
            If oSOP10106.isPopulated Then
                'change all vbCRLFs to vbCRs
                Me.txtLine1.Text = Replace(oSOP10106.CMMTTEXT, vbCr, vbCrLf)
 
                'keep the text from being selected
                Me.txtLine1.SelectionStart = Me.txtLine1.Text.Length
            End If
 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Try
            'parse the date into four strings using this logic:
            'take the first 50 chars.
            'if there is a vbCR, break on that and continue
            'if the remainder is less than one line, store and exit the while loop
            'if not, work backward and find the first 'new word' char and break on that
            'last, store the whole 50 and continue
 
            'data access code to get a class populated with one line from SOP10106
            Dim oSOP10106 As New SOP10106(mintSoptype, mstrSopnumber)
 
            Dim strString As String = Me.txtLine1.Text
 
            'change all vbCRLFs to vbCRs
            strString = strString.Replace(vbCrLf, vbCr)
 
            'set the main field. This will always be the same as the text box
            oSOP10106.CMMTTEXT = strString
 
            Dim strOut As String = ""
            Dim astrString(3) As String
            astrString(0) = ""
            astrString(1) = ""
            astrString(2) = ""
            astrString(3) = ""
            Dim intCounter As Int16 = 0
            Dim intPos As Int16
            Dim intLength As Int16
 
            'take 4 loops through the text
            While intCounter < 4
                'take the first 50 chars.
                strOut = strString.Substring(0, IIf(strString.Length < 50, strString.Length, 50))
 
                'if there is a vbCR, break on that and continue
                If strOut.Contains(vbCr) Then
                    'get the position of the vbCR
                    intPos = strString.IndexOf(vbCr)
 
                    'set the first array to the text (without the vbCr)
                    astrString(intCounter) = strString.Substring(0, intPos)
 
                    'set our main string to the remainder of the text
                    strString = strString.Substring(intPos + 1, strString.Length - (intPos + 1))
 
                    'loop
                    intCounter += 1
                    Continue While
                End If
 
                'if the remainder is less than one line, store and exit the while
                If strOut.Length = strString.Length Then
                    'we're done. Store and exit
                    astrString(intCounter) = strOut
                    Exit While
                End If
 
                'if not, work backward and find the first 'new word' char and break on that
                For a = strOut.Length - 1 To 0 Step -1
                    If strString.Substring(a, 1) = " " Then
                        'we've found a new word char. set our array string equal to everthing before that
                        astrString(intCounter) = strString.Substring(0, a)
                        'set our main string equal to the remainder
                        strString = strString.Substring(a + 1, strString.Length - a - 1)
 
                        'loop
                        intCounter += 1
                        Continue While
                    End If
                Next
 
                '
                'last, store the whole 50 and continue
                astrString(intCounter) = strOut
                intLength = astrString(intCounter).Length
                If intLength = strString.Length Then
                    Exit While
                End If
 
                strString = strString.Substring(intLength, strString.Length - intLength)
                intCounter += 1
            End While
 
            'data access code to store the results in SOP10106
            oSOP10106.COMMENT_1 = astrString(0)
            oSOP10106.COMMENT_2 = astrString(1)
            oSOP10106.COMMENT_3 = astrString(2)
            oSOP10106.COMMENT_4 = astrString(3)
            oSOP10106.Save()
            Me.Close()
 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
End Class

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences