Connecting Tech Pros Worldwide Forums | Help | Site Map

querying another query in VBA

Familiar Sight
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 168
#1: Dec 25 '08
Hi,
I usually use access query builder to create complex queries. Especially to create Sub Queries I.E. a querying another query. In my current situation I need to provide the criteria to the query from a Form and then call it using the rst function in ADO (see last line of my code). The issue with the rst function is that it doesn't cater for queries made in access query builder that have an object from a Form as their criteria. I have attempted to replicate the Sub query below, but it gives me a error saying" Syntax Error in FROM Clause"

Expand|Select|Wrap|Line Numbers
  1. Dim strSqlCashBank As String
  2. Dim StrSqlCashBankSUM As String
  3. strSqlCashBank = "SELECT qryReconciledInt.BankID, Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, " & _
  4. "qryReconciledInt.CollectionID, tblConsultantDetails.[First Name], qryReconciledInt.Cons " & _
  5. "FROM tblConsultantDetails INNER JOIN (qryReconciledInt INNER JOIN tblBank ON " & _
  6. "qryReconciledInt.BankID = tblBank.BankID) ON tblConsultantDetails.ID = qryReconciledInt.Cons " & _
  7. "WHERE qryReconciledInt.Cons =" & Me.cboConsID & " " & _
  8. "GROUP BY qryReconciledInt.BankID, tblBank.Dt, qryReconciledInt.CollectionID, " & _
  9. "tblConsultantDetails.[First Name], qryReconciledInt.Cons HAVING (((qryReconciledInt.CollectionID) = 0)) " & _
  10. "ORDER BY tblBank.Dt"
  11. StrSqlCashBankSUM = "SELECT SumOfAmount FROM " & strSqlCashBank
  12. MsgBox StrSqlCashBankSUM
  13. rst.Open StrSqlCashBankSUM, cnn, adOpenDynamic, adLockOptimistic

Familiar Sight
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 168
#2: Dec 25 '08

re: querying another query in VBA


Hi Guys ive figured it out, here is the correct code

Expand|Select|Wrap|Line Numbers
  1. Dim strSqlCashBank As String 
  2. Dim StrSqlCashBankSUM As String 
  3. strSqlCashBank = ("SELECT qryReconciledInt.BankID, Sum(tblBank.Amount) AS SumOfAmount, tblBank.Dt, " & _ 
  4. "qryReconciledInt.CollectionID, tblConsultantDetails.[First Name], qryReconciledInt.Cons " & _ 
  5. "FROM tblConsultantDetails INNER JOIN (qryReconciledInt INNER JOIN tblBank ON " & _ 
  6. "qryReconciledInt.BankID = tblBank.BankID) ON tblConsultantDetails.ID = qryReconciledInt.Cons " & _ 
  7. "WHERE qryReconciledInt.Cons =" & Me.cboConsID & " " & _ 
  8. "GROUP BY qryReconciledInt.BankID, tblBank.Dt, qryReconciledInt.CollectionID, " & _ 
  9. "tblConsultantDetails.[First Name], qryReconciledInt.Cons HAVING (((qryReconciledInt.CollectionID) = 0)) " & _ 
  10. "ORDER BY tblBank.Dt")
  11. StrSqlCashBankSUM = "SELECT SumOfAmount FROM " & strSqlCashBank 
  12. MsgBox StrSqlCashBankSUM 
  13. rst.Open StrSqlCashBankSUM, cnn, adOpenDynamic, adLockOptimistic 
It was just a matter of putting brackets around the Sql for strSqlCashBank
nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#3: Dec 25 '08

re: querying another query in VBA


Hmm the "(" and ")" brackets can't be the only reason.
I do however suspect your line continuation (the & _ ) to be influenced by this.
In general I don't use it as there's a maximum number of continuations, better to use the following construction:
strSQL = "SELECT x "
strSQL = strSQL & ", y, z "
strSQL = strSQL & "FROM tblZ "
etc.

Nic;o)
Reply