Results 1 to 10 of 10
  1. #1
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58

    Prevent Record Changes if checkbox property is true

    Trying to write some code that would prevent a user from making changes to a record unless the uncheck a field first.


    The problem is that I can get a message box to display if the property of the field is true but it saves the changes anyway.
    Here is the screenshot of the code I am trying to use...

    Private Sub Form_AfterUpdate()
    ' After updating record in Production Delivery Board, checks to see if Packed checkbox is "Yes"
    ' If so, runs append query "qryAddShipRecord" to add new record to Shipping Log table
    ' If not, then just saves record
    ' Also prevents user from making changes to the current record if 'Packed' field is checked

    If ysnPacked = True Then
    MsgBox " You must uncheck 'Packed' field in order to make any changes to this record."
    Exit Sub

    ElseIf ysnPacked = False Then
    With DoCmd
    .SetWarnings False
    .OpenQuery "qryAddShipLogRecord"
    .OpenQuery "qupUpdateShipItems"
    .SetWarnings True
    End With

    Else
    Me.Dirty = False

    End If
    End Sub

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    What is ysnPacked? Is that the name of a control? What type of control?

  3. #3
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    ysnPacked is a yes/no field that is visually a checkbox in that record on the form. It is the name of that control.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    It does not seem that your code is firing. So, maybe you can be explicit with the control name in order to make sure you are evaluating the correct thing. Also place another message box just before that line for good measure. Of course, stepping through it in Debug Mode is always an option.
    Code:
    msgbox "Hello from the procedure! " & Me.ysnPacked.Value
    If Me.ysnPacked.Value = True Then
        MsgBox " You must uncheck 'Packed' field in order to make any changes to this record."
        Exit Sub
    
     ElseIf Me.ysnPacked.Value = False Then
        With DoCmd

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Private Sub Form_AfterUpdate()
    I would think the form after update event would be to late to stop an update.
    Maybe the form before update event to undo an entry if ysnPacked is true would be a better event????

  6. #6
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    thanks... yes the "me." reference really helped and the message box confirmed that the field was checked or not.
    I also moved the code to the "Before Update" event. It seemed to be executing better there.

    However... the ysnPacked control could have been checked on previously.
    The idea here is to prevent changes to a record if the user doesn't first uncheck the "ysnPacked" field which is fine but the code still activates when you are turning off and then on the ysnPacked field.
    The code activates even when I turn on/off the ysnPacked field. I need the code to ignore changes to this field but still verify that the ysnPacked control is True or False.
    Thanks in advance.

  7. #7
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    Thanks for the response. I did realize the "Before Update" event would be the better choice.
    If you could look at my previous thread post I could use some help finalizing how this code works.
    Many thanks!

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Would something like this work? Try and make changes if the record is locked...
    Attached Files Attached Files

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Edit: I typed slow and did not see Steve's post. /edit

    I am not even sure when the Form's Before Update or After Update fire. I would usually use the form's Unload event to cancel closing the form. But, I am not sure what the objective is here.

    It seems there is a need to execute a couple of queries when a User is finished entering data into a form. Perhaps a button that executes the code would be a more practical approach. You could employ a MsgBox that asks for confirmation before running the queries. However, Users tend to go robotic and don't pay enough attention to the message boxes. So they might click OK, only to realize they did not want to run the update.

    Maybe a pop-up modal form would be appropriate. The pop-up form would have a check box to save the record or a check box to cancel. Then the command button on the special looking pop-up form would execute the queries or cancel, depending on the checkboxes. Just be sure to commit the values in the previous form at the appropriate time.

  10. #10
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    Thanks all for your responses. It is a huge help.
    I am going with ssanfu's response. That seems to do what I need directly. It only allows editing of the record when the "ysnPacked" field is OFF. You nailed it!

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

Similar Threads

  1. Replies: 7
    Last Post: 08-02-2016, 01:19 PM
  2. Replies: 1
    Last Post: 03-21-2016, 07:32 PM
  3. On Click event filter By checkbox = True
    By nick404 in forum Programming
    Replies: 14
    Last Post: 06-05-2015, 03:08 PM
  4. Replies: 3
    Last Post: 03-08-2013, 11:34 AM
  5. if checkbox= true subtract 8.75 from A to=B
    By VanillaAwesome in forum Queries
    Replies: 6
    Last Post: 07-28-2012, 12:48 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