By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,107 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,107 IT Pros & Developers. It's quick & easy.

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

P: 2
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!
Jun 18 '12 #1
Share this Question
Share on Google+
2 Replies


beacon
100+
P: 579
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):

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboYourComboBox_AfterUpdate()
  2.  
  3. End Sub
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboYourComboBox_AfterUpdate()
  2.  
  3.      If cboYourComboBox = "Transfer" Then
  4.           txtFirstTextBox.Visible = True     'Set text box to visible
  5.           txtFirstTextBox.Enabled = True     'Enable the text box
  6.           txtFirstTextBox.Locked = False     'Unlock the text box
  7.      Else
  8.           txtFirstTextBox.Visible = False     'Hide the text box
  9.           txtFirstTextBox.Enabled = False     'Disable the text box
  10.           txtFirstTextBox.Locked = True     'Lock the text box
  11.      End If
  12.  
  13. End Sub
  14.  
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 txtSecondTextBox:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboYourComboBox_AfterUpdate()
  2.  
  3.      If cboYourComboBox = "Transfer" Then
  4.           txtFirstTextBox.Visible = True     'Set first text box to visible
  5.           txtFirstTextBox.Enabled = True     'Enable the text box
  6.           txtSecondTextBox.Visible = False    'Hide the second text box
  7.           txtSecondTextBox.Enabled = False    'Disable the second text box
  8.      ElseIf cboYourComboBox = "BOBC" Then
  9.           txtFirstTextBox.Visible = False     'Hide the first text box
  10.           txtFirstTextBox.Enabled = False     'Disable the first text box
  11.           txtSecondTextBox.Visible = True     'Set the second text box visible
  12.           txtSecondTextBox.Enabled = True     'Enabled the second text box
  13.      Else     'Hide and disable both text boxes
  14.           txtFirstTextBox.Visible = False
  15.           txtFirstTextBox.Enabled = False
  16.           txtSecondTextBox.Visible = False
  17.           txtSecondTextBox.Enabled = False
  18.      End If
  19.  
  20. End Sub
  21.  
There are more elegant ways to write the above code, but hopefully this is simple enough to get you started.

Hope it helps,
beacon
Jun 18 '12 #2

P: 2
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!
Jun 19 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.