Results 1 to 7 of 7

Thread: breaking a string into sections

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10

    Question

    How can I break a string into fixed sections. For example, I need to break a string which has a max length of 100 characters into 5 strings of 20, where string each of the strings of 20 characters are fixed positions of the 100 character string, 0-19, 20-39, 40-59...

    Thanks - Bob

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Use the MID function, similar to the following.

    Code:
    Private Sub Command1_Click()
        Dim strTest As String
        Dim str1 As String
        Dim str2 As String
        Dim str3 As String
        Dim str4 As String
        Dim str5 As String
        
        strTest = "This is a test string that will be split into 5 pieces " & _
                  "each consisting of 20 characters"
    
        str1 = Mid(strTest, 1, 20)
        str2 = Mid(strTest, 21, 20)
        str3 = Mid(strTest, 41, 20)
        str4 = Mid(strTest, 61, 20)
        str5 = Mid(strTest, 81, 20)
        
        MsgBox str1 & vbCrLf & str2 & vbCrLf & str3 & vbCrLf & str4 & vbCrLf & str5
    End Sub
    [Edited by jbart on 11-13-2000 at 09:40 AM]

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10
    Is it also possible to read a database field directly into a number of text boxes, with each box showing a section of the field. I also need to be able to modify the individual field sections, so that the database may be updated with the new text from the group of text boxes.

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'breaking strings using a loop
    '
    'used a listbox for display only

    Code:
    Private Sub Command1_Click()
    
        List1.Font = "COURIER"
        
        Dim i As Integer, x As String
        x = "123456987458457;lkjhgbghtyrgtrtfgrgbnhjuyijkolp;[plkoijuhygtfrdesdfghjkmnbvcxzasdfghjkl;poiuytrewqq9"""
                
            Dim y As Integer
        'give i the value of the length of your string
        'oc course this will vary with different strings.
        
            i = Len(x)
        'do until the length of the string is diminished
            Do Until i <= 1
         
              If i >= 20 Then
                  List1.AddItem Left(x, 20)
                  x = Right(x, i - 20)
                  i = Len(x)
              Else
              List1.AddItem Right(x, i)
                   x = ""
                   i = Len(x)
            End If
            
                Loop
      
    End Sub
    For your last post, please give an example of what you have and what you want. It might help understand it a little more.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10
    What I have now is an Ado object pointing to a field in a database - the field is line of text about 100 chars. This was used to allow the user to traverse the db, and modify the full line the text box.

    The new requirement is for the user to be able to view the full line of text (in one box), and view it in a series of boxes, which each contain about 20 chars each. The user needs to be able to modify the small text boxes, which are then summed back together and written back to the db field.

    I'm running into some problems doing this. I have had the large text box tied to the ado object, which displays the field information ok. If I have the small text boxes get data from the big text box which gets from the ado, how do I get the modifications to go from small+small... to big..to ado?

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    You now have the string broken into smaller parts and the user can type in changes to the smaller section of codes

    then Dim myNesString as string
    myNewString = text1 & text2 & text3 & text4 & text5

    'haven't used ado but I would do it this way with dao

    if you are using a databound textbox you would just replace
    the text with myNewString and ensure all fields are filled and then
    datName.Recordset.Edit
    datName.Recordset.MoveNext 'this updates or use Update
    datName.Refresh

    and you are updated

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10
    thanks for the help jbart and HeSaidJoe

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