Results 1 to 11 of 11
  1. #1
    arothacker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jan 2014
    Posts
    43

    Sum field 1 if field 2 equals a value

    I have a form called "ViewHeadcount" that is linked to the table "Updated Headcount"



    On the form there are fields "Employee Type" and "Count". There is a textbox at the top that I want to sum the values in "Count" for all the records that "Employee Type" = Salary.

    I tried putting =Sum(IIf([Employee Type]="Salary",[Count],0)) but I get #Error. Any ideas??

    Thanks!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Not sure if this would work, but perhaps something like:
    =DSum("[Count]", "[YourTableOrQueryName]", "[Employee Type] = Salary")
    You would need to replace [YourTableOrQueryName] with the domain (table or query) used for the form.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    arothacker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jan 2014
    Posts
    43
    I tried putting =DSum([Count],[qry_ViewHeadcount],[Employee Type]="Salary") and I get a #Name? error now

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Try:
    =DSum("[Count]","[qry_ViewHeadcount],"[Employee Type]=Salary")
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    arothacker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jan 2014
    Posts
    43
    It says it has invalid syntax. Maybe it's how everything is connected....

    I have qry_viewheadcount that the user is prompted to select values to filter. Once they select the values the records are filtered and displayed on the form "Headcount". At the top of this form I have a textbox that I want to sum the "Count" field whenever the "Employee Type" field = Salary. Also the Employee Type field is a combobox...would that have anything to do with it?

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Type field is a combobox...would that have anything to do with it?
    I think it may.
    What is the SQL of the combo box.
    What are it Column widths and Column Count property settings.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Combobox could be issue. Is this because of lookup set in table? I NEVER set lookups in table, especially if there is an alias. Possibly the actual value of the field is not "Salary" but a numeric ID. Change the control to a textbox. What value displays?

    Did you put the textbox with Sum expression in a header or footer section? That expression should work with proper criteria.

    If you want to provide db for analysis, follow instructions at bottom of my post. Identify object involved.
    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
    arothacker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jan 2014
    Posts
    43
    The field is called Employee Type and is a drop down with the choices of "Salary", "Wage", and "Other". We're trying to display at in the header the sum of the "Count" field for Salary, Wage, and Other. So we would have 3 textboxes at the top with 3 different sum values.

    I unfortunately can't provide the database because it has confidential information in it.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Bob forgot apostrophe delimiters.

    =DSum("[Count]","[qry_ViewHeadcount],"[Employee Type]='Salary'")

    However, try SQL aggregate instead of domain aggregate:

    =Sum(IIf([Employee Type]="Salary",[Count],0))
    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
    arothacker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jan 2014
    Posts
    43
    Hm that's bizarre. I used that equation before (look at first post) and it was giving me an error but now it seems to be working! Not sure why haha but thanks everyone!

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Count is a reserved word. Should avoid reserved words as field names.
    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.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-27-2013, 02:33 PM
  2. Return rows where value equals drop down
    By AccessLiability in forum Forms
    Replies: 1
    Last Post: 07-02-2012, 07:15 AM
  3. Replies: 8
    Last Post: 03-18-2012, 11:17 AM
  4. Replies: 1
    Last Post: 03-03-2012, 10:17 PM
  5. Is the Access 'Equals' operator broken?
    By AccessPonderer in forum Queries
    Replies: 0
    Last Post: 03-18-2011, 09:00 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