Using AND operator with FindFirst method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waltvw
    New Member
    • Jun 2007
    • 7

    Using AND operator with FindFirst method

    I'm using FindFirst method in Access VBA to find a particular record in a recordset. I have 2 search criteria each of which works just fine if used separately as an argument in FindFirst, but NOT in combination with one another. I tried various combinations of quotes, double quotes, brackets and paranthesis but in case of using 2 conditions I'm still receiving the error message saying "run-time error 13 - type mismatch". If I use just one of them then there are no error codes, so it looks like I simply can't remember the right syntax, i.e. there aren't, in fact, any data type mismatches.

    This is the code that doesn't work:

    Set rst = dbs.OpenRecords et("tblCustomer ", dbOpenDynaset)
    With rst
    .FindFirst "[CustID] = ' " & rstOtherTBL!Cus tID & " ' " And _
    "[Date] = #" & StartDate & "# "
    'Other Statements
    End With

    However, these 2 pieces of code work just fine:

    Set rst = dbs.OpenRecords et("tblCustomer ", dbOpenDynaset)
    With rst
    .FindFirst "[CustID] = ' " & rstOtherTBL!Cus tID & " ' "
    'Other Statements
    End With

    Set rst = dbs.OpenRecords et("tblCustomer ", dbOpenDynaset)
    With rst
    .FindFirst "[Date] = #" & StartDate & "# "
    'Other Statements
    End With

    Anybody knows how to resolve it?

    Thanks a lot in advance!
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Originally posted by waltvw
    I'm using FindFirst method in Access VBA to find a particular record in a recordset. I have 2 search criteria each of which works just fine if used separately as an argument in FindFirst, but NOT in combination with one another. I tried various combinations of quotes, double quotes, brackets and paranthesis but in case of using 2 conditions I'm still receiving the error message saying "run-time error 13 - type mismatch". If I use just one of them then there are no error codes, so it looks like I simply can't remember the right syntax, i.e. there aren't, in fact, any data type mismatches.

    This is the code that doesn't work:

    Set rst = dbs.OpenRecords et("tblCustomer ", dbOpenDynaset)
    With rst
    .FindFirst "[CustID] = ' " & rstOtherTBL!Cus tID & " ' " And _
    "[Date] = #" & StartDate & "# "
    'Other Statements
    End With

    However, these 2 pieces of code work just fine:

    Set rst = dbs.OpenRecords et("tblCustomer ", dbOpenDynaset)
    With rst
    .FindFirst "[CustID] = ' " & rstOtherTBL!Cus tID & " ' "
    'Other Statements
    End With

    Set rst = dbs.OpenRecords et("tblCustomer ", dbOpenDynaset)
    With rst
    .FindFirst "[Date] = #" & StartDate & "# "
    'Other Statements
    End With

    Anybody knows how to resolve it?

    Thanks a lot in advance!
    Try this:
    Code:
    .FindFirst "[CustID] = ' " & rstOtherTBL!CustID & " ' "  _
    & "And [Date] = #" & StartDate & "# "

    Comment

    • puppydogbuddy
      Recognized Expert Top Contributor
      • May 2007
      • 1923

      #3
      Originally posted by puppydogbuddy
      Try this:
      Code:
      .FindFirst "[CustID] = ' " & rstOtherTBL!CustID & " ' "  _
      & "And [Date] = #" & StartDate & "# "
      oops! forgot a space before the And:
      Code:
      .FindFirst "[CustID] = ' " & rstOtherTBL!CustID & " ' "  _
      & " And [Date] = #" & StartDate & "# "

      Comment

      • waltvw
        New Member
        • Jun 2007
        • 7

        #4
        Thanks a lot, it now works!

        Comment

        • puppydogbuddy
          Recognized Expert Top Contributor
          • May 2007
          • 1923

          #5
          Originally posted by waltvw
          Thanks a lot, it now works!
          You are most welcome. Glad I could help.

          Comment

          • waltvw
            New Member
            • Jun 2007
            • 7

            #6
            It works but I just discovered that either Date or CustID is not taken into consideration in the evaluation, i.e. only one condition can actually be used in FindFirst as opposed to "Where" clause in SQL statement that can use multiple AND's & OR's. This is because my next statement, If .NoMatch evaluates to True even if there are records in the table that has a combination of CustID and Date which have been evaluated by .FindFirst statement. If that's the case, I'm still OK, 'cause before I got your response on how to fix the syntax in question I had created some logic in the loop to get around the issue.

            Thanks again!

            Comment

            • puppydogbuddy
              Recognized Expert Top Contributor
              • May 2007
              • 1923

              #7
              Originally posted by waltvw
              It works but I just discovered that either Date or CustID is not taken into consideration in the evaluation, i.e. only one condition can actually be used in FindFirst as opposed to "Where" clause in SQL statement that can use multiple AND's & OR's. This is because my next statement, If .NoMatch evaluates to True even if there are records in the table that has a combination of CustID and Date which have been evaluated by .FindFirst statement. If that's the case, I'm still OK, 'cause before I got your response on how to fix the syntax in question I had created some logic in the loop to get around the issue.

              Thanks again!
              To the best of my knowledge, the FindFirst method can have compound conditions. If .NoMatch is not working, it might be because the wrong syntax is being used for the data type.

              For example, the syntax of your FindFirst statement presents CustID as a text data type:
              Code:
              FindFirst "[CustID] = ' " & rstOtherTBL!CustID & " ' "  _
              & " And [Date] = #" & StartDate & "# "
              If CustID is a numeric data type, the syntax should have been as follows:
              Code:
              FindFirst "[CustID] = " & rstOtherTBL!CustID  _
              & " And [Date] = #" & StartDate & "# "
              If syntax of the CustID is not the problem, let me know and I will look into the FindFirst method. .

              Comment

              • puppydogbuddy
                Recognized Expert Top Contributor
                • May 2007
                • 1923

                #8
                Here is a link to another thread on this Forum that supports the fact that the FindFirst can be used with 2 variables. See response from Allen Browne, Microsoft MVP Expert.

                Comment

                Working...