?
Solved

Accessing parts of text in text boxes (70pts)

Posted on 1998-12-11
3
Medium Priority
?
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Expert Comment

by:lankford
ID: 1449588
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
ID: 1449589
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 280 total points
ID: 1449590
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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