Hi cpb, Yes, after a few modifications to get my prior syntax errors and make the tables right, the sql given to me on 4/24 @ 9:09 works correctly. Thank you, this is a condensed version of what I had. This is how I made it work:
SELECT
SUM("NoUnits" * "Power" * "OpDays" * 24 / 1000 * 0.09983)
+
(
SELECT SUM("Cost")
FROM "Expenses Table"
WHERE "ExpCatID" = 8
AND "AcctID" BETWEEN 4999000 AND 4999999
AND "Date" BETWEEN '2024-04-01' AND '2024-04-30'
AND "ConJobID" in(13,14)
)
FROM
"Elec Est Table"
WHERE
"FinishDate" BETWEEN '2024-03-16' AND '2024-04-15'
However, I could not make this work in the UPDATE statement in the macro. I get syntax errors, so I know I am messing up somewhere. It looks like this:
Sub UpdateCost
Dim oContext As Object
Dim oDatabase As Object
Dim oConnection As Object
Dim oStatement As Object
Dim sSQL As String
' Get the database context
oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
' Replace "DB_NAME" with your actual database name
oDatabase = oContext.getByName("New")
' Connect to the database (you may need to provide username and password)
oConnection = oDatabase.getConnection("", "")
' Create the SQL statement
sSQL ="Update ""Electricity Monthly Totals""" & _
"Set ""Cost"" = " & _
"(SELECT " & _
"SUM(""NoUnits"" * ""Power"" * ""OpDays"" * 24 / 1000 * 0.09983)" & _
"+" & _
"(" & _
"SELECT SUM(""Cost"")" & _
"FROM ""Expenses Table""" & _
"WHERE ""ExpCatID"" = 8" & _
"AND ""AcctID"" BETWEEN 4999000 AND 4999999" & _
"AND ""Date"" BETWEEN '2024-04-01' AND '2024-04-30'" & _
"AND ""ConJobID"" in(13,14)" & _
")" & _
"FROM" & _
"""Elec Est Table""" & _
"""WHERE""" & _
"""FinishDate"" BETWEEN '2024-03-16' AND '2024-04-15' ) AS ""Apr 2024 Total Bill""" & _
"FROM ""Elec Est Table"" WHERE ""FinishDate"" BETWEEN '2024-03-16' AND '2024-04-15' )" & _
"Where ""ElecTotalID"" = '8'"
' msgbox (sSQL)
' Execute the SQL query
oStatement = oConnection.createStatement()
oStatement.executeUpdate(sSQL)
End Sub
Yet, I was able to get to a different error message with the macro code using my longer version of sql, and assuming that I corrected syntax errors. The error message stated in a Message Box that no valid table:
BASIC runtime error.
An exception occurred
Type: com.sun.star.sdbc.SQLException
Message: The query cannot be executed. It contains no valid table..
So, I think that I am messing up with syntax up to a point , then I get that correct, then I get this last message about invalid tables. This is what started me thinking that spaces in names were causing problems, but @Villeroy said that double quotes should work earlier in the thread. So, I am not sure what to think.
I can use your sql, or mine, to perform the SELECT statement and acquire the correct total. I can also use SQL–TOOLS and perform the update statement manually there. And it works too. My UPDATE statement looks like this:
Update "Electricity Monthly Totals"
Set "Cost" =
(SELECT SUM( ( "NoUnits" * "Power" * ( ( "OpDays" * 24 ) / 1000 ) ) * .09983 ) +
( SELECT SUM( "Cost" ) FROM "Expenses Table" WHERE "ExpCatID" = '8' AND ( "AcctID" BETWEEN '4999000' AND '4999999' ) AND ( "Date" BETWEEN '2024-05-01' AND '2024-05-31' ) AND "ConJobID" = '14' ) +
( SELECT SUM( "Cost" ) FROM "Expenses Table" WHERE "ExpCatID" = '8' AND ( "AcctID" BETWEEN '4999000' AND '4999999' ) AND ( "Date" BETWEEN '2024-05-01' AND '2024-05-31' ) AND "ConJobID" = '13' ) AS "May 2024 Total Bill"
FROM "Elec Est Table" WHERE "FinishDate" BETWEEN '2024-04-16' AND '2024-05-15' )
Where "ElecTotalID" = '9'
But alas, when I try to place this update statement in a macro, correct syntax as best as possible, and assign to a button, I receive syntax or no valid table errors. I think I am close, but just not quite there. Thank you for the help you have given and for any future endowment.