Results 1 to 4 of 4
  1. #1
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31

    Filtering OpenRecordset Form reference error

    I'm attempting to populate a textbox with a field from a query based on where the ContactID matches the ContactID of a specific open form and where another field value in the query (called Default) is True. I'm using the .Filter of the OpenRecordset.

    I'm getting the error: "3061: Too few parameters. Expected 3."

    As you can see by all the commented out code, I've tried many ways to get it to work. Based on the MsgBox results the filter looks correct.

    So for instance:
    This works
    rs.Filter = "ContactName = 'Cory Goulden'" & " AND Default=True" 'WORKS

    This does not:


    rs.Filter = "ContactID = [Forms]![frmCompanyContacts]![txtContactID] AND Default=True"

    Code:
            Dim db As Database
            Dim rs As Recordset
            Dim rsFiltered As Recordset
            Dim strDefaultAddr As String
            
            strDefaultAddr = "SELECT DISTINCTROW [tblClients/Prospects].CompName, tblCompanyContacts.ContactName, tblContactAddresses.Attn, IIf(IsNull([Addr2]),[Addr1],[Addr1] & ', ' & [Addr2]) AS Street, tblContactAddresses.Zip, tblContactAddresses.City, tblContactAddresses.State, tblContactAddresses.Default " & _
                            "FROM ([tblClients/Prospects] INNER JOIN tblCompanyContacts ON [tblClients/Prospects].CompID = tblCompanyContacts.CompID) INNER JOIN tblContactAddresses ON tblCompanyContacts.ContactID = tblContactAddresses.ContactID " & _
                            "ORDER BY [tblClients/Prospects].CompName, tblCompanyContacts.ContactName;"
            
            Dim strWhere As String
            strWhere = "[Forms]![frmCompanyContacts]![txtContactID]"
            
            Set db = CurrentDb
            Set rs = db.OpenRecordset(strDefaultAddr, dbOpenDynaset)
                rs.MoveLast
                MsgBox [Forms]![frmCompanyContacts]![txtContactID] & ", " & [Forms]![frmCompanyContacts]![txtContactName] & ", " & Default
                'rs.Filter = "ContactName = 'Cory Goulden'" & " AND Default=True"    'WORKS
                'rs.Filter = "ContactID = '" & strWhere & "'"
                'rs.Filter = "ContactID = " & strWhere
                'rs.Filter = "ContactID = '" & [Forms]![frmCompanyContacts]![txtContactID] & "'"
                rs.Filter = "ContactID = [Forms]![frmCompanyContacts]![txtContactID]"
                'rs.Filter = "ContactID = [Forms]![frmCompanyContacts]![txtContactID]" & " AND Default=True"
                'rs.Filter = "ContactID = '[Forms]![frmCompanyContacts]![txtContactID]'" & " AND Default=True"
                'rs.Filter = "ContactID = [Forms]![frmCompanyContacts]![txtContactID] AND Default=True"
                'MsgBox rs.Filter
            Set rsFiltered = rs.OpenRecordset
                rsFiltered.MoveLast
                    MsgBox rsFiltered.RecordCount
                    Me.txtAddressInfo = rsFiltered("Street")
                    'Me.txtAddressInfo = rs("Street") & ", " & rs("City") & ", " & rs("State") & ", " & rs("Zip")
                rsFiltered.MoveNext
                rs.Close
                rsFiltered.Close
                db.Close
            Set rs = Nothing: Set rsFiltered = Nothing: Set db = Nothing

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,847
    Try (assuming Id is a number)
    rs.Filter = "ContactID = " & [Forms]![frmCompanyContacts]![txtContactID] & " AND Default=True"

    or if ContactId is text

    rs.Filter = "ContactID = '" & [Forms]![frmCompanyContacts]![txtContactID] & "' AND Default=True"

  3. #3
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31
    Thanks for replying with a possible solution.

    I've tried both your recommendations and now I'm getting:
    "3061: Too few parameters. Expected 2."

    So it changed from expected 3 to expected 2.

    BTW, ContactID referenced in the table is an Auto Number Long Integer.

  4. #4
    CaptainKen is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2012
    Location
    Arizona
    Posts
    31

    Solved!

    Well the main problem was that I hadn't included the ContactID field in the main query! Once I did that I was able to figure out the correct syntax to reference the textbox holding the ID. Here's the final code in case it helps someone else.

    Code:
            Dim db As Database
            Dim rs As Recordset
            Dim rsFiltered As Recordset
            Dim strSQL As String
            Dim strAddr As String
            
            'This one assembles the Addr1 and Addr2 in the query
            'strSQL = "SELECT DISTINCTROW [tblClients/Prospects].CompName, tblCompanyContacts.ContactName, tblCompanyContacts.ContactID, tblContactAddresses.Attn, IIf(IsNull([Addr2]),[Addr1],[Addr1] & ', ' & [Addr2]) AS Street, tblContactAddresses.Zip, tblContactAddresses.City, tblContactAddresses.State, tblContactAddresses.Default " & _
                        "FROM ([tblClients/Prospects] INNER JOIN tblCompanyContacts ON [tblClients/Prospects].CompID = tblCompanyContacts.CompID) INNER JOIN tblContactAddresses ON tblCompanyContacts.ContactID = tblContactAddresses.ContactID " & _
                        "ORDER BY [tblClients/Prospects].CompName, tblCompanyContacts.ContactName;"
            
            ' This one assembles the Addr1 and Addr2 later on
            strSQL = "SELECT DISTINCTROW [tblClients/Prospects].CompName, tblCompanyContacts.ContactName, tblCompanyContacts.ContactID, tblContactAddresses.Attn, tblContactAddresses.Addr1, tblContactAddresses.Addr2, tblContactAddresses.Zip, tblContactAddresses.City, tblContactAddresses.State, tblContactAddresses.Default " & _
                        "FROM ([tblClients/Prospects] INNER JOIN tblCompanyContacts ON [tblClients/Prospects].CompID = tblCompanyContacts.CompID) INNER JOIN tblContactAddresses ON tblCompanyContacts.ContactID = tblContactAddresses.ContactID " & _
                        "ORDER BY [tblClients/Prospects].CompName, tblCompanyContacts.ContactName;"
    
            Set db = CurrentDb
            Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
                rs.MoveLast
                rs.Filter = "ContactID = " & [Forms]![frmCompanyContacts]![txtContactID] & " AND Default=True"
            
            Set rsFiltered = rs.OpenRecordset
                rsFiltered.MoveLast
                
                    If rsFiltered("Addr2") > "" Then
                        strAddr = rsFiltered("Addr1") & ", " & rsFiltered("Addr2") & ", " & rsFiltered("City") & ", " & rsFiltered("State") & ", " & rsFiltered("Zip")
                    Else
                        strAddr = rsFiltered("Addr1") & ", " & rsFiltered("City") & ", " & rsFiltered("State") & ", " & rsFiltered("Zip")
                    End If
            
                    Me.txtAddressInfo = strAddr
                
                rsFiltered.MoveNext
                rs.Close
                rsFiltered.Close
                db.Close
            Set rs = Nothing: Set rsFiltered = Nothing: Set db = Nothing

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

Similar Threads

  1. db.OpenRecordset error
    By jscriptor09 in forum Programming
    Replies: 4
    Last Post: 01-27-2012, 12:17 AM
  2. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  3. Docmd.form cause reference error
    By snoopy2003 in forum Programming
    Replies: 0
    Last Post: 03-10-2011, 10:49 AM
  4. Filtering Dates Error!
    By emilyrogers in forum Forms
    Replies: 3
    Last Post: 02-15-2011, 03:00 AM
  5. Expression builder error, circular reference
    By cowboy in forum Programming
    Replies: 3
    Last Post: 07-15-2010, 12:55 PM

Tags for this Thread

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