?
Solved

Use String Query As Recordset for Second String Query in VBA

Posted on 2014-01-02
2
Medium Priority
?
417 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
[X]
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
2 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

719 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