Solved

Use String Query As Recordset for Second String Query in VBA

Posted on 2014-01-02
2
380 Views
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
dbs.Close
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.
0
Comment
Question by:gwflyer
2 Comments
 
LVL 84

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:gwflyer
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.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This collection of functions covers all the normal rounding methods of just about any numeric value.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

864 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