Cannot get InStr to work in VB 6 program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Yab
    New Member
    • Jul 2012
    • 10

    Cannot get InStr to work in VB 6 program

    I have vb 6 32-bit running on Vista 64-bit Premium Home. I am writing a Music Library program for my collection of Music CDs. Although I have used InStr in other appications and it worked, it will not in this app. I have a string of Artists in the database. I want select one of the string that is not first and print it to the MSFlexGrid.
    strArtistString = rstMusic![Artist}
    iLoc = InStr(1, strArtistString , txtSeeker.Text)
    where
    strArtistString = Eeny, Meeny, Miny, Moe
    txtSeeker.Text = Moe
    For some reason, iLoc never advances. With any of the names that are NOT first, the routine passes over and produces a "Artist Not Found". If Eeny is entered, then each search will go to the MSFlexGrid. I tried creating a function to see if that would help - it did not.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    is the value in "strArtistStrin g" OK?
    Because iLoc = InStr("Eeny, Meeny, Miny, Moe", "Moe")
    gives me iLoc= 20
    PS: no need to use the 1 in Instr(1,...) when you start at the first letter !

    Comment

    • Rodney Roe
      New Member
      • Oct 2010
      • 61

      #3
      Have you tried

      Code:
      instr(1,strArtistString,cstr(txtSeeker))

      Comment

      • Jim Yab
        New Member
        • Jul 2012
        • 10

        #4
        Originally posted by Guido Geurs
        is the value in "strArtistStrin g" OK?
        Because iLoc = InStr("Eeny, Meeny, Miny, Moe", "Moe")
        gives me iLoc= 20
        PS: no need to use the 1 in Instr(1,...) when you start at the first letter !
        Unfortunately, that has worked in other situations but not this one. I suspect that either the database or the MSFlexGrid may have something to do with it not working.
        I have tried:
        iLoc = InStr(1, strArtistString , txtSeeker.Text, vbTextCompare)
        iLoc = InStr(1, strArtistString , txtSeeker.Text, vbBinaryCompare )
        iLoc = InStr(1, strArtistString , txtSeeker.Text, vbDataBaseCompa re)
        The same variations with the following variable changes:
        iLoc = InStr(strArtist String, txtSeeker.Text)
        iLoc = InStr(1, (rstMusic![Artist]), txtSeeker.Text, vbTextCompare)
        I have used the InStr before and it worked flawlessly. For some reason it will not count the letters in the string this application. It will only work for the very first name. I am missing something and have run out of ideas.
        Last edited by Jim Yab; Jul 24 '12, 02:53 PM. Reason: Add statement

        Comment

        • Jim Yab
          New Member
          • Jul 2012
          • 10

          #5
          Originally posted by Rodney Roe
          Have you tried

          Code:
          instr(1,strArtistString,cstr(txtSeeker))
          That did not work either. I am still open to other suggestions.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Try it without any dynamic values and see if that works as expected.

            Comment

            • Rodney Roe
              New Member
              • Oct 2010
              • 61

              #7
              Have you stepped through your program line by line to see if that is the actual problem.

              Comment

              • Jim Yab
                New Member
                • Jul 2012
                • 10

                #8
                This sort of worked:
                Set rstMusic = frmMain.gdbMySo ngs.OpenRecords et(strSQL)

                iIndex = 1
                If rstMusic.Record Count = 0 Then
                MsgBox "No Albums found", vbOKOnly, "Search - Album"
                Else
                rstMusic.MoveLa st
                rstMusic.MoveFi rst
                Me.MSFlexGrid1. Rows = rstMusic.Record Count + 1

                'replace quotation (") with apostrophe (')using:
                'Replace(strAlb umP, Chr$(34), Chr$(39))

                Do While Not rstMusic.EOF

                '### this If-Then is to test of the InStr works - so far it does NOT work
                strArtistString = "Tom, Dick, Harry" '(rstMusic![Artist])
                If InStr(1, strArtistString , txtSeeker.Text, vbTextCompare) > 1 Then
                Debug.Print InStr(1, strArtistString , txtSeeker.Text, vbTextCompare)
                Stop
                End If

                With MSFlexGrid1

                .TextMatrix(iIn dex, 1) = Format(iIndex, " 0000#")
                .TextMatrix(iIn dex, 2) = Format(rstMusic ![SongID], " 0000#")
                .TextMatrix(iIn dex, 3) = Replace(RTrim(r stMusic![album]), _
                Chr$(34), Chr$(39))
                .TextMatrix(iIn dex, 4) = Replace(RTrim(r stMusic![Artist]), _
                Chr$(34), Chr$(39))
                .TextMatrix(iIn dex, 5) = Replace(RTrim(r stMusic![Song]), _
                Chr$(34), Chr$(39))
                .TextMatrix(iIn dex, 6) = Format(rstMusic ![SongID], " 0000#")
                .TextMatrix(iIn dex, 7) = " " & RTrim(rstMusic![Convert])

                I entered "Harry" and got the same "No Album Found" without stopping at the debug.print.
                Then I tried "Har" because I had two known artists in the DB that began with Har.
                This time it stopped at the debug and printed 12 which is correct. Continuing the program resulted in the two artist names appearing in the grid. These were single names and not a series of names in the string.
                Now I know the InStr is working, but something else is creating the failure. Why did the debug not stop with Harry? In the DB, I have a vocalist named "Harris" as the second name in the artist string. The code ignores that when I entered Har. That is what I want to find with this routine.

                Comment

                • Jim Yab
                  New Member
                  • Jul 2012
                  • 10

                  #9
                  After much trial and error, it occurred to me that the solution was with my SQL statement and not using the InStr at all. For your information, this line in my SQL statement is the answer to my problem:
                  strSQL = strSQL & "LIKE '*" & txtSeeker.Text & "*' "
                  Please note the asterisks and their location. Initially, I had the one following txtSeeker.Text. Today I added the one following LIKE and the problem was history. Thank you all for taking time to work with me.

                  Comment

                  Working...