Use String Query As Recordset for Second String Query in VBA

Posted on 2014-01-02
Last Modified: 2014-01-02
I am trying to building a query in VBA using another query in the same VBA.  Here is my code:

Dim dbs As Database
Dim rs2 As Recordset
Dim str2 As String
Dim str3 As String

Set dbs = CurrentDb()

str = [Forms]![frmClinic_Main]![ClinicID]

Set rs2 = CurrentDb.OpenRecordset("SELECT * from tblEnrollment_byAge WHERE EnrSite =" & Chr(34) & str & Chr(34) & ";", dbOpenSnapshot)

str3 = "SELECT rs2.AgeCombo, " & _
        "Sum(rs2.TotalEnrolled) AS TotalEnrolled FROM qdf INNER JOIN " & _
        "tblAgeGroupRanking ON rs2.AgeCombo = tblAgeGroupRanking.AgeCode GROUP BY " & _
        "rs2.AgeCombo, tblAgeGroupRanking.Rank ORDER BY tblAgeGroupRanking.Rank;"

With dbs
qdf1 = .CreateQueryDef("TempTable", str3)
DoCmd.OpenQuery "TempTable"
.QueryDefs.Delete "TempTable"
End With
Set qdf1 = Nothing

Is this possible to do?  Nothing happens when I click the command button.  I'm using the QueryDef method to view my results.    Any help you can provide is greatly appreciated.
Thank you very much.
Question by:gwflyer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 84

Accepted Solution

Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39751516
Perhaps this query will work:

SELECT EBA.AgeCombo, Sum(EBA.TotalEnrolled) AS TotalEnrolled FROM tblEnrollment_byAge AS EBA
INNER JOIN tblAgeGroupRanking AS AGR EBA.AgeCombo = AGR.AgeCode
HAVING EBA.EnrSite='" & str & "' GROUP BY EBA.AgeCombo, AGR.Rank ORDER BY ARG.Rank

If it does, then just create a stored query with that as the SQL, and then use DoCmd.OpenQuery to open that query. Of course, if you do that, you'd have to change the way you refer to EnrSite, something like:

SELECT EBA.AgeCombo, Sum(EBA.TotalEnrolled) AS TotalEnrolled FROM tblEnrollment_byAge AS EBA
INNER JOIN tblAgeGroupRanking AS AGR EBA.AgeCombo = AGR.AgeCode
HAVING EBA.EnrSite=[Forms]![YourForm]!YourControl GROUP BY EBA.AgeCombo, AGR.Rank ORDER BY ARG.Rank

Better yet, create a Form based on that query and let the user view the data through the form. Queries are for data manipulation, not for direct user viewing.

Author Closing Comment

ID: 39753184
Thank you very much for helping with this.  I did take your advice and created a stored query.  All is working fine now.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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