title: how to use VBA created SQL in an Access query
body: <p>I am new to Access and I think there must be an easy way to do this but I have not found it. I was hoping to use the same access query to produce a detail or summary report based on the user entered parameter. I was thinking of changing the SQL associated with the query in VBA.  </p>
  
  <ol>
  <li><p>Is this the best way to accomplish my goal? Or is there a better way?
  If using VBA is the way to go, I did the following:</p>
  
  <pre><code>Function ChangeSQL() As String
      Dim strSQL As String
  
      If [Summary] = -1 Then
          ChangeSQL = "Select LASTNAME, Sum(G.AMOUNT) AS AMOUNT " _
                    &amp; "FROM DONOR D, GIFT G" _
                    &amp; "WHERE D.ID = G.DONOR " _
                    &amp; "GROUP BY D.LASTNAME "
  
      Else
          ChangeSQL = "Select LASTNAME, AMOUNT  AS AMOUNT " _
                    &amp; "FROM DONOR D, GIFT G" _
                    &amp; "WHERE D.ID = G.DONOR "
      End If
  
  End Function
  </code></pre></li>
  <li><p>how do I call the <em>ChangeSQL</em> function from the Access query </p></li>
  <li>how do I actually execute the changed SQL from the query? </li>
  </ol>
  
  <p>I have seen many examples where the VBA executes the desired SQL, so I know how to do that in VBA but I still have question 2) and I would also like to be able to just use my simple <em>ChangeSQL</em> function as written and have the query actually execute the changed SQL (hence my question 3).</p>