Results 1 to 6 of 6
  1. #1
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87

    FindFirst with multiple fields in criteria

    Hi,
    Whats wrong with my code?


    I want to check if a record exists that has the same coName,InvType,InvDate & Biweekly value. If it does then I'll go ahead and delete it, if it doesn't then it doesn't exist.

    Code:
    Function DltInvoice(CoName As Integer, InvType As Integer, InvDate As Date, Biweekly As String)
     
    Dim logRec As Recordset
    Set logRec = CurrentDb.OpenRecordset("logImportedFiles")
    Dim dbs As Database
    Set dbs = CurrentDb
    
    strMsg = "This invoice does not exist, please check again"
    
    logRec.FindFirst ("[CompanyID] = " & CoName & " AND [InvTypeID] = " & InvName & " AND [InvoiceDate] = " & InvDate & " AND [Biweekly] = " & Biweekly & "")
    If logRec.NoMatch Then
        MsgBox strMsg
    Else: dbs.Execute "qryDLTInvoice"
    End If
    
    End Function

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You're treating every field as numeric; are they all? As a general rule, text fields need to be surrounded by a single quote, and date fields by #. Though doing a different thing, this demonstrates the syntax:

    BaldyWeb wherecondition

    A more detailed explanation, again not doing the same thing but again the syntax regarding delimiters is the same:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    I'm getting an error: Operation is not supported for this type of object
    & it highlights logRec.FindFirst strCriteria

    CoName, InvType are Integers
    InvDate is a Date value
    Biweekly is a String

    I'm trying to do it a few at a time now, and the two integers are not working

    Code:
    Function DltInvoice(CoName As Integer, InvType As Integer, InvDate As Date, Biweekly As String)
     
    Dim logRec As Recordset
    Dim strCriteria As String
    Set logRec = CurrentDb.OpenRecordset("logImportedFiles")
    Dim dbs As Database
    Set dbs = CurrentDb
     
    strCriteria = "([CompanyID] = " & CoName & ") AND ([InvTypeID] = " & InvType & ")"
    strMsg = "This invoice does not exist, please check again"
    
    logRec.FindFirst strCriteria
    If logRec.NoMatch Then
        MsgBox strMsg
    Else: dbs.Execute "qryDLTInvoice"
    End If
    
    End Function

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Define "not working"? Are you getting an error, if so which one? Wrong result?

    A couple of general points. You should disambiguate your declarations of the recordset and database, and both should be closed or set to nothing at the end. Both points demonstrated here:

    http://www.baldyweb.com/ErrorTrap.htm

    Also, it would be much more efficient to open the recordset on an SQL statement that included a WHERE clause like your FindFirst criteria. Then you test for EOF. The way you're doing it, the entire table gets brought over the wire. This type of thing:
    Code:
      strSQL = "SELECT Zone FROM tblLabels WHERE CarNum = " & lngCarNum
      Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
    
      If Not rs.EOF Then
        'there was at least one record
      Else
        'no records
      End If
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    compooper is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2011
    Posts
    87
    Thanks so much! Figured it out using SQL statements like your mentioned

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Ah good, happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-13-2011, 11:00 AM
  2. Search By Criteria - Flexible Criteria Fields
    By lilanngel in forum Access
    Replies: 0
    Last Post: 03-16-2011, 06:25 PM
  3. using FindFirst with a Date Criteria (syntax error)
    By ajetrumpet in forum Programming
    Replies: 2
    Last Post: 09-10-2010, 11:01 PM
  4. RST Findfirst date problem
    By ColPat in forum Programming
    Replies: 3
    Last Post: 08-09-2010, 07:28 AM
  5. Recordset Findfirst Problem
    By ColPat in forum Programming
    Replies: 6
    Last Post: 07-22-2010, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums