Results 1 to 7 of 7

Thread: text code

  1. #1
    Join Date
    Jun 2002
    Posts
    173

    Unanswered: text code

    Is there a way to return all the first letters of every word in a sentence?
    ie. one text field has the entry "this is some data", the next text box will default to "tisd". I know you can use the left function but how would you apply it to multiple words that would be different each time depending on the record?

    Any help would be appreciated.

  2. #2
    Join Date
    Jul 2002
    Location
    Australia
    Posts
    147
    Code:
    Private Sub GetFirstLetter()
        Dim strSource, strResult As String
        Dim arrWords As Variant
        
        'Sentence to use
        strSource = "This is a test"
        
        'Split words into array
        arrWords = Split(strSource, " ")
        
        'loop through array
        For i = 0 To UBound(arrWords)
            strResult = strResult & Left(arrWords(i), 1)
        Next
        
        Debug.Print strResult
    End Sub
    Cheers,
    Andrew
    Last edited by Bunce; 07-12-02 at 00:54.
    There have been many posts made throughout the world.
    This was one of them.

  3. #3
    Join Date
    Jun 2002
    Posts
    173
    I gave it a try. It gives me a message:

    Compile error:
    Sub or function not defined.


    Then it opens the code and highlights the word "split" in the code.

    I'm pretty new at this so I'm not really sure how to fix it. Any help would be great. Thanks.

  4. #4
    Join Date
    Feb 2002
    Posts
    2,232
    I bet a $1 that you are not using Access 2000. If you are not, then you will have to be more creative and use the mid and instr functions. Please respond if you need additional information.

  5. #5
    Join Date
    Jun 2002
    Posts
    173
    You're right. I'm using Access97. If you have any info it would be a great help. Thanks.

  6. #6
    Join Date
    Nov 2001
    Posts
    336
    Try function below.

    msgbox test("This is a test.")


    Public Function test(ByVal strText As String) As String

    Dim intI As Integer
    Dim strCharacter As String
    Dim bolNewWord As Boolean

    test = ""
    bolNewWord = True
    If Nz(strText, "") = "" Then Exit Function

    For intI = 1 To Len(strText)
    strCharacter = Mid(strText, intI, 1)
    If strCharacter = " " Then
    bolNewWord = True
    Else
    If bolNewWord And ((strCharacter >= "a" And strCharacter <= "z") Or (strCharacter >= "A" And strCharacter <= "Z")) Then
    test = test & strCharacter
    bolNewWord = False
    End If
    End If
    Next

    End Function

  7. #7
    Join Date
    Feb 2002
    Posts
    2,232
    Try the following:

    Dim str1 As String
    Dim str2 As String
    Dim iCount As Integer

    str1 = "This old house"
    str2 = Left(str1, 1)
    iCount = InStr(1, str1, " ")

    While iCount
    str2 = str2 & Mid(str1, iCount + 1, 1)
    iCount = InStr(iCount + 1, str1, " ")
    Wend

    If you are concerned that the string being passed to you contains leading/trailing blanks then use the trim function.

    Good luck.

Posting Permissions

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