0

I'm trying to add to the following query:

    strSQL = "SELECT  fldName, blkName, CDbl(fldValue) " & _
        "FROM dbSecurities2 as S " & _
        "WHERE " & _
        "S.isin='" & Code & "' " & _
        "AND " & _
        "S.fldName='" & fldName & "' "

A row that makes the sum of the fldValue like:

    strSQL = "SELECT  fldName, blkName, CDbl(fldValue) " & _
        "FROM dbSecurities2 as S " & _
        "UNION " & _
        "SELECT Sum(fldValue) AS fldValue " & _
        "WHERE " & _
        "S.isin='" & Code & "' " & _
        "AND " & _
        "S.fldName='" & fldName & "' "

the error is:

Run -time error '3141'. The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect

3
  • 2
    Define "doesn't work" ... is there an error? If so, what is the error? Part of the trouble is likely because the union select does not have a FROM clause and is not querying the same number of columns. Commented Dec 4, 2020 at 14:03
  • You're right the error is Run -time error '3141'. The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect Commented Dec 4, 2020 at 14:08
  • Can you add a small data example to the question (from the first query), along with an example of the desired output? Best to add this info as text and not as pictures. Commented Dec 4, 2020 at 14:15

2 Answers 2

1

I found this is working:

   strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
        "FROM dbSecurities2 as S " & _
        "WHERE " & _
        "S.isin='" & Code & "' " & _
        "AND " & _
        "S.fldName='" & fldName & "' " & _
        "UNION " & _
        "SELECT '' AS fldName, 'Total' AS Total, Sum(CDbl(fldValue)) " & _
        "FROM dbSecurities2 AS B " & _
        "WHERE " & _
        "B.isin='" & Code & "' " & _
        "AND " & _
        "B.fldName='" & fldName & "' "
Sign up to request clarification or add additional context in comments.

Comments

0

This should run as expected:

    strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
        "FROM dbSecurities2 AS S " & _
        "WHERE " & _
        "S.isin='" & Code & "' " & _
        "AND " & _
        "S.fldName='" & fldName & "' " & _
        "UNION ALL " & _
        "SELECT TOP 1 "", "Total", Sum(CDbl(fldValue)) " & _
        "FROM dbSecurities2"

If you have Null values, use Nz:

    strSQL = "SELECT fldName, blkName, CDbl(Nz(fldValue, 0)) " & _
        "FROM dbSecurities2 AS S " & _
        "WHERE " & _
        "S.isin='" & Code & "' " & _
        "AND " & _
        "S.fldName='" & fldName & "' " & _
        "UNION ALL " & _
        "SELECT TOP 1 "", "Total", Sum(CDbl(Nz(fldValue, 0))) " & _
        "FROM dbSecurities2"

4 Comments

Hi Gustav, I adjusted your expression in the second SELECT as :"SELECT top 1 '' as fldName, 'Total' as blkName, Sum(CDbl(fldValue))" & _ But I get an get the error Data type mismatch in criteria expression... any Idea why?
instead "SELECT top 1 '' as fldName, 'Total' as blkName, fldValue " & _ works and give me an empty line as Total. it seems that the problem is the Sum but I cannot figure out why...
_ I get an get the error Data type mismatch in criteria expression... any Idea why?_ Sure. You corrected the correct SQL to invalid SQL. If you have Null values, CDbl will fail; see extended answer, please.
I still have the same error: Data type mismatch in criteria expression. see extended answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.