Question : DoCmd.openquery

Hi there.  Is it possible to use DoCmd.openquery as a function.

Answer : DoCmd.openquery

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
Random Solutions  
 
programming4us programming4us