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:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You appear to be missing a field name from the last part of your WHERE criterion
 
Upvote 0
You appear to be missing a field name from the last part of your WHERE criterion
Sorry, not only there but in my excel macro it is good


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"
 
Upvote 0
Sorry, not only there but in my excel macro it is good


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"
STRANGE after paste it goes wrong
 
Upvote 0
You are still missing your field name here:
where [t1$]. <> 0"

t1$
is your table reference, and then you usually have a period and then the field name.
You have the period, but are still missing the field name.
 
Upvote 0
You are still missing your field name here:
where [t1$]. <> 0"

t1$
is your table reference, and then you usually have a period and then the field name.
You have the period, but are still missing the field name.
1632398840285.png
 
Upvote 0
I think I see what is going on with your posting issue. I think that the "B" between the right and left brackets is being mistaken for the BOLD editing tags on this board.
We can see it on your screen print.
 
Upvote 0
I don't think you have the subquery in the correct place or structured properly.
I do not have the resources I need to set it up and test it out right now, but think I should be able to at some point later in the day, if no one else replies before then.
 
Upvote 0
I don't think you have the subquery in the correct place or structured properly.
I do not have the resources I need to set it up and test it out right now, but think I should be able to at some point later in the day, if no one else replies before then.
Thank you in advance !
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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