0

Hi guys I dont know if this makes sense but how can I query another query in VBA?

I will show with example below

This is my first query

strSQL1 = "SELECT DISTINCT SourceBank" _
    & ", Fullname, FirstNames" _
    & ", Surname, Company" _
    & ", EmailAddress" _
    & " FROM question" _
    & " WHERE FirstNames = '" & strFirstNames & "'" _
    Set rs = dbs.OpenRecordset(strSQL)

Then I want to do something like this. Query the first query

        strSQL2 = "S"SELECT * from " & strSQL1
    Set rs1 = dbs.OpenRecordset(strSQL)

I just want to know if this is possible and if not then what is the best way around it? All I want to do is to be able to query another query/string/recordset.

thanks

1
  • why do you use tag [mysql]? ms-access use Jet SQL. Commented Sep 24, 2013 at 10:30

1 Answer 1

2

You can do it almost like you've wrote:

strSQL2="SELECT * FROM (" & strSQL1 & ")"

but be sure not to include ; in strSQL1

upd, try:

strSQL2 = "SELECT Question.EmailAddress, SUBQUERY.EmailAddress &" _ 
          & "FROM Question LEFT JOIN (" & strSQL1 & ") AS SUBQUERY ON Question.EmailAddress = SUBQUERY.EmailAddress"

OR just save sql1 into QueryDef (Query in ms access) and use it like source table.

Sign up to request clarification or add additional context in comments.

7 Comments

and how is it used in a query with a left join? <code> strSQL1 = "SELECT Question.EmailAddress, (" & strSQL & ").EmailAddress &" _ & "FROM Question LEFT JOIN (" & strSQL & ") ON Question.EmailAddress = (" & strSQL & ").EmailAddress" </code>
hmm. I'm only getting back 1 record count when I should be getting a lot more
i changed the join type but still get the same count
left join was to select all Question and some connected rows from subquery. what result do you need?
Left join is what I want. I want all records from question and only matching records from strSQL1 where the email matches. It's just that I'm not getting the 18k records I should be getting. I'm only getting one from this - RsCount = rs1.RecordCount
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.