Results 1 to 17 of 17

Thread: Split text with length

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Unhappy Split text with length

    Hi,
    I have a textbox with multiline for the address such as:
    18-G, Kuala Kangsar Road, 65890 Ipoh, Perak

    I split it with "," so that each line can be saved to the add1, add2, add3, add4, add5, etc...respectively.
    The result is:
    18-G
    Kuala Kangsar Road
    65890 Ipoh
    Perak

    But I would like to split it with the fixed length, like:
    18-G, Kuala Kangsar Road,
    65890 Ipoh,
    Perak

    Please help in the VB6 coding. Hope my question is clear to you, thanks in advance.
    Viv

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Split text with length

    No it isn't really

    Can you show us exactly what your input is and what you want the output to be?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Split text with length

    it is a texbox with multilines setting for user to key in the address.
    I would like to split the address in the format like:
    18-G, Kuala Kangsar Road,
    65890 Ipoh,
    Perak

    and saved each line of the address to different field such as: add1, add2 and add3....

    thanks

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Split text with length

    So each line goes into a different variable.

    What result do you want?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Split text with length

    i want to save each line to the different variable in the database.

    so that it can be used for the report. I would like to display the address in seperated line and not a whole one line.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Split text with length

    You want to access the database from VB? In that case, I can't help you, sorry.
    Which bit of the coding do you need help with?

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

    Re: Split text with length

    Your description of the problem is a little vague, but the following code should get you started.

    You need to change "yourdatabase.mdb" to the name of your database. Inside the database, create a table with 6 fields - idnum as an autonumbered primary key and 5 fields named add1, add2, add3, add4 and add5. You need to create a form with a textbox called txtAddress and a commandbutton called cmdSave, then add a reference to the Microsoft ADO 2.x Library.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim objConn As ADODB.Connection
    4.  
    5. Private Sub cmdSave_Click()
    6.     Dim strAddressLines() As String
    7.     Dim intPointer As Integer
    8.     Dim objRS As ADODB.Recordset
    9.  
    10.     If txtAddress.Text <> "" Then
    11.         strAddressLines = Split(txtAddress.Text, ",")
    12.        
    13.         Call ConnectToDatabase(App.Path & "\yourdatabase.mdb")
    14.        
    15.         Set objRS = New ADODB.Recordset
    16.         objRS.Open "Addresses", objConn, adOpenStatic, _
    17.           adLockPessimistic, adCmdTable
    18.         objRS.AddNew
    19.        
    20.         For intPointer = 0 To UBound(strAddressLines)
    21.             If intPointer < 5 Then
    22.                 objRS.Fields("add" & CStr(intPointer + 1)) = strAddressLines(intPointer)
    23.             Else
    24.                 MsgBox "Too many address lines", vbOKOnly + vbExclamation, App.Title
    25.                 Exit For
    26.             End If
    27.         Next intPointer
    28.        
    29.         objRS.Update
    30.         If objRS.State <> adStateClosed Then
    31.             objRS.Close
    32.         End If
    33.         Set objRS = Nothing
    34.        
    35.         Call DisconnectFromDatabase
    36.     End If
    37. End Sub
    38.  
    39. Private Sub ConnectToDatabase(ByVal strPath As String)
    40.     Set objConn = New ADODB.Connection
    41.    
    42.     objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    43.       "Data Source=" & strPath & ";" & _
    44.       "Mode=Share Deny None;"
    45.      
    46.     objConn.Open
    47. End Sub
    48.  
    49. Private Sub DisconnectFromDatabase()
    50.     If objConn.State <> adStateClosed Then
    51.         objConn.Close
    52.     End If
    53.     Set objConn = Nothing
    54. End Sub

    I would recommend looking at some tutorials if you have never worked with databases before through VB.
    eg. http://www.developerkb.com/modules/w...hp?category=23

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Re: Split text with length

    Thank you for the coding.

    I would like to split the text at "," , For example: 265, Kuala Kangsar Road, 65000 Ipoh. The result will be:

    265
    Kuala Kangsar Road
    65000 Ipoh

    I would like to split the text at "," but with the fixed length (30). The result
    I would like to have is:

    265, Kuala Kangsar Road
    65000 Ipoh

    Please help

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

    Re: Split text with length

    That's more difficult to do - why not have separate text boxes for each line of the address? You'd find it a lot easier to code.

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

    Re: Split text with length

    Is this what you want?

    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"
    6.    
    7.     Do Until Len(strAddress) < 31
    8.         strTest = Left$(strAddress, 30)
    9.         intPos = InStrRev(strTest, ",")
    10.         Debug.Print Left$(strAddress, intPos)
    11.         strAddress = Right$(strAddress, Len(strAddress) - intPos)
    12.         ' get rid of the leading space
    13.         If Left$(strAddress, 1) = " " Then
    14.             strAddress = Right$(strAddress, Len(strAddress) - 1)
    15.         End If
    16.     Loop
    17.     ' Show the last part
    18.     Debug.Print strAddress

  11. #11
    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.

  12. #12
    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

  13. #13
    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

  14. #14

    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.

  15. #15

  16. #16

    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

  17. #17
    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