The macro code below illustrates how to access the results of an existing query with VBA.

Using the Query ID

Public Sub GetQuery()

Dim oquery As CQuerySet

Set oquery = New CQuerySet
oquery.Init REApplication.SessionContext

Dim sfield As String

sfield = "Name"  'Name of column in query output
oquery.QueryID = 20 'System Record ID of Query

Dim i As Long

For i = 1 To oquery.NumberOfRecords
   Debug.Print oquery.FieldValue(sfield)
Next i


Set oquery = Nothing

End Sub

Using the Query Name

Public Sub GetQueryByName()

'Reads data results from a saved RE Query called marc_test

Dim oQuery As CQueryObject
Set oQuery = New CQueryObject

oQuery.Init REApplication.SessionContext 'Initialize object
oQuery.LoadByField uf_QUERY_NAME, "marc_test" 'Load query by name
oQuery.QuerySet.OpenQuerySet 'Open the query set

Dim lCount As Long
     Do While Not oQuery.QuerySet.EOF
        Debug.Print oQuery.QuerySet.FieldName(1) 'First Column Name
        Debug.Print oQuery.QuerySet.FieldValue(1)  'First Column Value
        oQuery.QuerySet.MoveNext 'Move on to next record in query
        lCount = lCount + 1

Debug.Print "Number of records processed: " & lCount


Set oQuery = Nothing

End Sub

Disclaimer: We provide programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes you are familiar with Microsoft Visual Basic and the tools used to create and debug procedures. Our Customer Support can help explain the functionality of a particular procedure, but we will not modify, or assist you with modifying, these examples to provide additional functionality. If you are interested in learning more about the VBA and API modules, contact your account manager.