Results 1 to 5 of 5
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038

    CASE Statement not working

    Good morning:

    I need some assistance with updating a record set based on CASE statements (in a module). The attached ACCDB contains the following:

    1. Table "tbl_PERSON" with 20 *example* records.
    2. Form "F01_MainMenu"... containing a command button that calls function "Business Rules" (BR).


    3. Module "mod_BusinessRules" which contains BR function.

    Process:
    - Open up form and click command button
    - Based on the BR, I want to update field [PERS_INJURY_COST].

    What I need some help with:
    - First, I have NOT created a complete *sample* record set that is representative of *all* BR.
    - At this time, I merely need to ensure I update the first two (2) records where [MSHP_CLASS_ID] = 1.

    Issue:
    - Record [ID] = 1 (with [MSHP_CLASS_ID] = 1) has [PERS_DATE_OF_DEATH] = Null. Based on the BR, I want [PERS_INJURY_COST] = $50,000 after executing the update routine.
    - Record [ID] = 2 (with [MSHP_CLASS_ID] = 1) has [PERS_DATE_OF_DEATH] Not Null. Based on the BR, I want [PERS_INJURY_COST] = $100,000 after executing the update routine.
    - At this time, however, the value in [PERS_INJURY_COST] does NOT get updated for neither 2 records.

    Question:
    - How do I need to modify the CASE statement (where CASE = 1) AND/OR the sub (nested) CASE statement so the either 50,000 or 100,000 will be applied to the 1st 2 records?

    Thank you,
    EEH
    Attached Thumbnails Attached Thumbnails Example.JPG  
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Why is PERS_DATE_OF_DEATH field a text and not a date/time type? How could this field ever have 0 value?

    0 is Access constant for False (or Not True because anything not -1 will be treated as False). So, your condition of Not 0 means NOT False, which evaluates to TRUE (-1).

    However, if PERS_DATE_OF_DEATH is Null neither condition can be met because Null cannot be compared with anything since Null is nothing.

    Instead, use:

    Case Else

    Or use IIf() instead of the inner Case.

    With the data shown, both of those 2 records will match Case Else.
    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.

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    June:

    Correct... "PERS_DATE_OF_DEATH" should be a date. However, source data type is set to "text" and won't be changed.

    That said, I tried the IF version instead. Only the ELSE kicks in and updates the cost to $100k for both records. Not entirely certain as to why only the Else will be found.

    My question: Given that one record has a value (i.e., although it's a date in text format, the record is not empty), why would only the Else "kick" in? How should I change the below that $50 will be added where a date (even though it's in text format) exist and the 2nd record receives a $100k value?

    Code:
        Select Case rs!MSHP_CLASS_ID
        
            'Class A
            Case 1
                    
                If rs!PERS_DATE_OF_DEATH = Null Then
                
                    rs!PERS_INJURY_COST = 50000
                    
                Else
                    
                    rs!PERS_INJURY_COST = 100000
                
                End If

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I said that would happen. The Else kicks in because Case 0 translates to Case False. Neither Null nor 4/22/2021 equate to False.

    I also said cannot compare anything to Null. If s!PERS_DATE_OF_DEATH = Null is meaningless. Consider:

    rs!PERS_INJURY_COST = IIf(Nz(rs!PERS_DATE_OF_DEATH, "") = "", 50000, 100000)

    or

    rs!PERS_INJURY_COST = IIf(IsDate(rs!PERS_DATE_OF_DEATH), 100000, 50000)
    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.

  5. #5
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Thank you... that worked great! Appreciate the assistance.

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

Similar Threads

  1. Case statement not working as Expected
    By Dave14867 in forum Access
    Replies: 5
    Last Post: 08-07-2020, 09:14 AM
  2. Replies: 5
    Last Post: 04-26-2019, 02:57 PM
  3. CASE STATEMENT or IF-ELSE-THEN? HELP
    By Shakenaw in forum Access
    Replies: 9
    Last Post: 06-08-2015, 11:24 AM
  4. Case Statement
    By RussH in forum Programming
    Replies: 1
    Last Post: 08-12-2013, 02:50 PM
  5. if or case statement and how
    By Dannat in forum Queries
    Replies: 1
    Last Post: 01-31-2012, 10:35 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