Problem with Function used in Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mforema
    New Member
    • May 2007
    • 72

    Problem with Function used in Query

    Hi Everybody,

    I have a function that worked perfectly when it was used in a query. I received help in creating this function from this discussion: Help with Query and Subform - post #18

    However, I had to change the Data Type of one of my fields ("ArticleID" ) from a number to text. Now, the query doesn't work, and I know it's because of the change that I made. The original code and sql are as follows:

    Code:
    Public Function KeyWordsInStr(ByVal strKeyWords As String, _
                                  ByVal lngArticleID As long) As Boolean
        Dim intPos As Integer
        Dim strKeyWord As String
         
        KeyWordsInStr = False
         
        Do
            intPos = InStr(1, strKeyWords, ",")
            If intPos = 0 Then
                strKeyWord = Trim(strKeyWords)
            Else
                strKeyWord = Trim(Left(strKeyWords, intPos - 1))
                strKeyWords = Trim(Mid(strKeyWords, intPos + 1))
            End If
            If IsNull(DLookup("ArticleID", "tblArticleKeyword", _
                "ArticleID=" & lngArticleID & " AND Keyword Like '*" & _
                strKeyWord & "*'")) Then
                Exit Function
            End If
        Loop Until intPos = 0
        
        KeyWordsInStr = True
         
    End Function
    Code:(sql)
    SELECT tblLiteratureAr ticles.*
    FROM tblLiteratureAr ticles
    WHERE KeyWordsInStr(F orms!Form1!txtK eywords,tblLite ratureArticles! ArticleID)

    I knew that I would need to change the data type for the variable lngArticleID from 'long' to 'string.' So, I did, as shown below:

    Code:
    Public Function KeyWordsInStr(ByVal strKeyWords As String, _
                                  ByVal strArticleID As String) As Boolean
        Dim intPos As Integer
        Dim strKeyWord As String
         
        KeyWordsInStr = False
         
        Do
            intPos = InStr(1, strKeyWords, ",")
            If intPos = 0 Then
                strKeyWord = Trim(strKeyWords)
            Else
                strKeyWord = Trim(Left(strKeyWords, intPos - 1))
                strKeyWords = Trim(Mid(strKeyWords, intPos + 1))
            End If
            If IsNull(DLookup("ArticleID", "tblArticleKeyword", _
                "ArticleID=" & strArticleID & " AND Keyword Like '*" & _
                strKeyWord & "*'")) Then
                Exit Function
            End If
        Loop Until intPos = 0
        
        KeyWordsInStr = True
         
    End Function
    Yet, the query still does not work. Can anyone spot the problem?

    Thanks!
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by mforema
    Hi Everybody,

    I have a function that worked perfectly when it was used in a query. I received help in creating this function from this discussion: Help with Query and Subform - post #18

    However, I had to change the Data Type of one of my fields ("ArticleID" ) from a number to text. Now, the query doesn't work, and I know it's because of the change that I made. The original code and sql are as follows:

    Code:
    Public Function KeyWordsInStr(ByVal strKeyWords As String, _
                                  ByVal lngArticleID As long) As Boolean
        Dim intPos As Integer
        Dim strKeyWord As String
         
        KeyWordsInStr = False
         
        Do
            intPos = InStr(1, strKeyWords, ",")
            If intPos = 0 Then
                strKeyWord = Trim(strKeyWords)
            Else
                strKeyWord = Trim(Left(strKeyWords, intPos - 1))
                strKeyWords = Trim(Mid(strKeyWords, intPos + 1))
            End If
            If IsNull(DLookup("ArticleID", "tblArticleKeyword", _
                "ArticleID=" & lngArticleID & " AND Keyword Like '*" & _
                strKeyWord & "*'")) Then
                Exit Function
            End If
        Loop Until intPos = 0
        
        KeyWordsInStr = True
         
    End Function
    Code:(sql)
    SELECT tblLiteratureAr ticles.*
    FROM tblLiteratureAr ticles
    WHERE KeyWordsInStr(F orms!Form1!txtK eywords,tblLite ratureArticles! ArticleID)

    I knew that I would need to change the data type for the variable lngArticleID from 'long' to 'string.' So, I did, as shown below:

    Code:
    Public Function KeyWordsInStr(ByVal strKeyWords As String, _
                                  ByVal strArticleID As String) As Boolean
        Dim intPos As Integer
        Dim strKeyWord As String
         
        KeyWordsInStr = False
         
        Do
            intPos = InStr(1, strKeyWords, ",")
            If intPos = 0 Then
                strKeyWord = Trim(strKeyWords)
            Else
                strKeyWord = Trim(Left(strKeyWords, intPos - 1))
                strKeyWords = Trim(Mid(strKeyWords, intPos + 1))
            End If
            If IsNull(DLookup("ArticleID", "tblArticleKeyword", _
                "ArticleID=" & strArticleID & " AND Keyword Like '*" & _
                strKeyWord & "*'")) Then
                Exit Function
            End If
        Loop Until intPos = 0
        
        KeyWordsInStr = True
         
    End Function
    Yet, the query still does not work. Can anyone spot the problem?

    Thanks!
    Try the change in Line #17 where strArticleID is Delimited by Single Quotes:
    Code:
    Public Function KeyWordsInStr(ByVal strKeyWords As String, _
                                  ByVal strArticleID As String) As Boolean
        Dim intPos As Integer
        Dim strKeyWord As String
         
        KeyWordsInStr = False
         
        Do
            intPos = InStr(1, strKeyWords, ",")
            If intPos = 0 Then
                strKeyWord = Trim(strKeyWords)
            Else
                strKeyWord = Trim(Left(strKeyWords, intPos - 1))
                strKeyWords = Trim(Mid(strKeyWords, intPos + 1))
            End If
            If IsNull(DLookup("ArticleID", "tblArticleKeyword", _
                "ArticleID='" & strArticleID & "' AND Keyword Like '*" & _
                strKeyWord & "*'")) Then
                Exit Function
            End If
        Loop Until intPos = 0
        
        KeyWordsInStr = True
         
    End Function

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      You might also want to take a look at this tutorial by our Forum Leader, NeoPa. He explains the mysteries of the single and double quotes, when and how to use each!



      Linq ;0)>

      Comment

      • mforema
        New Member
        • May 2007
        • 72

        #4
        Originally posted by ADezii
        Try the change in Line #17 where strArticleID is Delimited by Single Quotes:
        Code:
        Public Function KeyWordsInStr(ByVal strKeyWords As String, _
                                      ByVal strArticleID As String) As Boolean
            Dim intPos As Integer
            Dim strKeyWord As String
             
            KeyWordsInStr = False
             
            Do
                intPos = InStr(1, strKeyWords, ",")
                If intPos = 0 Then
                    strKeyWord = Trim(strKeyWords)
                Else
                    strKeyWord = Trim(Left(strKeyWords, intPos - 1))
                    strKeyWords = Trim(Mid(strKeyWords, intPos + 1))
                End If
                If IsNull(DLookup("ArticleID", "tblArticleKeyword", _
                    "ArticleID='" & strArticleID & "' AND Keyword Like '*" & _
                    strKeyWord & "*'")) Then
                    Exit Function
                End If
            Loop Until intPos = 0
            
            KeyWordsInStr = True
             
        End Function
        Thank you! It worked perfectly!

        Comment

        • mforema
          New Member
          • May 2007
          • 72

          #5
          Originally posted by missinglinq
          You might also want to take a look at this tutorial by our Forum Leader, NeoPa. He explains the mysteries of the single and double quotes, when and how to use each!



          Linq ;0)>
          Thanks! The link was helpful!

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            There's a lot of good stuff up in the attic!

            ;0)>

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Originally posted by mforema
              Thank you! It worked perfectly!
              You are quite welcome.

              Comment

              Working...