GK in the UK
Registered User.
- Local time
- Today, 08:46
- Joined
- Dec 20, 2017
- Messages
- 281
SQL novice here. I've created a SQL lookup on two tables with a subquery on a single field.
But, I want to fetch more than one field from the sub query, which is an IN clause. Not to compare and select, I just want the fields.
When I try, it Access pops up error 3306, 'You have written a subquery that can return more than one field without using the EXISTS clause ... Revise the SELECT statement of the subquery to request only one field'
Working code (with only 1 field selected from tblAllocations):
I'm re-writing the SQL but don't seem to have got it right. I've re-written it with an EXISTS (and with only the same fields extracted for the moment) but it hangs for a few seconds and Access quits. What's wrong ?
But, I want to fetch more than one field from the sub query, which is an IN clause. Not to compare and select, I just want the fields.
When I try, it Access pops up error 3306, 'You have written a subquery that can return more than one field without using the EXISTS clause ... Revise the SELECT statement of the subquery to request only one field'
Working code (with only 1 field selected from tblAllocations):
Code:
'' ' WORKING CODE
'' ' SQL to query tblAllocations
'' ' Will contain all records that were in the original batch eg MBAT0000015
'' ' Form field where we want allocDocReference is called thDocReference
'' strSQL1 = "SELECT allocDocReference " & _
'' "AS thDocReference FROM tblAllocations " & _
'' "WHERE allocBatchReference ='" & varBatchID & "'"
''
'' ' SQL to query tblTransHeaders
'' ' Will contain all header records that were allocated in eg MBAT0000015
'' ' Field names match the fields on the form, don't need AS here
'' strSQL2 = "SELECT TransHeaderID, thDocReference, thDate, thDueDate, thYourOrderRef, " & _
'' "thNetValue_summed, thVatValue_summed, thPPD_summed " & _
'' "FROM tblTransHeaders WHERE thDocReference IN (" & strSQL1 & ")"
Code:
strSQL2 = "SELECT allocDocReference AS thDocReference FROM tblAllocations WHERE EXISTS " & _
"(SELECT TransHeaderID, thDocReference, thDate, thDueDate, thYourOrderRef, thNetValue_summed, thVatValue_summed, thPPD_summed " & _
"FROM tblTransHeaders " & _
"WHERE thDocReference IN (SELECT allocDocReference FROM tblAllocations WHERE allocBatchReference ='" & varBatchID & "'))"