Results 1 to 7 of 7
  1. #1
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544

    Question Summing/Counting data in rows

    I have a table that has a list of employees. Going across I have a column for each week in the year. For each week the box can be "Y" or "N". What I want to do is count up the number of "Y"s for each employee and update the value within a field called "CountOfY". I'm struggling with getting the count of "Y"s. Can this be done with a cross-tab? Am I going to have to use a loop in VBA? What would be best method for obtaining this data? Any websites for research? Thanks for any help you can provide.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    I don't think a crosstab will work. I believe that only works with one field, if you wanted to transform it into a calculation of somekind, but I could possibly be wrong.

    vba though, wouldn't be that difficult, but on a large dataset it might take a while. for instance, to sum all of the fields that have a "yes" value in a boolean field for every record, you can simply write something like this:

    Code:
    select id, SumOfFields([id])...
    and write a function like this:
    Code:
    Function SumOfFields(fldID As Long) As Long
                         
    On Error GoTo ErrorHandler
    
    Dim fld As Field
    Dim fldSum As Long
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("table name", dbOpenDynaset)
    
       rs.FindFirst "[ID] = " & fldID
    
    For Each fld In rs.Fields
        If fld.Name <> "ID" Then
            if fld.value = -1 then 'OR "YES" will work too
                fldSum = fldSum + 1
            end if
        End If
    Next fld
    
    SumOfFields = fldSum
    
    ErrorHandler:
       rs.Close
       db.Close
       Set rs = Nothing
       Set db = Nothing
       
    End Function

  3. #3
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    It sounds like you're making the classic "newbie" mistake of using a Table like a Spreadsheet. If you're really interested in setting up a normalized relational database I'm sure people here would be happy to help you out!

    That said, your current approach can actually be made to work. I'd strongly recommend however, that you always calculate your number of "Y" weeks "on-the-fly" since it's constantly changing. With the speed of today's computers (even last decade's computers), this can be done almost instantly.

    All you have to do is add the fields together using an IIf() statement. In the Control Source of your Form/Report, do the following:
    Code:
    =IIf([week1]="Y",1,0)+IIf([week2]="Y",1,0)+IIf([week3]="Y",1,0)
    Just replace [week1], etc. with the actual field names used. If your field names were Jan1, Jan2, and Jan3 for instance, you'd use:
    Code:
    =IIf([Jan1]="Y",1,0)+IIf([Jan2]="Y",1,0)+IIf([Jan3]="Y",1,0)
    If you're going to be saving the number to your Table though, I'd recommend using aje's VBA solution instead.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by Rawb View Post
    All you have to do is add the fields together using an IIf() statement. In the Control Source of your Form/Report, do the following:
    Code:
    =IIf([week1]="Y",1,0)+IIf([week2]="Y",1,0)+IIf([week3]="Y",1,0)
    Just replace [week1], etc. with the actual field names used. If your field names were Jan1, Jan2, and Jan3 for instance, you'd use:
    [CODE]
    =IIf([Jan1]="Y",1,0)+IIf([Jan2]="Y",1,0)+IIf([Jan3]="Y",1,0)
    I wasn't going to put the effort into explaining this exact solution, but this is exactly the problem that people run into when they treat Access like Excel. Inevitably, a question about "horizontal calculation" just like this pops up.

    Believe me, I'm not reiterating what this man just said, but it's good for you to know jg, simply so you don't run into this situation again.

  5. #5
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Thanks for the input. Unfortunately I'm not sure there is another way to do what I am doing other than treating this as an excel table. I plan on obtaining the count each week and just appending the results to the field that corresponds with the employee. In all of my time using Access I have never attempted something like this. I don't think Rawb's method will work because I have 52 columns (one for each week in the year). I do like your method, aje, and thank you for the input. I am concerned with the processing requirement but I think I'll have to live with it as this is my only known option. Thanks again for the quick response and help.

  6. #6
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    post back if more ?'s pop up.

  7. #7
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Will do. Thank you.

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

Similar Threads

  1. similar data rows trouble
    By andyf80 in forum Database Design
    Replies: 3
    Last Post: 06-11-2010, 10:06 AM
  2. New to Access- help with counting rows
    By cip315 in forum Access
    Replies: 5
    Last Post: 03-19-2010, 09:35 AM
  3. Condensing Rows of Data into one Cell
    By alexandermorris in forum Access
    Replies: 1
    Last Post: 02-25-2010, 07:47 AM
  4. Replies: 0
    Last Post: 01-24-2009, 11:40 AM
  5. Counting rows
    By anishap in forum Access
    Replies: 0
    Last Post: 10-08-2008, 10:41 PM

Tags for this Thread

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