Make a field appear or not in Form

stephen

New member
Local time
Today, 04:18
Joined
Nov 13, 2008
Messages
6
When a certain selection is made in a combo box on a form, I would like another field from the same table to appear. How do I make this happen?
 
You will need to do a logical test on the value of the combo in the on current event of the form and the on change event of the combo. it would look something like;
Code:
[COLOR="Green"]'This code will hide or show the combo based on the selection that has been made in the combo[/COLOR]
If Me.ComboName.Value = X Then [COLOR="Green"]'Where X = the value of the field[/COLOR]
     Me.TextBoxName.Visible = True
Else
     Me.TextBoxNmae.Visible = False
End IF

If how ever you wish to hide the field until such time as a selection has been made in the combo put the following code in the On Current event of the form and also in the on change event of the Combo;

Code:
[COLOR="Green"]'This code will hide the named text box until a selection has been made in your Combo, assuming that it does not have a default value[/COLOR]
If Isnull(Me.ComboName) then
     Me.TextBoxName.Visible = False
Else
     Me.TextBoxNmae.Visible = True
End IF
 
John Big Booty,

My need falls into your first scenerio. It didn't work, but the field in the form disappeared, instead of showing up all the time. I just need my 'Piston Ring' Field to show when the 'Body Type' is ED. Here's my version:

Private Sub Body_Type_Change()
'This code will hide or show the combo based on the selection that has been made in the combo
'Where X = the value of the field
If Me.Body_Type.Value = ED Then
Me.Piston_Ring.Visible = True
Else
Me.Piston_Ring.Visible = False
End If
End Sub

I wonder about your assumption that I want a text box to show when a certain selection is made in my Combo Box. Really I have two Combo Boxes. Would that make a difference in the coding that you have so generously provided me. I pasted the above coding directly from my, 'on change event' of the combo box. Do you see any errors here. Am I to assume that no coding would go into 'any event' for my 'Piston Ring' Combo Box, only the Form and the Combo Box initiating the trigger?

Thank you so much,

steve
 
I got it working with:

Private Sub Body_Type_Change()
'This code will hide or show the combo based on the selection that has been made in the combo
'Where X = the value of the field
If Me.Body_Type.Value = "ED" Then
Me.Piston_Ring.Visible = True
Else
Me.Piston_Ring.Visible = False
End If
End Sub
 
Remember, as John said, the code also needs to be in the Form_Current event, as well.
 
missinglinq,

I did put it in there as well, but didn't paste that in on my reply. I was curious though, has there been an earlier or newer version that didn't require the quotes? I am using Access 2003. Thank you so much. Now I can move on to making selections grey out or not show in a Combo box drop down when they are not appropriate or conflict with another selection. Any thoughts on that?

stephen
 
Test them in a similar manner but use the .enable property for the desired control!
 
missinglinq,

I did put it in there as well, but didn't paste that in on my reply. I was curious though, has there been an earlier or newer version that didn't require the quotes? I am using Access 2003. Thank you so much. Now I can move on to making selections grey out or not show in a Combo box drop down when they are not appropriate or conflict with another selection. Any thoughts on that?

stephen

You will require quotes if you are testing against a Text variable, however quotes are not required if you are testing against an integer. As you didn't specify how your combo was set up I wasn't able to be more specific, But you did work it out in the end. Good work.
 
missinglinq,

..........Now I can move on to making selections grey out or not show in a Combo box drop down when they are not appropriate or conflict with another selection. Any thoughts on that?

stephen

To hide the Combo use;
Code:
me.ComboName.Visible = False

You will need to have some code at some stage to unhide it when you need it again, simply replace False with True to Un hide.

To grey out the combo use;
Code:
me.ComboName.Enabled=False
Once again change False to True to un-Grey it.
 
Now I can move on to making selections grey out or not show in a Combo box drop down when they are not appropriate or conflict with another selection.

As an addendum, you will generally want to stick the code in the AfterUpdate event of the initial combo box (or check boxes/whatever) as to whether I want the next controls to be enabled or not.

If I want cmbNumberTwo to be disabled when cmbNumberOne contains a certain value I'd generally do something like:

Code:
If me.cmbNumberOne = SomeValue Then
   me.cmbNumberTwo.Enabled = False
EndIf

in the AfterUpdate event of cmbNumberOne.

HTH.
 
I don't want to hide the whole combo box, I want to make it so that if in combo box Body_Type="ED", then when I go to combo box Body_Size and click on the down arrow to veiw the possible choices, only the choices that are valid for the ED will be available to be chosen. The other choices that aren't valid would be Grayed out or not there at all.
Its either make the invalid choices gray out or disappear, or I'll have to make special field just for ED Body Sizes. I would rather keep all body size selections in one field.

Stephen
 
I don't want to hide the whole combo box, I want to make it so that if in combo box Body_Type="ED", then when I go to combo box Body_Size and click on the down arrow to veiw the possible choices, only the choices that are valid for the ED will be available to be chosen. The other choices that aren't valid would be Grayed out or not there at all.
Its either make the invalid choices gray out or disappear, or I'll have to make special field just for ED Body Sizes. I would rather keep all body size selections in one field.

Stephen
You need cascading combo boxes mate. Search for em, lots of threads on this, and even more samples.
 
This almost works for my case too. Except that it just grays the boxes out completely. So that I cannot update either one... If someone can help me gray out one field if another field is filled in on the form. Example: If textbox containing Mass is filled in then gray out Volume and if Mass is not filled in then gray out Mass and allow to fill in Volume.


Any help will be appreciated!
 
Last edited:

Users who are viewing this thread

Back
Top Bottom