Results 1 to 10 of 10
  1. #1
    laavista is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2010
    Posts
    42

    Nested IIF statements?

    I'm trying to display the number of active cases for each CaseCode

    fields:


    CaseID, number long
    casecode, text, 3 char
    CaseStatus, text, 1 char (A or I)

    data:
    CaseID
    CaseCode
    CaseStatus
    1
    SOC
    A
    2
    TMM
    A
    3
    SOC
    I
    4
    SOC
    A

    I need to display the number of active cases , e.g.,
    SOC : 2
    TMM: 1

    In a form, I put the following in a text box and it sums the total number of active cases:
    =sum(IIF(CaseStatus = "A",1,0)) results in A=3, but now I need it to provide the total active for each CaseCode.

    I'd really appreciate any suggestions.

  2. #2
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    Just use a query:

    Code:
    SELECT tblCase.CaseCode, Count(tblCase.CaseCode) AS ActiveCases
    FROM tblCase
    WHERE (tblCase.CaseStatus)="A"
    GROUP BY tblCase.CaseCode;
    Cheers,

  3. #3
    laavista is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2010
    Posts
    42

    Still need help

    Thanks so much for responding. I apologize, but I did not give you my table definitions so the query does not work.


    I have two tables:
    tblCase,
    field: CaseID, number, long (key)
    field: CaseCode, text, 3 char
    (plus additional fields)

    tblCaseInfo,
    field: CaseID, number, long (key)
    field: CaseStatus, text, 1 char (A or I)
    (plus additional fields)

    tbl_case is a 1 to many to tbl_caseinfo


    The SQL I tried was:
    SELECT tblCase.CaseCode, Count(tblCase.CaseCode) as ActiveCases
    from tblCaseInfo INNER JOIN tblCase on tblCaseInfo.CaseID = tblCase.CaseID
    Where (tblCaseInfo.CaseStatus)="A"
    Group by tblCase.CaseCode;

    I get the error "You tried to execute a query that doesnot include the specified expression "CaseCode" as part of an aggregate function" when I try to execute the query.

    If I add the field: Casestatus to the query, I get "enter parameter value tblCase.CaseStatus.

    Any suggestions?

  4. #4
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    There's something here that doesn't make sense.

    You appear to be using the same Primary Key in both tables, then you state the relationship is one-to-many; that can't work without violating the uniqueness of the key in tblCaseInfo. If however, you mean that CaseID is only a "Foreign Key" in tblCaseInfo, then the key arrangement is better, but you're using an Active/Inactive status field in tblCaseInfo as well. This causes a logic conflict where you might have simultaneous Active and Inactive statuses for the same case.

    Please clarify.

  5. #5
    laavista is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2010
    Posts
    42

    CaseID

    You're right. The foreign key is "CaseID"--it was a typo

    I really need to think about how I present my question, and then ensure I type the fields, etc. correctly.

    Sorry.

  6. #6
    laavista is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2010
    Posts
    42

    Clarification of tables and fields

    To clarify:

    I have two tables:
    tblCase,
    field: ID, number, long (key)
    field: CaseCode, text, 3 char
    (plus additional fields)

    tblCaseInfo,
    field: CaseID, number, long (key)
    field: CaseStatus, text, 1 char (A or I)
    (plus additional fields)

    tbl_case is a 1 to many to tbl_caseinfo

  7. #7
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    At this point I'll just assume you have a CaseInfoID primary key in tblCaseInfo as well as the CaseID foreign key; but, based on your last post, it's no longer a concern.

    Can you explain the second part of my question? It boils down to asking: "Why don't you have your CaseStatus in tblCase?"

  8. #8
    laavista is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2010
    Posts
    42

    additional info

    This is an 'inherited' database and at this point cannot be changed. I have to work with what I've been given. I hope to do a redesign in the near future, but for now I have to get this one task done.

    Any suggestions?

  9. #9
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    In that case, is there any other field in tblCaseInfo that might identify the "current" CaseStatus? For example, assume these records exist in the tables:

    tblCase
    Code:
    CaseID	CaseCode
    1	SOC
    2	TMM
    3	SOC
    4	SOC
    tblCaseInfo
    Code:
    CaseInfoID	CaseID	CaseStatus
    1	1	A
    2	2	A
    3	3	I
    4	4	A
    5	1	I
    6	4	I
    7	1	A
    This is possible due to the one-to-many relationship between these tables. If you queried these tables, you would find multiple CaseStatus for cases 1 & 4. Which ones are current?

  10. #10
    laavista is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jun 2010
    Posts
    42

    Thanks

    Thanks to your questions, I was able to solve this.
    Thanks for taking the time to help me.

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

Similar Threads

  1. SQL statements from VBA
    By John Southern in forum Programming
    Replies: 12
    Last Post: 05-16-2010, 01:07 PM
  2. Muliple If, Then Statements
    By jrockusa in forum Access
    Replies: 2
    Last Post: 12-06-2009, 11:06 PM
  3. Query for IIF statements
    By SpotoR1 in forum Queries
    Replies: 2
    Last Post: 08-26-2009, 06:57 AM
  4. IIF Statements
    By JDA2005 in forum Queries
    Replies: 8
    Last Post: 07-07-2009, 04:24 PM
  5. If statements
    By Ezeecopy in forum Access
    Replies: 0
    Last Post: 03-24-2009, 04:54 AM

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