Results 1 to 11 of 11
  1. #1
    rdougherty is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107

    Case statement to update empty form field with parent values

    Hello,

    I am trying to get child records to auto-populate a date from the parent record, and there are three situations.

    Case 1: If Rejected_Date is not null, use it.
    Case 2: If Accepted Date is not null, use it instead.
    Case 3: Both are null, and the user needs to be forced to enter one before continuing.

    Here is my code thus far: (it is currently throwing a compile error "Statement invalid outside Type block")

    Can you see what I am doing wrong? THanks!


    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        
        Dim msgrslt As Variant
    
    
        If IsNull(Me.Defect_Date) = True Then
        
        Select Case Me.Defect_Date
        Case IsNull([Forms]![frm_final_inspection].Rejected_Date) = False
          Me.Defect_Date = [Forms]![frm_final_inspection].Rejected_Date
        Case IsNull([Forms]![frm_final_inspection].Accepted_Date) = False
          Me.Defect_Date = [Forms]![frm_final_inspection].Accepted_Date
        Case IsNull([Forms]![frm_final_inspection].Rejected_Date), IsNull([Forms]![frm_final_inspection].Accepted_Date)
            msgbox("Rejection Date is a required field.  Please enter a value before continuing.", vbOKOnly, [Required Field]) As msgrslt
        Exit Sub
          
      End Select
        End If
    End Sub

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    The Case evaluation statements under "Select Case Performance" must refer to the value of Performance.

  3. #3
    rdougherty is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107
    Oh crap, I had a copied snippet from Microsoft's explanation for "Performance" because they entered it as a function()...

    I replaced with Select Case Me.Defect_Date and it throws the same error...

    Still not working!

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    None of your Case statement refer to a value of Defect_Date.
    They refer to Rejected_date and Accepted_Date.

    You should code this as If---Else. Scrap the select case.

  5. #5
    rdougherty is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107
    The value on the child record is Defect_Date, but the dates I want to grab from are on the Parent record, Accepted_Date and Rejected_Date...

    I see your point about If--Else, but I was just trying to use this opportunity to learn Case statements. I'll try that instead, perhaps.

    Thank you,

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You could use something like this or break it down into smaller pieces as desired. Don't forget to Cancel the BeforeUpdate event if the validation returns False.
    Code:
    Me.Defect_Date=IIf(Not Isnull([Forms]![frm_final_inspection].Accepted_Date),[Forms]![frm_final_inspection].Accepted_Date,IIf(Not Isnull([Forms]![frm_final_inspection].Rejected_Date),[Forms]![frm_final_inspection].Rejected_Date,InputBox("Rejection Date is a required field! Please enter a date!","Required Field")))
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Sometimes I use case blocks that evaluate to True. That way you can use a mix of references with logical operators as in

    Case A = B And C = D
    Case E = F

    Just be aware that case blocks stop evaluating as soon as the first case is satisfied.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    rdougherty is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107
    thanks all for the different options...

    I got it to work with the if then statement, and it seems the issue was my syntax or usage of the msgbox line originally... just making it a very basic msgbox with no arguments other than the text, works. But, I don't know why it was giving error before...

    The following code is not stopping the save of the record and returning the cursor to the Defect_Date field... should I be using a different event, or do I have this coded incorrectly?

    Code:
     Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.Defect_Date) = True Then        If IsNull([Forms]![frm_final_inspection].Rejected_Date) = False Then
            Me.Defect_Date = [Forms]![frm_final_inspection].Rejected_Date
            ElseIf IsNull([Forms]![frm_final_inspection].Accepted_Date) = False Then
            Me.Defect_Date = [Forms]![frm_final_inspection].Accepted_Date
            Else
            msgbox ("Rejection Date is a required field.  Please enter a value before continuing.")
            
            Me.Defect_Date.SetFocus
            Exit Sub
            End If
        End If
       
    End Sub

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    What about this?
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer) 
    If IsNull(Me.Defect_Date) = True Then        
    	If IsNull([Forms]![frm_final_inspection].Accepted_Date) = False Then
            Me.Defect_Date = [Forms]![frm_final_inspection].Accepted_Date  'Accepted_Date has priority
        ElseIf IsNull([Forms]![frm_final_inspection].Rejected_Date) = False Then
            Me.Defect_Date = [Forms]![frm_final_inspection].Rejected_Date
        Else
            Msgbox "Rejection Date is a required field.  Please enter a value before continuing.", vbCancel,"Required field"       
            Me.Defect_Date.SetFocus
    		Cancel=True 'to prevent saving the record
            Exit Sub
        End If
    End If
    End Sub
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    The error message you posted doesn't seem related to the problem, but to answer your question about the message box problem, it's because you must not use ( ) around the message box function unless it is returning a value.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    rdougherty is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107
    thank you Vlad and Micron!

    Those answers helped. Perhaps now I will go back and use the case statement just for practice.

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

Similar Threads

  1. Replies: 10
    Last Post: 12-29-2021, 07:53 AM
  2. Replies: 20
    Last Post: 05-13-2020, 02:49 PM
  3. In case of empty formular get all the values
    By sandordan in forum Queries
    Replies: 4
    Last Post: 03-06-2018, 03:52 AM
  4. Replies: 12
    Last Post: 10-22-2014, 02:11 PM
  5. Replies: 2
    Last Post: 04-23-2012, 10:13 PM

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