Results 1 to 6 of 6

Thread: Setting recordset to query in Access VBA [Solved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Setting recordset to query in Access VBA [Solved]

    hey I am trying to set a recordset to a query in my database so I can use the .findfirst function. I keep getting a error saying that the item is not found in the collection.

    Is this possible or do I have to use a table instead of a query for my recordset?

    the code I am using is:

    VB Code:
    1. Dim rcQuery1 as Recordset
    2.  
    3. Set rcQuery1 = Recordset("Query1")

    I am getting stuck on the set statement.

    Thanks for any help.
    Last edited by Besoup; Mar 3rd, 2005 at 02:21 PM.

  2. #2
    Junior Member
    Join Date
    Mar 2005
    Location
    Kaiserslautern, Germany
    Posts
    25

    Re: Setting recordset to query in Access VBA

    Hi....

    the easiest way to do this is to refer the Microsoft DAO 3.6 Object Library (if you use at least Acess 2000) and work with the DAO objects.

    VB Code:
    1. Dim db As DAO.DataBase
    2. Dim rs as DAO.Recordset
    3. Set db = CurrentDB()
    4. Set rs = db.OpenRecordSet("Query1")
    5. While Not rs.Eof
    6.   ' Do what has to be done ;-)
    7.   rs.MoveNext
    8. Wend
    9. Set rs = Nothing

    if you only want to execute the query:
    VB Code:
    1. Dim rs As ADODB.Recordset
    2. Set rs = CurrentProject.Connection.Execute("select * from Query1")
    3. While Not rs.Eof
    4.   ' Do what has to be done ;-)
    5.   rs.MoveNext
    6. Wend
    7. Set rs = Nothing
    Last edited by ivobenkovic; Mar 3rd, 2005 at 01:59 PM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Setting recordset to query in Access VBA

    Ty

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Setting recordset to query in Access VBA [UnSolved]

    Actually I thought that was gonna work but now I am getting a too few parameters error with the following code:

    VB Code:
    1. Dim db As DAO.DataBase
    2. Dim rs as DAO.Recordset
    3. Set db = CurrentDB()
    4. Set rs = db.OpenRecordSet("Query1")

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Setting recordset to query in Access VBA [UnSolved]

    Got it now.... me dumb. Thanks for the help.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Setting recordset to query in Access VBA [UnSolved]

    The OpenRecordSet method takes parameters in addition to the query.
    VB Code:
    1. Private Sub OpenMe()
    2.     Dim rs As Recordset
    3.     Set rs = CurrentDb.OpenRecordset("Query1", dbOpenDynamic, dbReadOnly, dbOptimistic)
    4. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width