Results 1 to 17 of 17

Thread: Split text with length

Hybrid View

  1. #1
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Split text with length

    Just to point out, though - if a line is longer than 30 characters, it will go into an unending loop.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Split text with length

    OK, here is a fix.

    VB Code:
    1. Dim intPos As Integer
    2.     Dim strTest As String
    3.     Dim strAddress As String
    4.    
    5.     strAddress = "18-G, Kuala Kangsar Road, 65890 Ipoh, Perak ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
    6.    
    7.     Do Until Len(strAddress) < 31
    8.         strTest = Left$(strAddress, 30)
    9.         intPos = InStrRev(strTest, ",")
    10.         If intPos = 0 Then
    11.             'no comma so look for a space instead
    12.             intPos = InStrRev(strTest, " ")
    13.         End If
    14.         If intPos = 0 Then
    15.             ' no space either so just artificially break it
    16.             intPos = 30
    17.         End If
    18.         Debug.Print Left$(strAddress, intPos)
    19.         strAddress = Right$(strAddress, Len(strAddress) - intPos)
    20.         ' get rid of the leading space
    21.         If Left$(strAddress, 1) = " " Then
    22.             strAddress = Right$(strAddress, Len(strAddress) - 1)
    23.         End If
    24.     Loop
    25.     ' Show the last part
    26.     Debug.Print strAddress

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Split text with length

    There should be no comma delimiter between a house (or lot#) and the street name.

    Are the users in your locale used to entering: 123, MAIN STREET??

    Is that comma needed?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Split text with length

    yes, the user used to put delimiter between a house (or lot#) and the street name.

  5. #5

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Split text with length

    Thank you. I need to try it out first. Heres are my coding, since i dont know how to fixed the length, i make it in other way. It might not in the good standard but it works. I will try yours too.

    Dim strLineArr() As String
    Dim strWordArr() As String
    Dim intLineIdx As Integer, intWordIdx As Integer

    strLineArr() = Split(txtAddress.Text, vbCrLf)

    For intLineIdx = 0 To UBound(strLineArr)
    strWordArr() = Split(strLineArr(intLineIdx), ",")
    If Len(strWordArr(0)) < 5 Then
    rs_add(1) = strWordArr(0) & "," & "" & strWordArr(1) & ","
    For intWordIdx = 2 To UBound(strWordArr)
    rs_add(intWordIdx) = strWordArr(intWordIdx) & ","
    Next
    Else
    For intWordIdx = 0 To UBound(strWordArr)
    rs_add(intWordIdx) = strWordArr(intWordIdx) & ","
    Next
    End If
    Next

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Split text with length

    Quote Originally Posted by vivian2u
    yes, the user used to put delimiter between a house (or lot#) and the street name.
    Since the comma is required between the house/lot# and street name then you cannot use SPLIT - that's obvious.

    Do you always have a house/lot# in your data entry - or can there be a street name by itself?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

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



Click Here to Expand Forum to Full Width