Left$ Function, Looping through

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    Left$ Function, Looping through

    Hiya, Partners!

    I have been into it for 12 hours straight this week-end, my son is very unhappy. Looks like I am getting pretty close but need your help, Again. I will post my first Option/Solution, then the next through another post. Here it goes:

    'PROPOSED SOLUTION 1
    'Button below formats words in one column, the words however, are all
    over the place
    'I attemepted to use vbCrlf to enable LineFeed, no joy there.
    'Well it gets me to the next line, the words are broken in pieces.
    'The Big idea I have is to later have the application look through
    Text6.Text using a simple function
    'that'll grab exactly the words I want, thus 'GiveMeIt= Asc("Rain") something in that kind of a line, but at least what I want :-)
    Code:
    Private Sub ClonegetLength_Click()
    Open App.Path + "\Test.txt" For Output As #1 
    'file is opened as in Print to get rid of quotes, just learned this :-)
    Print #1, Text6.Text 
    'this should hold enough text, wouldn't you say?
     
    Close #1 'close, then reopen below
     
    Dim strGetWords As String
     
     
    FileName = "\Test.txt" 'here is our fancy text file again
    f = FreeFile
    Open App.Path + FileName For Input As #1
    'this time we want to, not only see what's inside, we will ATTEMPT to 
    format
     
    Do While Not EOF(f) 'all the way 'til we're happy
    Line Input #1, strGetWords
     
    i = False 'wonderful looping mechanism, allows us to chop it up and 
    fromat,
    'set to false (Be sure to do this!)
     
    Do Until i = True
     
    intLength = Len(strGetWords) ' the first time in we read the whole 
    piece
    If intLength <= 8 Then 'here is the trick I have been talking about, 
    I wanted to add
    'to give it the highest length specific to words the if statement 
    will hope to grab
    'as one example, database. But ladies and gentlemen, my format will 
    attempt to break down
    'database depending on its position. For now we'll move on, we'll 
    get to that later
    Exit Do 'I got what I wanted, 8 chars for database
    End If
    strBringIn = strBringIn & Left(strGetWords, 8) & vbCrLf
    'I am reading left here to make sure I get Train, not rain
    strGetWords = Right(strGetWords, intLength - 8)
    ' I also want to read right to allow others words to be tucked under
    'previous words, having 10 chars
    Loop 'do until Loop ends here
     
    Text6.Text = strBringIn 'add the formatted results in
    Loop ' found end of file, may or may not search again, provided we're 
    happy...
     
    End Sub 'that's it for us...Below are the results of our positioning of 
    these few words
    'FIRST THE TEXT ADDED IN Text6.Text, before ClonegetLength is pressed:

    'This is working, but I will need to also tell VB the words I do not
    need it to find, thus busted.
    'A wildcard, if it exists for searching a .txt file should do the
    trick, since searching the actual
    'database "Bus*" does in fact give me "Bus", addded/found from
    different text, typed my son's
    '"the magic school bus" grabbed "the wheels on the bus" as well from my
    database...
    'Oh! here is what my Like operator atttempt loked like: database is
    added here just to see how it's
    'getting picked up elsewhere



    'SECONDLY, HERE IS THE FORMAT RETURED:

    's well f
    'rom my d
    'atabase. (I lost my d here)
    'Oh! here
    ' is what
    ' my Like
    ' operato (there is a space in here, Ltrin will take care of it but the
    least of our problems right now)
    'r atttem
    'pt loked
    ' like:
    'Database (looks good here)
    ' is adde
    'd here j
    'ust to s
    'ee how i
    'getting
    'picked u
    'p elsewh


    'REMARKS: I need my format to read whole words, then drop/tuck them
    under one another to resamble:

    'well
    'from
    'my
    'database.
    'Oh!
    'here
    'is
    'what
    'my
    'Like
    'operator
    'atttempt


    ' I will need to repeat do until to record additional characters I
    want, if I need bus for instance

    'Do Until i = True

    'intLength = Len(strGetWords ) ' the first time in we read the whole
    piece
    'If intLength <= 3 Then 'here is the trick I have been talking
    about, I wanted to add...

    'Exit Do 'I got what I wanted, 8 chars for database
    'End If
    'strBringIn = strBringIn & Left(strGetWord s, 3) & vbCrLf
    'strGetWords = Right(strGetWor ds, intLength - 3)
    'Loop 'do until Loop ends here

    'Text6.Text = strBringIn 'add the formatted results in
    'Loop ' found end of file, may or may not search again, provided we're
    happy...



    In a bit (for Option/Solution 2 forthcoming)
    Last edited by Niheel; Jan 8 '07, 11:47 AM.
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi D, why don't you use Split() ??

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Originally posted by willakawill
      Hi D, why don't you use Split() ??
      Hey Will, Split did not seem to work when I used InstrB:

      Code:
      Line input, strGetWords
      
      GoSplit=Split(strGetWords, " ")
      
      If InstrB(strGetWords, "Rain") <> 0 Then
      
      this and that here...
      
      End If
      when Train was found in the text, rain was pulled up as well :-(

      I thought, forget about it, move on with my life.

      Do you think now that I am adding everything in one column, it might work?

      D

      Comment

      • Dököll
        Recognized Expert Top Contributor
        • Nov 2006
        • 2379

        #4
        Hello again, Will!

        Tried Split with the new code:

        Code:
        Dim GoSplit As Variant
        Dim strGetWords As String
        
        
        FileName = "\Test.txt"  'here is our fancy text file again
        f = FreeFile
        Open App.Path + FileName For Input As #1
        'this time we want to, not only see what's inside, we will ATTEMPT to 
        format
        
        Do While Not EOF(f) 'all the way 'til we're happy
        Line Input #1, strGetWords
        GoSplit=Split(strGetWords, " ")
        
        i = False 'wonderful looping mechanism, allows us to chop it up and 
        fromat,
        'set to false (Be sure to do this!)
        Got nada, same deal...

        Making a mad dash for it, perhaps another 12 hours will do thr trick :-)

        D

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by Dököll
          Hello again, Will!

          Tried Split with the new code:

          Code:
          Dim GoSplit As Variant
          Dim strGetWords As String
          
          
          FileName = "\Test.txt"  'here is our fancy text file again
          f = FreeFile
          Open App.Path + FileName For Input As #1
          'this time we want to, not only see what's inside, we will ATTEMPT to 
          format
          
          Do While Not EOF(f) 'all the way 'til we're happy
          Line Input #1, strGetWords
          GoSplit=Split(strGetWords, " ")
          
          i = False 'wonderful looping mechanism, allows us to chop it up and 
          fromat,
          'set to false (Be sure to do this!)
          Got nada, same deal...

          Making a mad dash for it, perhaps another 12 hours will do thr trick :-)

          D
          Split will give you an array of the words in the string
          You can check each of the elements for the word you are searching with
          Code:
          For lp = 0 To UBound(GoSplit)
          If InstrB(GoSplit(lp), "Rain") <> 0 Then

          Comment

          • Dököll
            Recognized Expert Top Contributor
            • Nov 2006
            • 2379

            #6
            Hey, thanks for your quick response. Is GoSplit a Variant here or a String?

            Code:
            For lp = 0 To UBound(GoSplit)
            If InstrB(GoSplit(lp), "Rain") <> 0 Then
            Dököll

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by Dököll
              Hey, thanks for your quick response. Is GoSplit a Variant here or a String?

              Code:
              For lp = 0 To UBound(GoSplit)
              If InstrB(GoSplit(lp), "Rain") <> 0 Then
              Dököll
              It is a variant that becomes an array of strings

              Comment

              • Dököll
                Recognized Expert Top Contributor
                • Nov 2006
                • 2379

                #8
                Greetings, Will!
                What happens to strGetWords, I cannot get it to work:

                Code:
                Dim GoSplit As Variant
                Dim strGetWords As String
                Dim lp As Integer
                
                
                FileName = "\Test.txt"  'here is our fancy text file again
                f = FreeFile
                Open App.Path + FileName For Input As #1
                'this time we want to, not only see what's inside, we will ATTEMPT to 
                format
                
                Do While Not EOF(f) 'all the way 'til we're happy
                Line Input #1, strGetWords
                GoSplit=Split(strGetWords, " ")
                
                For lp = 0 To UBound(GoSplit)
                If InStrB(GoSplit(lp), "Rain") <> 0 Then
                Text1.Text = GoSplit
                End If
                Next lp
                I get a type mismatched error. If GoSplit has taken over for strGetWords, won't it still grab Rain from Train. strGetWords has already gone into the file to find stuff. Should lp be in the plave of strGetWords as such:

                Code:
                Dim GoSplit As Variant
                Dim lp as Integer
                
                FileName = "\Test.txt"  'here is our fancy text file again
                f = FreeFile
                Open App.Path + FileName For Input As #1
                'this time we want to, not only see what's inside, we will ATTEMPT to 
                format
                
                Do While Not EOF(f) 'all the way 'til we're happy
                Line Input #1, lp
                GoSplit=Split(lp, " ")
                
                For lp = 0 To UBound(GoSplit)
                If InStrB(GoSplit(lp), "Rain") <> 0 Then
                Text1.Text = GoSplit
                End If
                Next lp
                I am up early, perhaps I am missing a beat :-)

                Comment

                • willakawill
                  Top Contributor
                  • Oct 2006
                  • 1646

                  #9
                  Code:
                  Text1.Text = GoSplit(lp)
                  You can also check that length of GoSplit(lp) is the same as the search string length so you will not get Train instead of rain. As you are checking only single words you should use StrComp

                  Code:
                  If StrComp(GoSplit(lp), "rain", 1) = 0 Then
                  'this is an exact match and is not case sensitive
                  End If

                  Comment

                  • Dököll
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 2379

                    #10
                    Will,

                    I think we've got it, just added a note to Killer about this. vbTextCompare is now working. When you mentioned it, I wrote:

                    [CODE]
                    If InStr(strGetWor ds, "use", vbTextCompare) <> 0 Then

                    it did not work, so I gave up...

                    End If

                    [\CODE]

                    It's likely you saw my other post (Vol 2), this is the one that is doing wha I hoped to achieve:

                    Code:
                    Private Sub GtWords_Click()
                    Dim strGetWords As Long
                    strGetWords = InStr([B]1[/B], Text6(0).Text, " " & Text1.Text & " ", vbTextCompare) 
                    If strGetWords = 0 Then
                    Text1.Text = "Steve says no way"
                    Else
                    Text6(0).SetFocus
                    Text6(0).SelStart = strGetWords
                    Text6(0).SelLength = Len(Text1.Text)
                    End If
                    End Sub
                    I appreciate your help and assitance, Will...

                    Stay tuned!

                    Dököll

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Wouldn't this code miss the word if it is at the start or end of the text?

                      If so, I suppose you could fix that by just adding a space to both ends of the text. Hm... this seems as though it would handle the start/end thing, but what about if the word is followed by a comma, for example?

                      I note that you could probably cut out the UCASE$ conversion in my code by using the text compare as you've done here, rather than binary compare.

                      Comment

                      • Dököll
                        Recognized Expert Top Contributor
                        • Nov 2006
                        • 2379

                        #12
                        Indeed, Killer, it would miss anything with a comma but my plan with this version of the code was to use use a variable such as:

                        Code:
                        strGetWords = Asc("Apple") 'would give me 65
                        And when 65 specific to Apple was added in LyricalContentf ind.Text1(0).Te xt, I would revert back its own container, thus
                        LyricalContentf ind.Text1(0).Te xt = "Use"

                        One reason why I attempted to achieve this in a column, reading 3 chars with Left$ and Len. Now, I may need need to implement this elsewhere in the code.


                        Here is what I hoped to achieve:

                        The
                        Perfect
                        way
                        of
                        doing
                        things
                        is
                        to
                        make
                        use
                        of
                        time
                        and
                        not
                        waste


                        Seems as though the code was surely adding my text in one column, but could not get the proper character length:

                        Code:
                        i = False 
                        Do Until i = True
                        intLength = Len(strGetWords)    
                        If intLength <= 8 Then                
                        Exit Do    
                        End If   
                        strBringIn = strBringIn & Left(strGetWords, 8) & vbCrLf
                        strGetWords = Right(strGetWords, intLength - 8)
                        I guess, in the above example, since I have 8 chars to pick from, anything that falls in that category, even though it included a space, would have been picked up (i.e. my wheel, database). If you see anything here, let me know.

                        So far, we're good, a few hours of coding will help complete the current task.

                        Talk soon!

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          I think I had better give up at this point. You seem to have things well in hand with willakawill's help, and I'm now completely lost as to what you're trying to achieve. So I'll just bow out before I embarrass myself (further). :o

                          Good luck with the project! :)

                          Comment

                          • Dököll
                            Recognized Expert Top Contributor
                            • Nov 2006
                            • 2379

                            #14
                            I have been testing all morning long, WillAkaWill, can you point me to an example where BinaryCompare is case sensitive. Would you say LCase/UCase handles case, if BinaryCompare is used. Say it were:

                            Code:
                            strGetWords = InStr(1, " " & LCase(Text6(0).Text) & " ", " " & LCase(Text1.Text) & " ", vbBinaryCompare)
                            Would I be covered?

                            I cannot but go with you on this one since I am not familiar with case sensitiveness in these comparison tactics. I have modified to vbTextCompare and kept two different copies.

                            Will wait your response to give it a go, thanks again...

                            Did you, WillAkaWill, by any chance, check out Aivosto? Do you think they're solid in they optimiztion techniques. I should really be careful wat I am reading.

                            In a bit :-)

                            Comment

                            • willakawill
                              Top Contributor
                              • Oct 2006
                              • 1646

                              #15
                              Originally posted by Dököll
                              I have been testing all morning long, WillAkaWill, can you point me to an example where BinaryCompare is case sensitive. Would you say LCase/UCase handles case, if BinaryCompare is used. Say it were:

                              Code:
                              strGetWords = InStr(1, " " & LCase(Text6(0).Text) & " ", " " & LCase(Text1.Text) & " ", vbBinaryCompare)
                              Would I be covered?

                              I cannot but go with you on this one since I am not familiar with case sensitiveness in these comparison tactics. I have modified to vbTextCompare and kept two different copies.

                              Will wait your response to give it a go, thanks again...

                              Did you, WillAkaWill, by any chance, check out Aivosto? Do you think they're solid in they optimiztion techniques. I should really be careful wat I am reading.

                              In a bit :-)
                              Yes you can do it this way. The notion that you are now 'speeding things up' by using binary comparison is lost on the fact that you have introduced 2 extra string processes to ensure same case.

                              Binary comparison will compare the string byte by byte numerically so it is case sensitive. Text comparison is not case sensitive and has a processing overhead to cover that. Setting the 2 strings to upper or lowercase eliminates any speed benefit so you might as well use text comparison. It reads better :)

                              Comment

                              Working...