Results 1 to 10 of 10
  1. #1
    George is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    295

    Recordset Filtering

    Good Morning,



    The following code is used to filter a table called "Class", however a runtime error 3251 returns, stating "Operation is not supported by this type of object. what can be the problem?

    Private Sub Command0_Click()
    Dim curDatabase As Database
    Set curDatabase = CurrentDb

    Dim rst As Recordset

    Set rst = CurrentDb.OpenRecordset("Class")
    rst.Filter = "Year = 2011" ' The problem is here.

    MsgBox rst.RecordCount

    Set rst = Nothing
    End Sub

  2. #2
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    You might explore the DCount method as an approach.

    HTH

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You should really disambiguate your variables:
    Dim curDatabase As DAO.Database
    Set curDatabase = CurrentDb
    Dim rst As DAO.Recordset

    And then you have the problem that Year is a reserved word in VBA.
    http://www.allenbrowne.com/AppIssueBadWord.html#Y

    You might try brackets to get around the issue:
    rst.Filter = "[Year] = 2011"

    Do you really have a field named YEAR? Is it Numeric or a String?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I'm going to take a flyer and say the REAL problem is:
    From VBA Help on Filter:
    Use the Filter property to apply a filter to a dynaset-, snapshot-, or forward-only–type Recordset object.

    The default for OpenRecordset if nothing is specified is a Table so it probably does not like the Filter.

    Change your code to:
    Set rst = CurrentDb.OpenRecordset("Class", dbOpenDynaset)
    ...and I suspect the error will go away.

  5. #5
    George is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    295

    Thanks for all the replys. They helped abit but other problems arise as i advanced.

    This is my updated code. My new problem is that the Rs2.RecordCcount is giving a number of 1 when there are may more records in this filter. The Rs1.RecordCount is now working fine. Can the reason for this problem be explained?

    Private Sub Command0_Click()
    Dim db As Dao.Database, rs1 As Dao.Recordset
    Dim rs2 As Dao.Recordset

    Set db = currentDb
    Set rs1 = db.OpenRecordset("Select * from Class", dbOpenDynaset)

    rs1.Filter = "TheYear = 2008 "
    Set rs2 = rs1.OpenRecordset

    MsgBox rs1.RecordCount
    MsgBox rs2.RecordCount

    Set rs2 = Nothing: Set rs1 = Nothing
    Set db = Nothing

    End Sub

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    RecordCount is not guaranteed accurate until after you have done a .MoveLast.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    When you first open a Recordset, you are at the beginning and if there are *any* records then .RecordCount will be non zero. If the RecordCount is important to you then it is an excellent time to do a .MoveLast and then a .MoveFirst.

  8. #8
    George is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    295

    Recordset filtering problem is resolved

    Thanks to you experts my problem is solved by the movelast and Movefirst Methods

  9. #9
    George is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    295
    I am grateful to you. My problem is resolved

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad we could help. I marked this thread Solved for you.

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

Similar Threads

  1. How to Select second Recordset from first recordset
    By FrustratedAlso in forum Programming
    Replies: 28
    Last Post: 05-10-2012, 05:45 PM
  2. Replies: 2
    Last Post: 03-08-2012, 12:59 PM
  3. Recordset
    By Rick West in forum Programming
    Replies: 7
    Last Post: 11-14-2011, 02:40 PM
  4. create table filtering a recordset
    By JJCHCK in forum Programming
    Replies: 5
    Last Post: 09-27-2011, 01:11 AM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 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