hide text boxes in a form unless specific item is checked in a combo box?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeffcityman
    New Member
    • Jun 2012
    • 2

    #1

    hide text boxes in a form unless specific item is checked in a combo box?

    I have a pretty simple database I started but am stuck at one particular point. In the database I have 1 table and 1 form. My users type data in the form only. I have a combo box in the form with the following items to choose from: AR, Increase, Transfer, ARR, ARI, and BOBC. What I would like to do is have certain fields (or text boxes) only be visible or able to type in, if the 3rd item (Transfer) in the combo box is checked. Is there an easy way to do this? I've done some searching and what I've found is related to the AfterUpdate function in properties. I'm just not tech savvy enough to know what to type in. I can provide screen shots or more specific data if needed. I've seen other posts that have some pretty involved info to enter in order to get the results I think I'm looking for, but many are worded in such a way that I'm having difficulty following, such as: Me!name.visible =false Private Sub check3_after update. I just haven't been able to follow that logic yet!
    Thanks for any and all help!
  • beacon
    Contributor
    • Aug 2007
    • 579

    #2
    Hi,

    Welcome to Bytes!

    What you're trying to do is simple enough, but it can be somewhat daunting if you've never done anything like it before.

    The first thing you need to do, with your form in Design View, is click on the combo box and navigate the properties (right-click and choose Properties if your properties aren't immediately available) for the AfterUpdate event. Click the dropdown, choose Event Procedure, then click the box next to the dropdown with the elipsis (...).

    You will then be taken to the VBA editor and a bit of code will be auto-generated for you that looks like this (where cboYourComboBox is my assumed name for your combo box...clever, I know):

    Code:
    Private Sub cboYourComboBox_AfterUpdate()
    
    End Sub
    In this code block, you will type your code to complete the action you're after. I'm going to assume you have a text box called txtFirstTextBox in order to show you a code example.

    With this control (you called them fields, but control is the appropriate term) you will identify the properties, such as visibility or type-ability (I'm not sure that's a word) in the code block like this:

    Code:
    Private Sub cboYourComboBox_AfterUpdate()
    
         If cboYourComboBox = "Transfer" Then
              txtFirstTextBox.Visible = True     'Set text box to visible
              txtFirstTextBox.Enabled = True     'Enable the text box
              txtFirstTextBox.Locked = False     'Unlock the text box
         Else
              txtFirstTextBox.Visible = False     'Hide the text box
              txtFirstTextBox.Enabled = False     'Disable the text box
              txtFirstTextBox.Locked = True     'Lock the text box
         End If
    
    End Sub
    The code above will do everything you need it to do, although you don't need all the code to accomplish what I'm sure you're after...you'll likely use some combination of the properties.

    For instance, a hidden text box doesn't really need to have an enabled/locked property set since you can't type into a hidden text box. However, if you want to make a text box visible, but not allow a user to type in the box, then you could use either the enabled property or the locked property depending on your end game.

    If you want the text box visible, but the user can't put the cursor in the textbox, you would set .Enabled to false and not worry about using .Locked. If you want the user to be able to put the cursor in the text box, but not type, then you'd set the .Locked property to false and not worry about the .Enabled property.

    There are different combinations you can use, so play around with it until you find something you like.

    As you work with this more, you may find that you want to set one text box to visible and hide another text box that was made visible by a different choice in the combo box. Here's a sample of that too, using a second assumed text box called txtSecondTextBo x:

    Code:
    Private Sub cboYourComboBox_AfterUpdate()
    
         If cboYourComboBox = "Transfer" Then
              txtFirstTextBox.Visible = True     'Set first text box to visible
              txtFirstTextBox.Enabled = True     'Enable the text box
              txtSecondTextBox.Visible = False    'Hide the second text box
              txtSecondTextBox.Enabled = False    'Disable the second text box
         ElseIf cboYourComboBox = "BOBC" Then
              txtFirstTextBox.Visible = False     'Hide the first text box
              txtFirstTextBox.Enabled = False     'Disable the first text box
              txtSecondTextBox.Visible = True     'Set the second text box visible
              txtSecondTextBox.Enabled = True     'Enabled the second text box
         Else     'Hide and disable both text boxes
              txtFirstTextBox.Visible = False
              txtFirstTextBox.Enabled = False
              txtSecondTextBox.Visible = False
              txtSecondTextBox.Enabled = False
         End If
    
    End Sub
    There are more elegant ways to write the above code, but hopefully this is simple enough to get you started.

    Hope it helps,
    beacon

    Comment

    • jeffcityman
      New Member
      • Jun 2012
      • 2

      #3
      Thanks Beacon,
      I'm still a bit lost. I can't seem to get my reply posted to you either, so I hope this isn't a duplicate reply I'm sending. I tried attaching two screen shots, to show the form I have, and the code I've typed in the VBA editor. But it requires the attachements to be a web link? I'm not sure how to do that. Is there a way I can get the two screen shots on the post, or to you?
      Thanks, I appreciate the help so far!

      Comment

      Working...