Need for assistance using DSUM

Ahmed.H.Qasim

New member
Local time
Yesterday, 17:03
Joined
Mar 20, 2020
Messages
19
"I have a peculiar issue... I have a table named 'amanat' with daily data... and another table 'fordate'. I want to sum the values of the field 'pamount' based on the date 'ta'. It executes for some dates and doesn't for others, as shown in the attachment.

The desired idea: Sum the 'pamount' values from the 'amanat' table that are on or before the specified date in 'fordate'. I have constructed the following query:

DSum("pamounts", "amanat", "pdate <= #" & [ta] & "#")"


3.5
 

Attachments

What format is your ta date? dd/mm/yyyy?

If so that is a common issue and easily resolved- use the format function to convert it to mm/dd/yyyy
 
yes dd/mm/yyyy
1724444092515.png
 
Your query is an update query and the only values shown are current values. Change to a select query and add the calculation:
Code:
SELECT fordate.ada, DSum("pamounts","amanat","pdate<=#" & [ta] & "#") AS Expr1, fordate.ta
FROM fordate;

Also, why is fordate hidden when it enters into this question?
 
Your query is an update query and the only values shown are current values. Change to a select query and add the calculation:
Code:
SELECT fordate.ada, DSum("pamounts","amanat","pdate<=#" & [ta] & "#") AS Expr1, fordate.ta
FROM fordate;

Also, why is fordate hidden when it enters into this question?
it dosent work too ,
1724445358896.png
 
You have been told. You are using UK date format.!!! You need to use USA or unambiguous format. Just surrounding date with # is only good for USA formats.
 
If you use a subquery rather than DSum(), you won't need to worry about date formats, like
Code:
SELECT fordate.ada, ( SELECT Sum(pamounts) FROM Amanat WHERE pdate <= fordate.ta ) AS SumPriors, fordate.ta
FROM fordate;
 
I agree with the subquery and think it is more efficient. The minor drawback is the result will not allow edits.
 
another method is to use a non standard join - again not editable

Code:
SELECT ada, Sum(pamounts) as sumpriors
FROM forDate INNER JOIN Amanat ON Amanat.pdate <= fordate.ta
GROUP BY ada
 
I have my laptop back up and running, so can now post this.

Code:
Option Compare Database
Option Explicit

'Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
Public Const strcJetDate = "\#yyyy-mm-dd\#"  'Needed for dates in queries as Access expects USA but will accept ISO format.

and here is an example of using it

Code:
Set db = CurrentDb

strSQL = "INSERT INTO tblDates ( StepsDate ) SELECT DateSerial([YearNo],[MonthNo],[DayNo]) AS Dates"
strSQL = strSQL & " FROM tblDay, tblMonth, tblYear WHERE ((DateSerial([YearNo],[MonthNo],[DayNo])) Between "
strSQL = strSQL & " FROMDATE AND TODATE "
strSQL = strSQL & " AND ((IsDate([MonthNo] & "" / "" & [DayNo] & "" / "" & [YearNo]))<>False))"
strSQL = strSQL & " ORDER BY DateSerial([YearNo],[MonthNo],[DayNo])"

strSQL = Replace(strSQL, "FROMDATE", Format(Me.txtFrom, strcJetDate))
strSQL = Replace(strSQL, "TODATE", Format(Me.txtTo, strcJetDate))

or
Code:
lngDate = DLookup("StepsID", "tbldates", "Stepsdate=" & Format(Me.txtDate.Text, strcJetDate))
 
I have my laptop back up and running, so can now post this.

Code:
Option Compare Database
Option Explicit

'Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
Public Const strcJetDate = "\#yyyy-mm-dd\#"  'Needed for dates in queries as Access expects USA but will accept ISO format.

and here is an example of using it

Code:
Set db = CurrentDb

strSQL = "INSERT INTO tblDates ( StepsDate ) SELECT DateSerial([YearNo],[MonthNo],[DayNo]) AS Dates"
strSQL = strSQL & " FROM tblDay, tblMonth, tblYear WHERE ((DateSerial([YearNo],[MonthNo],[DayNo])) Between "
strSQL = strSQL & " FROMDATE AND TODATE "
strSQL = strSQL & " AND ((IsDate([MonthNo] & "" / "" & [DayNo] & "" / "" & [YearNo]))<>False))"
strSQL = strSQL & " ORDER BY DateSerial([YearNo],[MonthNo],[DayNo])"

strSQL = Replace(strSQL, "FROMDATE", Format(Me.txtFrom, strcJetDate))
strSQL = Replace(strSQL, "TODATE", Format(Me.txtTo, strcJetDate))

or
Code:
lngDate = DLookup("StepsID", "tbldates", "Stepsdate=" & Format(Me.txtDate.Text, strcJetDate))
thank you
 

Users who are viewing this thread

Back
Top Bottom