Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434

    Long subquery

    Hi, i have this query in vba



    Code:
    Sql = "SELECT Trattative.IDtrattativa, Candidati.IDcandidato, Candidati.Nome & "" "" & candidati.Cognome AS NomeCognome, Candidati.[RAL/Fatturato], Candidati.[penale economica], Trattative.[RAL richiesta], FormatPercent(([RAL richiesta]-[RAL/Fatturato])/[RAL/Fatturato],0) AS [Incremento percentuale], Candidati.[corrispettivo patto], FormatCurrency([ral richiesta]*15/100,0) AS [Corr patto nuovo], [RAL richiesta]*5 AS [RAL 5 anni], [Penale economica]*2 AS [Penale * 2], [RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2] AS [RAL 5 anni + corr patto nuovo + Patto * 2], ([RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2])*0.4 AS [Costo INPS], [RAL 5 anni + corr patto nuovo + Patto * 2]+[Costo inps] AS [Costo totale], Candidati.[PTF trasferibile personale], Candidati.[Redditivitą ptf trasferibile], ([ptf trasferibile personale]*1000000)*[redditivitą ptf trasferibile]/100 AS [Mint annuo], [mint annuo]*5 AS [Mint 5 anni], Round([Costo totale]/[mint 5 anni],4) AS Costi, " & vbCrLf & _         "Round(1-[costo totale]/[mint 5 anni],4) AS Ricavi, FormatNumber([Costo totale]/[mint annuo]*12,0) AS Payback " & vbCrLf & _
             "FROM Candidati INNER JOIN Trattative ON Candidati.IDcandidato = Trattative.CandidatoID " & vbCrLf & _
             "WHERE (((Trattative.IDtrattativa)=443155041435185 " & vbCrLf & _
             "ORDER BY Candidati.Nome & "" "" & candidati.Cognome, Trattative.[RAL richiesta] DESC;"
    this should be the source of another query, i set a string variable "sql" that is the first query

    Code:
       Me.AnalisiOperazioneGrafico.RowSource = "SELECT IDtrattativa, NomeCognome, [Ricavi] AS Valore, ""Redditivitą"" AS CostiRicavi FROM (sql) UNION SELECT  IDtrattativa, NomeCognome, [costi], ""Costi"" FROM  (sql);"
    it gives me error i think for the "", but i'm not able to insert the first query as source of the second query without using a variable, the first query it is too long
    Last edited by diegomarino; 02-10-2022 at 03:39 PM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Use an apostrophe instead of the doubled quote marks.

    'Redditivitą'


    `Costi`
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    It didn't work. Sorry, i made a mistake, first post edited

  4. #4
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    i tried not to use the "sql" variable and i write all the sql in the final query, but it says it's too long

    Code:
    "SELECT IDtrattativa, NomeCognome, [Ricavi] AS Valore, ""Redditivitą"" AS CostiRicavi FROM " & vbCrLf & _                                           "(SELECT Trattative.IDtrattativa, Candidati.IDcandidato, Candidati.Nome & "" "" & candidati.Cognome AS NomeCognome, Candidati.[RAL/Fatturato], Candidati.[penale economica], Trattative.[RAL richiesta], FormatPercent(([RAL richiesta]-[RAL/Fatturato])/[RAL/Fatturato],0) AS [Incremento percentuale], Candidati.[corrispettivo patto], FormatCurrency([ral richiesta]*15/100,0) AS [Corr patto nuovo], [RAL richiesta]*5 AS [RAL 5 anni], [Penale economica]*2 AS [Penale * 2], [RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2] AS [RAL 5 anni + corr patto nuovo + Patto * 2], ([RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2])*0.4 AS [Costo INPS], [RAL 5 anni + corr patto nuovo + Patto * 2]+[Costo inps] AS [Costo totale], Candidati.[PTF trasferibile personale], Candidati.[Redditivitą ptf trasferibile], ([ptf trasferibile personale]*1000000)*[redditivitą ptf trasferibile]/100 AS [Mint annuo], [mint annuo]*5 AS [Mint 5 anni], Round([Costo totale]/[mint 5 anni],4) AS Costi, " & vbCrLf & _
                                               "Round(1-[costo totale]/[mint 5 anni],4) AS Ricavi, FormatNumber([Costo totale]/[mint annuo]*12,0) AS Payback " & vbCrLf & _
                                               "FROM Candidati INNER JOIN Trattative ON Candidati.IDcandidato = Trattative.CandidatoID " & vbCrLf & _
                                               "WHERE (((Trattative.IDtrattativa)=443155041435185 " & vbCrLf & _
                                               "ORDER BY Candidati.Nome & "" "" & candidati.Cognome, Trattative.[RAL richiesta] DESC;) " & vbCrLf & _
                                               "UNION SELECT  IDtrattativa, NomeCognome, [costi], ""Costi"" FROM " & vbCrLf & _
                                               "(SELECT Trattative.IDtrattativa, Candidati.IDcandidato, Candidati.Nome & "" "" & candidati.Cognome AS NomeCognome, Candidati.[RAL/Fatturato], Candidati.[penale economica], Trattative.[RAL richiesta], FormatPercent(([RAL richiesta]-[RAL/Fatturato])/[RAL/Fatturato],0) AS [Incremento percentuale], Candidati.[corrispettivo patto], FormatCurrency([ral richiesta]*15/100,0) AS [Corr patto nuovo], [RAL richiesta]*5 AS [RAL 5 anni], [Penale economica]*2 AS [Penale * 2], [RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2] AS [RAL 5 anni + corr patto nuovo + Patto * 2], ([RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2])*0.4 AS [Costo INPS], [RAL 5 anni + corr patto nuovo + Patto * 2]+[Costo inps] AS [Costo totale], Candidati.[PTF trasferibile personale], Candidati.[Redditivitą ptf trasferibile], ([ptf trasferibile personale]*1000000)*[redditivitą ptf trasferibile]/100 AS [Mint annuo], [mint annuo]*5 AS [Mint 5 anni], Round([Costo totale]/[mint 5 anni],4) AS Costi, " & vbCrLf & _
                                               "Round(1-[costo totale]/[mint 5 anni],4) AS Ricavi, FormatNumber([Costo totale]/[mint annuo]*12,0) AS Payback " & vbCrLf & _
                                               "FROM Candidati INNER JOIN Trattative ON Candidati.IDcandidato = Trattative.CandidatoID " & vbCrLf & _
                                               "WHERE (((Trattative.IDtrattativa)=443155041435185 " & vbCrLf & _
                                               "ORDER BY Candidati.Nome & "" "" & candidati.Cognome, Trattative.[RAL richiesta] DESC;)"

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    The vbCrLf's are not needed but shouldn't hurt.

    Again, use apostrophes.

    Don't end the first string with semi-colon.
    Code:
    Sql = "SELECT Trattative.IDtrattativa, Candidati.IDcandidato, Candidati.Nome & ' ' & candidati.Cognome AS NomeCognome, " & _
            "Candidati.[RAL/Fatturato], Candidati.[penale economica], Trattative.[RAL richiesta], " & _
            "FormatPercent(([RAL richiesta]-[RAL/Fatturato])/[RAL/Fatturato],0) AS [Incremento percentuale], Candidati.[corrispettivo patto], " & _
            "FormatCurrency([ral richiesta]*15/100,0) AS [Corr patto nuovo], [RAL richiesta]*5 AS [RAL 5 anni], [Penale economica]*2 AS [Penale * 2], " & _
            "[RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2] AS [RAL 5 anni + corr patto nuovo + Patto * 2], " & _
            "([RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2])*0.4 AS [Costo INPS], " & _
            "[RAL 5 anni + corr patto nuovo + Patto * 2]+[Costo inps] AS [Costo totale], Candidati.[PTF trasferibile personale], Candidati.[Redditivitą ptf trasferibile], " & _
            "([ptf trasferibile personale]*1000000)*[redditivitą ptf trasferibile]/100 AS [Mint annuo], [mint annuo]*5 AS [Mint 5 anni], " & _
            "Round([Costo totale]/[mint 5 anni],4) AS Costi, Round(1-[costo totale]/[mint 5 anni],4) AS Ricavi, FormatNumber([Costo totale]/[mint annuo]*12,0) AS Payback " & _
            "FROM Candidati INNER JOIN Trattative ON Candidati.IDcandidato = Trattative.CandidatoID " & _
            "WHERE (((Trattative.IDtrattativa)=443155041435185 " & _
            "ORDER BY Candidati.Nome & ' ' & candidati.Cognome, Trattative.[RAL richiesta] DESC"
    Also, need to concatenate the Sql variable.
    FROM (" & Sql & ") UNION
    Code:
    Me.AnalisiOperazioneGrafico.RowSource = "SELECT IDtrattativa, NomeCognome, [Ricavi] AS Valore, 'Redditivitą' AS CostiRicavi FROM (" & Sql & ") " & _
                                              "UNION SELECT IDtrattativa, NomeCognome, [costi], 'Costi' FROM  (" & Sql & ");"
    Could save the first sql as a query object then reference that query name in the second SQL.

    Strongly advise not to use spaces nor punctuation/special characters in naming convention.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    thanks, i used query def but i hoped to get rid of that, so i was triyng a subquery.

    you're right i made a lot of mistakes

    i copied you solution but i have this error
    Click image for larger version. 

Name:	Immagine 2022-02-10 231817.png 
Views:	26 
Size:	5.3 KB 
ID:	47252

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Translate to English please.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Agree - OP needs to translate, but since the number was there I did it this time:
    The setting for this property is too long.

    I'll bet anyone else's money that the data contains apostrophes (like O'Brien) or the sql contains an orphan delimiter and that's why the error. Check the data and output the sql to the immediate window (Debug.Print SQL) and post that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Not seeing cause for apostrophes to be an issue.

    Do wonder if all the ID fields are number type. I am not able to enter 443155041435185 into a long integer field so is this generated by autonumber? A double field will take it and displays as scientific notation and the filter works.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Not seeing cause for apostrophes to be an issue.
    Just posting what I found in my research as a solution for the error message.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    no, it is a calculated field, a date/time with seconds.
    i tried with a smaller number but i get the same error

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I am confused. IDtrattativa is a calculated date/time field?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    434
    yes, since i'm the only working on that and i need to use powerapps (linked to some excel files) too so i can't use an autonumber. so i set the id as date time with seconds, i can't literally make two records so fast.

    sorry i missed the late night post, i didn't translate cause there were the number.

    this is what i get in immediate wind.

    Code:
    SELECT Trattative.IDtrattativa, Candidati.IDcandidato, Candidati.Nome & ' ' & candidati.Cognome AS NomeCognome, Candidati.[RAL/Fatturato], Candidati.[penale economica], Trattative.[RAL richiesta], FormatPercent(([RAL richiesta]-[RAL/Fatturato])/[RAL/Fatturato],0) AS [Incremento percentuale], Candidati.[corrispettivo patto], FormatCurrency([ral richiesta]*15/100,0) AS [Corr patto nuovo], [RAL richiesta]*5 AS [RAL 5 anni], [Penale economica]*2 AS [Penale * 2], [RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2] AS [RAL 5 anni + corr patto nuovo + Patto * 2], ([RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2])*0.4 AS [Costo INPS], [RAL 5 anni + corr patto nuovo + Patto * 2]+[Costo inps] AS [Costo totale], Candidati.[PTF trasferibile personale], Candidati.[Redditivitą ptf trasferibile], ([ptf trasferibile personale]*1000000)*[redditivitą ptf trasferibile]/100 AS [Mint annuo], [mint annuo]*5 AS [Mint 5 anni], Round([Costo totale]/[mint 5 anni],4) AS Costi, Round(1-[costo totale]/[mint 5 anni],4) AS Ricavi, FormatNumber([Costo totale]/[mint annuo]*12,0) AS Payback FROM Candidati INNER JOIN Trattative ON Candidati.IDcandidato = Trattative.CandidatoID WHERE (((Trattative.IDtrattativa)=100 ORDER BY Candidati.Nome & ' ' & candidati.Cognome, Trattative.[RAL richiesta] DESC
    i can see it is cut. I don't know why

  14. #14
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I have reformatted that to this;
    Code:
    SELECT Trattative.IDtrattativa,
    	 Candidati.IDcandidato,
    	 Candidati.Nome & ' ' & candidati.Cognome AS NomeCognome,
    	 Candidati.[RAL/Fatturato],
    	 Candidati.[penale economica],
    	 Trattative.[RAL richiesta],
    	 FormatPercent(([RAL richiesta]-[RAL/Fatturato])/[RAL/Fatturato],0) AS [Incremento percentuale],
    	 Candidati.[corrispettivo patto],
    	 FormatCurrency([ral richiesta]*15/100,0) AS [Corr patto nuovo],
    	 [RAL richiesta]*5 AS [RAL 5 anni],
    	 [Penale economica]*2 AS [Penale * 2],
    	 [RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2] AS [RAL 5 anni + corr patto nuovo + Patto * 2],
    	 ([RAL 5 anni]+([corr patto nuovo]*5)+[Penale * 2])*0.4 AS [Costo INPS],
    	 [RAL 5 anni + corr patto nuovo + Patto * 2]+[Costo inps] AS [Costo totale],
    	 Candidati.[PTF trasferibile personale],
    	 Candidati.[Redditivitą ptf trasferibile],
    	 ([ptf trasferibile personale]*1000000)*[redditivitą ptf trasferibile]/100 AS [Mint annuo],
    	 [mint annuo]*5 AS [Mint 5 anni],
    	 Round([Costo totale]/[mint 5 anni],4) AS Costi,
    	 Round(1-[costo totale]/[mint 5 anni],4) AS Ricavi,
    	 FormatNumber([Costo totale]/[mint annuo]*12,0) AS Payback 
    FROM Candidati 
    INNER JOIN Trattative ON Candidati.IDcandidato = Trattative.CandidatoID 
    WHERE (((Trattative.IDtrattativa)=100 
    ORDER BY Candidati.Nome & ' ' & candidati.Cognome, Trattative.[RAL richiesta] DESC
    The only thing I can see that is possibly wrong is referring to an alias field in the query that the alias is created in - [RAL 5 anni]
    SQL Server won't let you do that sometimes Access will.

    If it is still truncating try adding to the sql string like this to break it into readable separate statements

    Code:
    StrSQL = " SELECT ....." 
    StrSQL = StrSql & " FROM ...."
    StrSQL = StrSql & " WHERE ...."
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  15. #15
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    2 missing close parens?

    Code:
    WHERE (((Trattative.IDtrattativa)=100 
    ORDER BY Candidati.Nome & ' ' & candidati.Cognome, Trattative.[RAL richiesta] DESC

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subquery
    By Alex Motilal in forum Queries
    Replies: 3
    Last Post: 12-03-2020, 07:27 AM
  2. A subquery inside a subquery
    By tbjmobile@gmail.com in forum Queries
    Replies: 15
    Last Post: 04-22-2018, 11:48 AM
  3. TOP subquery
    By helpaccess in forum Queries
    Replies: 5
    Last Post: 08-30-2011, 10:28 AM
  4. Subquery sum?
    By anemoskkk in forum Access
    Replies: 0
    Last Post: 04-29-2011, 12:36 PM
  5. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums