Posted on 2010-01-02
Last Modified: 2013-11-28
Hi there.  Is it possible to use DoCmd.openquery as a function.
Question by:leahgillon
    LVL 22

    Expert Comment

    by:Kelvin Sparks
    You can create a function which uses that.
    LVL 3

    Expert Comment


    Author Comment

    These examples seem to use openquery as a sub, not a function.  I'm thinking that perhaps it can't be used as a function to return a recordset and that I may have to use DAO.  What do you think?  Thanks.
    LVL 7

    Accepted Solution

    Hi leahgillon,

    DoCmd.OpenQuery method will just open query in say datasheet view. To return recordset use type of:

    Function fOpenQuery(QueryName As String) As DAO.Recordset
    Set fOpenQuery = CurrentDb.QueryDefs(QueryName).OpenRecordset
    End Function

    The following will print ID field of the first record of Query1 query in immediate window

    Sub sTest()
    Debug.Print fOpenQuery("Query1")!ID
    End Sub

    Hope this helps

    Author Comment

    We ended up going a different route but this was very helpful in clarifying our thinking.  Thanks!
    LVL 7

    Expert Comment

    You are welcome and thank you for the points! :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now