SQL Query in VBA

tigor

New Member
Joined
Sep 23, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello Everybody!

I'm trying to get collect data from two excel sheet with OLEDB and sql query with subquery but couldnt.
I got error messaage:
At most one record can be returned by this subquery (Run-time error :80004005)

1632392560643.png


Please help me, I was trying to find solution on internet but couldnt.

Rich (BB code):
Sub Makró1()
'
' Makró1 Makró
'
 Dim myConnection As String
    Dim RS As ADODB.Recordset
    Dim mySQL As String
    Dim strPath As String
    Dim wsMain As Worksheet
    Debug.Print Now
    Set wsMain = Worksheets("Report")
    Application.ScreenUpdating = False
    strPath = ActiveWorkbook.FullName
    myConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                     "Data Source=" & strPath & ";Extended Properties=Excel 12.0"
                    
            mySQL = "SELECT [T2$].[A], " & _
                    "[T2$].[B1], " & _
                    "[T1$]., " & _
                    "(select SUM([T2$].[B1]) FROM [T2$] GROUP BY [T2$].[A], [T2$].[B1]) " & _
                    "FROM [T2$] inner JOIN [T1$] ON [T2$].[A] = [T1$].[A]  where [t1$]. <> 0"
              
        Set RS = New ADODB.Recordset
        RS.Open mySQL, myConnection, adOpenForwardOnly, adLockOptimistic
        
        wsMain.Range("A2").CopyFromRecordset RS
    RS.Close
    Set RS = Nothing
    Application.ScreenUpdating = True
    Debug.Print Now
'
End Sub
 
Last edited by a moderator:
I can do this in a series of two queries, but am having issues doing it all in one.

If we had a query to do the SUM part, and called it "Q1", and the query looked like this:
SQL:
SELECT T2.A, Sum(T2.B1) AS SumB1
FROM T2
GROUP BY T2.A;

Then we could do another query structured like this (which would return what you want):
SQL:
SELECT T2.A, T2.B1, T1.B, Q1.SumB1
FROM (T2 INNER JOIN T1 ON T2.A = T1.A) INNER JOIN Q1 ON T1.A = Q1.A
WHERE (((T1.B)<>0));

(Note that I left the "$" and brackets off of the table names to avoid the issues you were having posting).
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I can do this in a series of two queries, but am having issues doing it all in one.

If we had a query to do the SUM part, and called it "Q1", and the query looked like this:
SQL:
SELECT T2.A, Sum(T2.B1) AS SumB1
FROM T2
GROUP BY T2.A;

Then we could do another query structured like this (which would return what you want):
SQL:
SELECT T2.A, T2.B1, T1.B, Q1.SumB1
FROM (T2 INNER JOIN T1 ON T2.A = T1.A) INNER JOIN Q1 ON T1.A = Q1.A
WHERE (((T1.B)<>0));

(Note that I left the "$" and brackets off of the table names to avoid the issues you were having posting).
thanks for the info.
yes, this workaround is known but why I should use this ?
why IT doesn give a solution for an error???? I mean excel, vba,sql developers.
I'm sad.
 
Upvote 0
The issue is that you are not "linking" the totals subquery in to the tables in the FROM clause.
You need to build that relationship between that and the other objects in the query, and you have not done that.
If you just try placing it in the SELECT portion of your query (like you did), there is nothing linking the totals for "A" to the "A" records in the other query.

I am pretty sure that there is a way to do it, but it can get a little tricky, and it has been a few years since I wrote a lot of complex SQL queries.
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top