Results 1 to 12 of 12
  1. #1
    PSSMargaret is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    55

    Lock Fields if Another Field is Not Null

    I have a form that the user should only enter one of three scenarios.



    Enter data in Textbox 1 and Textbox 2 or
    Enter data in Textbox 3 or
    Enter data in Textbox 4

    If data is entered into Textbox 1 and Textbox 2, I would like textboxes 3 and 4 to be locked.
    If data is entered into Textbox 3, I would like textboxes 1, 2 and 4 to be locked
    If data is entered into Textbox 4, I would like textboxes 1, 2, and 3 to be locked.

    I implemented this one step at a time with the below code and haven't gotten past the first step. While it does work by locking Textbox 1 when I populate Textbox 3, if I remove the data from Textbox 3, it never unlocks Textbox 1.

    I also tried it differently by creating a function and then calling it in the Textbox3_AfterUpdate event and the Form_Current event but it never seems to unlock Textbox 1 if I populate Textbox 3.

    Any guidance on how to implement this would be greatly appreciated.


    Code:
    Private Sub Form_Current()
    
    
        If Not IsNull(Me.Textbox3) Then
            Me.Textbox1.Locked = True
        Else
            Me.Textbox1.Locked = False
        End If
    
    
    End Sub

    Code:
    Private Sub Textbox3_AfterUpdate()
        
        If Not IsNull(Me.Textbox3) Then
            Me.Textbox1.Locked = True
        Else
            Me.Textbox1.Locked = False
        End If
        
    End Sub

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Try:

    Private Sub Textbox3_AfterUpdate()

    If IsNull(Me.Textbox3) Or Me.Textbox3 = "" Then
    Me.Textbox1.Locked = False
    Else
    Me.Textbox1.Locked = True
    End If

    End Sub

    Put a breakpoint at the If line and step through, make sure it is doing what you want it to do based on the data in that text box.

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Add this sub:
    Code:
    private sub subBoxes(arg as long)
        txtbox1.locked = false
        txtbox2.locked = false
        txtbox3.locked = false
        txtbox4.locked = false    
        select case arg
            case 1
                if not isnull(txtbox2) then
                    txtbox3.locked = true
                    txtbox4.locked = true
                endif    
            case 2
                if not isnull(txtbox1) then
                    txtbox3.locked = true
                    txtbox4.locked = true
                endif
            case 3
                txtbox1.locked = true
                txtbox2.locked = true
                txtbox4.locked = true    
            case 4
                txtbox1.locked = true
                txtbox2.locked = true
                txtbox3.locked = true
        end select
    end sub
    Then in the after_update events:
    for txtbox1: If not isnull(txtbox1) then call subBoxes(1)
    for txtbox2: If not isnull(txtBox2) then call subBoxes(2)
    and so on.

    Instead of the after_update event you may have to use the lost_focus event in case the textbox is entered and then exited without any change. In that case the after_update event will not fire.

  4. #4
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    A variation to consider. In the AfterUpdate event of each textbox, call this private sub to control locking/unlocking.
    Code:
    Private Sub LockStatus ()
        If Len(Me.Textbox1 & vbNullString) <> 0 And Len(Me.Textbox2 & vbNullString) <> 0 Then
            Me.Textbox3.Locked = True
            Me.Textbox4.Locked = True
        Else
            Me.Textbox3.Locked = False
            Me.Textbox4.Locked = False
            
            If Len(Me.Textbox3 & vbNullString) <> 0    Then
                Me.Textbox1.Locked = True
                Me.Textbox2.Locked = True
                Me.Textbox4.Locked = True
            Else
                Me.Textbox1.Locked = False
                Me.Textbox2.Locked = False
                Me.Textbox4.Locked = False
            End If
    
    
            If Len(Me.Textbox4 & vbNullString) <> 0    Then
                Me.Textbox1.Locked = True
                Me.Textbox2.Locked = True
                Me.Textbox3.Locked = True
            Else
                Me.Textbox1.Locked = False
                Me.Textbox2.Locked = False
                Me.Textbox3.Locked = False
            End If
        End If
    End Sub

  5. #5
    PSSMargaret is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    55
    Thank to all who responded. Truly appreciated. Unfortunately, I haven't been able to get any of them to work. Not sure what I'm doing wrong so I'm going to pick one to respond to and hopefully figure out what's wrong.

    Getting mixed results with the code from jwhite. If I implement the code as suggested, I get the below results:
    Scenario #1 - If data is entered into Textboxes 1 and 2. It won't allow data entry in Textboxes 3 and 4.
    Scenario #2 - If data is entered into Textbox 3 it allows data entry in Textboxes 1 and 2 but it doesn't allow data entry in Textbox 4. It won't allow me to go back and delete the data in Textbox 3 but I can go back and delete the data in Textboxes 1 and 2.
    Scenario #3 - If data is entered into Textbox 4 it won't allow data entry into Textboxes 1, 2 and 3.

    I also tried calling this code with the textboxes' GotFocus events and received the below results:
    Scenario #1 - If data is entered into Textboxes 1 and 2, it allowed data entry in Textbox 3 but not Textbox 4. It would not allow me to go back and delete the data in TextBox 3 but I could go back and delete the data in Textboxes 1 and 2.
    Scenario #2 - If data is entered into Textbox 3, it allowed data entry in Textboxes 1 and 2 but not Textbox 4. It would allow me to go back and delete the data in Textbox 3 as well as Textboxes 1 and 2.
    Scenario #3 - If data is entered into Textbox 4, it allowed data entry in Textbox 3 but not Textboxes 1 and 2. It would not allow to me to go back and delete data from either Textbox 4 or 3.

    Should the first If statement have it's own End If?

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Attachment 28656
    Too complex to display here. Try the database. The form shows green and red lights next to the textboxes to show if locked or not.

  7. #7
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Did you consider disabling rather than locking? A locked text box isn't obvious to a lot of users.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I had an issue with the posted db (Method or Data Member not found). Perhaps it depends on another db for a library reference. Anyway, this does work provided the control names match.

    Code:
    Private Sub textbox1_AfterUpdate()
    
    If Not IsNullEmpty(Me.textbox1) Then
     DisableControl Me.textbox3, True
     DisableControl Me.textbox4, True
    Else
     DisableControl Me.textbox3, False
     DisableControl Me.textbox4, False
    End If
    
    End Sub
    
    Private Sub textbox2_AfterUpdate()
    
    If Not IsNullEmpty(Me.textbox2) Then
     DisableControl Me.textbox3, True
     DisableControl Me.textbox4, True
    Else
     DisableControl Me.textbox3, False
     DisableControl Me.textbox4, False
    End If
    
    End Sub
    
    Private Sub textbox3_AfterUpdate()
    
    If Not IsNullEmpty(Me.textbox3) Then
     DisableControl Me.textbox1, True
     DisableControl Me.textbox2, True
     DisableControl Me.textbox4, True
    Else
     DisableControl Me.textbox1, False
     DisableControl Me.textbox2, False
     DisableControl Me.textbox4, False
    End If
    
    End Sub
    
    Private Sub textbox4_AfterUpdate()
    
    If Not IsNullEmpty(Me.textbox4) Then
     DisableControl Me.textbox1, True
     DisableControl Me.textbox2, True
     DisableControl Me.textbox3, True
    Else
     DisableControl Me.textbox1, False
     DisableControl Me.textbox2, False
     DisableControl Me.textbox3, False
    End If
    
    End Sub
    
    Private Sub DisableControl(ctl As Control, mode As Boolean)
    
    If mode = False Then 'control is not to be disabled
      ctl.Enabled = True
    Else
      ctl.Enabled = False
    End If
    
    End Sub
    and if this function is in a standard module (could be in the form module as well, but then can't be used by any other db code)
    Code:
    Public Function IsNullEmpty(ctl As Control) As Boolean
    
    IsNullEmpty = False
    If IsNull(ctl) Or ctl = "" Then IsNullEmpty = True
    
    End Function
    As long as you have a way of navigating off of the only enabled control when you want to delete its contents. Tab will work, but not all users may know that.
    Feel free to lock instead of disable.
    Last edited by Micron; 05-15-2017 at 05:56 PM. Reason: code correction
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    PSSMargaret is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    55
    The code provided by davegri (Post #6) does work but only after data is entered into one of the textboxes. If you leave the record and then go back you can click on one of the other textboxes and it allows you to enter data. Do you think something like this would work for the four textboxes GotFocus events?

  10. #10
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Attachment 28657
    OK, you didn't mention the form reacting to existing data scrolling. So take a look at this.
    Last edited by davegri; 05-15-2017 at 11:56 PM. Reason: hold temper

  11. #11
    PSSMargaret is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2016
    Posts
    55
    Davegri,

    My apologies for not mentioning existing data scrolling. Sometimes you don't know what you need until you implement something and then it leads to another needed feature.

    The code works as far as locking the cells. The back coloring of the cells is a little wonky. It doesn't always display correctly in a continuous form until you click on a record. I'm not worried about that. Was thinking of using conditional formatting for the fields back coloring anyway.

    Thank you again for your assistance. I wasn't going in this direction at all and haven't used select case before. Another new lesson. Now I have to read up on it.

  12. #12
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Attachment 28666
    Here's a version that colors the textbox.backcolor.

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

Similar Threads

  1. Fields lock randomly on subform
    By zosta in forum Forms
    Replies: 7
    Last Post: 02-10-2015, 09:24 AM
  2. Show Not Null Fields in one Field
    By Njliven in forum Queries
    Replies: 6
    Last Post: 08-14-2013, 09:25 AM
  3. Replies: 2
    Last Post: 01-03-2011, 05:17 PM
  4. Automatically Lock Fields
    By Toolman in forum Database Design
    Replies: 2
    Last Post: 05-27-2010, 10:36 AM
  5. Can I lock out fields in a form by keying off...
    By swampdonkey in forum Access
    Replies: 2
    Last Post: 09-07-2006, 07:16 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