Results 1 to 8 of 8
  1. #1
    jworegon is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2025
    Posts
    2

    Need help with this query

    The following query (which isn't working) works off of the MonthYear field in the table tblPayrollRecords, which is a date field (e.g., 1/1/2025). If the current month is January, it returns the GrossAvailable value for January. If it is any other month in the year, it returns the January value plus the sum of all the GrossAvailable values for the remainder of the year.

    Here's the query that isn't working:
    PayrollAmt2: IIf((DatePart("m",[MonthYear]))=1,
    [GrossAvailable],
    ([GrossAvailable]+DSum("GrossAvailable","tblPayrollRecords","tblPay rollRecords.EmployeeID= “ & [EmployeeID] & “ And [MonthYear] >= #"
    & DateSerial(Year(Date()),1,1) &


    "# And [MonthYear] <= #"
    & DateSerial(Year(Date()),DatePart("m",[MonthYear])-1,1) & "#")))

    BUT, if I insert a specific EmployeeID value, as below, it works properly.

    PayrollAmt2: IIf((DatePart("m",[MonthYear]))=1,
    [GrossAvailable],
    ([GrossAvailable]+DSum("GrossAvailable","tblPayrollRecords","tblPay rollRecords.EmployeeID= 48 And [MonthYear] >= #"
    & DateSerial(Year(Date()),1,1) &
    "# And [MonthYear] <= #"
    & DateSerial(Year(Date()),DatePart("m",[MonthYear])-1,1) & "#")))

    So, I'm guessing there's something wrong in the syntax, though it's not throwing any errors.

    Thanks for any help to get it working properly.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Use the full forms reference for employeeID
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    jworegon is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2025
    Posts
    2
    I've been away from Access for several years, so I'm not sure exactly how to do that. Sorry. The form that this query is in is named frmMonthlyCalculatorSubForm and the query provides the RecordSource for the subform. Would I be better off to put this formula in a separate textbox on the form, and not in the underlying form query? I apologize for my ignorance!

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    When you say it does not work, what exactly do you mean?
    You need to use the full form reference of the form where employeeid is located.
    https://www.google.com/search?q=acce...d%20in%20query
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    I would put the DSum() as a seperate field just as a test. That will allow you to use the full form reference in the Builder.
    Then you can go into sql window and amend the syntax to suit, if those links do not help you.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Your query has to reference a form that is used as a subform - that can get complicated as the full path reference must be through the subform container control. The container might have same name as the form it holds but I always name them different. So reference would be like:
    Code:
    Forms!MainformName.SubformContainerName.Form!EmployeeID
    Yes, I suggest just do this calc in a textbox on the subform. Do not have to use full form path. The calc would work whether form is used as subform or not.


    Domain aggregate functions can slow performance of query or form with large dataset.
    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.

  7. #7
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    on the Original Query you posted, you have a "slant" double qoute:

    PayrollAmt2: IIf((DatePart("m",[MonthYear]))=1,
    [GrossAvailable],
    ([GrossAvailable]+DSum("GrossAvailable","tblPayrollRecords","tblPay rollRecords.EmployeeID= & [EmployeeID] & And [MonthYear] >= #"
    & DateSerial(Year(Date()),1,1) &
    "# And [MonthYear] <= #"
    & DateSerial(Year(Date()),DatePart("m",[MonthYear])-1,1) & "#")))

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Quote Originally Posted by jojowhite View Post
    on the Original Query you posted, you have a "slant" double qoute:

    PayrollAmt2: IIf((DatePart("m",[MonthYear]))=1,
    [GrossAvailable],
    ([GrossAvailable]+DSum("GrossAvailable","tblPayrollRecords","tblPay rollRecords.EmployeeID= & [EmployeeID] & And [MonthYear] >= #"
    & DateSerial(Year(Date()),1,1) &
    "# And [MonthYear] <= #"
    & DateSerial(Year(Date()),DatePart("m",[MonthYear])-1,1) & "#")))
    Good spot!
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 06-29-2018, 01:22 PM
  2. Replies: 28
    Last Post: 11-09-2015, 11:15 AM
  3. Replies: 3
    Last Post: 07-09-2015, 08:27 AM
  4. Replies: 2
    Last Post: 04-11-2014, 02:27 PM
  5. Need to use access for this, need some help
    By Nelson12 in forum Access
    Replies: 3
    Last Post: 01-29-2010, 09:33 AM

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