Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Cedar Falls, Iowa, USA
    Posts
    268
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parsing a string with up to 2 commas (a2003)

    The following pieces of code are extracted from a much longer procedure. It takes a string (which could be x or x, y) entered into an unbound control and breaks it down into its component pieces based on the comma.

    I need to adapt it to be able to handle a string with up to 2 commas and parse it into its component pieces, i.e. x or x and y or x and y and z depending on what's entered in the control. I'm new to parsing and am unclear how to handle the second comma. Any help is welcome.

    E

    Dim intCS As Integer
    Dim intC As Integer

    Select Case True
    Case intCS > 0
    ![txtOrgName] = left(strNewData, intCS - 1)
    ![txtOrgDept] = Mid(strNewData, intCS + 2)
    Case intC > 0
    ![txtOrgName] = left(strNewData, intC - 1)
    ![txtOrgDept] = Mid(strNewData, intC + 1)
    Case Else
    ![txtOrgName] = strNewData
    End Select

    PS What do I need to do to keep from losing the indents on the code sample?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: parsing a string with up to 2 commas (a2003)

    About your PS first: there are two methods of preserving indentation in code:

    1. Insert a <!t>[pre]<!/t> tage before and a <!t>[/pre]<!/t> tag after the code. Example:

    <!t>[pre]<!/t>
    For i = 1 To 100
    Debug.Print i
    Next i
    <!t>[/pre]<!/t>

    becomes
    <pre> For i = 1 To 100
    Debug.Print i
    Next i
    </pre>

    2. Replace 4 spaces (or whatever your default indent is) by <!t>[tab]<!/t> tags. Example:

    <!t>[tab]<!/t>For i = 1 To 100
    <!t>[tab]<!/t><!t>[tab]<!/t>Debug.Print i
    <!t>[tab]<!/t>Next i

    becomes

    For i = 1 To 100
    Debug.Print i
    Next i

    See <post#=189637>post 189637</post#> for a discussion about this, and some tips.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: parsing a string with up to 2 commas (a2003)

    About your question: with 2 commas, your string would be divided into three parts. Where does the third part go? In the code you posted, you have txtOrgName and txtOrgDept. You need a third component.

  4. #4
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Cedar Falls, Iowa, USA
    Posts
    268
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: parsing a string with up to 2 commas (a2003)

    third part is txtOrgOffice

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: parsing a string with up to 2 commas (a2003)

    Try this:

    Dim intC1 As Integer
    Dim intC2 As Integer

    ' Test for comma
    intC1 = InStr(strNewData, ",")
    If intC1 = 0 Then
    ' No comma at all, only Name is filled
    ![txtOrgName] = strNewData
    ![txtOrgDept] = Null
    ![txtOrgOffice] = Null
    Else
    ' Name is first part
    ![txtOrgName] = Trim(Left(strNewData, intC1 - 1))
    ' Test for second comma
    intC2 = InStr(intC1 + 1, strNewData, ",")
    If intC2 = 0 Then
    ' No second comma, rest is Dept
    ![txtOrgDept] = Trim(Mid(strNewData, intC1 + 1))
    ![txtOrgOffice] = Null
    Else
    ' We have two commas, so three parts
    ![txtOrgDept] = Trim(Mid(strNewData, intC1 + 1, intC2 - intC1 - 1))
    ![txtOrgOffice] = Trim(Mid(strNewData, intC2 + 1))
    End If
    End If

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: parsing a string with up to 2 commas (a2003)

    Or even shorter:

    Dim strParts() As String
    strParts = Split(strNewData, ",")
    ![txtOrgName] = Trim(strParts(0))
    If UBound(strParts) > 0 Then
    ![txtOrgDept] = Trim(strParts(1))
    End If
    If UBound(strParts) > 1 Then
    ![txtOrgOffice] = Trim(strParts(2))
    End If

  7. #7
    3 Star Lounger
    Join Date
    Jul 2002
    Location
    Cedar Falls, Iowa, USA
    Posts
    268
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: parsing a string with up to 2 commas (a2003)

    Thanks, worked like a charm.

    E

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •