Solved

Accessing parts of text in text boxes (70pts)

Posted on 1998-12-11
3
130 Views
Last Modified: 2010-05-03
How can I read a specific word of a certain line in a text box? If I have a box containing lots of words, I want to be able to get a certain word and read it by referring to some kind of address, e.g.

If I had a box containing the sentence "The cat sat on the mat.", how could I ask the program to return, say, word 3?
0
Comment
Question by:The_Oracle
3 Comments
 

Expert Comment

by:lankford
Comment Utility
As long as you are using a vb text box, you are probably going to have to parse the text and figure it out your self.

If you are programmatically using Microsoft Word as your text box, then you can actually refer to the third word in the Word object with something like this:

   ThirdWord = Document.Word(3)

Does anyone out there know of a text box ocx control that stores its information in easily accessible objects like this?

Lankford

0
 
LVL 1

Expert Comment

by:OeilNoir
Comment Utility
Yep, you would have to check all text, but you could write a function who would create a kind of index of words. something what would keep the position of the word and it's lenth
then with the index you could get your word

"this is my string and i want to find a word in it"

for the word "my" you would keep the position (9) and the lenth (2) . and you could read your index to know where the words are. would be longer whe you create your index, but faster when you search your word...
0
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 70 total points
Comment Utility
Here are some parsing functions you might find useful to achive that task:

'PURPOSE:    Finds part of a string found after the N occurence of a substring
'ARGUMENTS:
'   IN szMainString:    string to extract from
'   IN szSubString:     substring to look for
'   IN iOccurence:      the number of the occurence to return the string after it
'   IN bCaseSensetive:  specifies whether the search is case sensetive
'RETURNS:    The string located after the first occurence of a substring
'
Public Function GetAfterSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found, or if there is nothing after it
    If iPos = 0 Or iPos = Len(szMainString) Then
        GetAfterSubstring = ""
        Exit Function
    End If
   
    GetAfterSubstring = Right(szMainString, Len(szMainString) - iPos)
End Function
'PURPOSE:    Finds part of a string found before the N occurence of a substring
'ARGUMENTS:
'   IN szMainString:    string to extract from
'   IN szSubString:     substring to look for
'   IN iOccurence:      the number of the occurence to return the string before it
'   IN bCaseSensetive:  specifies whether the search is case sensetive
'RETURNS:    The string located before the first occurence of a substring
'
Public Function GetBeforeSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found
    If iPos = 0 Then
        GetBeforeSubstring = ""
        Exit Function
    End If
   
    GetBeforeSubstring = Left(szMainString, iPos - 1)
End Function
'PURPOSE:   Locates the position of the last occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the last substring in a string
'
Public Function InstrL(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim nLen As Integer
    Dim nLen1 As Integer
    Dim iComparisonMethod As Integer
    Dim bFlag As Boolean
   
    nLen = Len(szMainString)
    nLen1 = Len(szSubString)
    If nLen = 0 Or nLen1 = 0 Then Exit Function
   
    'Set comparison method
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
   
    'Look for the first substring from the end
    For I = nLen - nLen1 To 1 Step -1
        If InStr(I, szMainString, szSubString, iComparisonMethod) > 0 Then
            bFlag = True
            Exit For
        End If
    Next I
   
    If Not bFlag Then
        InstrL = 0
        Exit Function
    End If
   
    InstrL = I
End Function
'PURPOSE:   Locates the position of the N occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN iOccurrance:     the number of the occurence of the substring to return
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the N substring in a string
'
Public Function InstrN(szMainString As String, szSubString As String, Optional iOccurance As Integer = 1, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim iComparisonMethod As Integer
    Dim iPos As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
    iPos = 1
   
    If iOccurance = 1 Then
        InstrN = InStr(1, szMainString, szSubString, iComparisonMethod)
        Exit Function
    End If
   
   
    For I = 1 To iOccurance 'Look for the substring N times
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
        If iPos = 0 Then    'If no more found, means that there are less occurences then specified
            InstrN = 0
            Exit Function
        End If
    Next I
   
    InstrN = iPos
End Function

'PURPOSE:   Counts how many times a substring appears in a string
'ARGUMENTS:
'   IN szMainString:    string to look in
'   IN szSubString:     string to look for
'   IN bCaseSensetive:  Specifies whether the comparison is case sensetive
'RETURNS:   The number of times the substring occures in the string
'
Public Function GetOccurrenceCount(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Long
    Dim iPos As Integer
    Dim iCount As Integer
    Dim iComparisonMethod As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)   'Set comparison method
    iPos = InStr(1, szMainString, szSubString, iComparisonMethod)
   
    While iPos > 0      'Loop while finding substring
        iCount = iCount + 1
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
    Wend
   
    GetOccurrenceCount = iCount
End Function

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now