PDA

View Full Version : Show\Hide Text Box



TravD16v
08-09-2013, 05:14 AM
I have a Form, on this form is a drop down box ("box1") which for argument sake has three options ("Opt1","Opt2" & "Opt3")
I Also Have a Text Box ("txt1") Which Is Not Visable

What I am looking for is some code that makes "txt1" visable when "Opt3" is chosen in the drop down box
And Hided it again if "Opt1 or Opt2" is selected afterwards

I feel this should be relativly simple but am struggling to get it to work

Thankyou in Advance

HiTechCoach
08-12-2013, 09:37 AM
I have a Form, on this form is a drop down box ("box1") which for argument sake has three options ("Opt1","Opt2" & "Opt3")
I Also Have a Text Box ("txt1") Which Is Not Visable

What I am looking for is some code that makes "txt1" visible when "Opt3" is chosen in the drop down box
And Hided it again if "Opt1 or Opt2" is selected afterwards

I feel this should be relativly simple but am struggling to get it to work

Thankyou in Advance

In the After update event of the combo box try this:





If Me.Box1.Column(0) = "Opt3" Then
Me.txt1.Visible = True
Else
Me.txt1.Visible = False
End If



NOTE: In the above you will need to modify Me.Box1.Column(0) so the column index number is the column in the drop down with the text you want to match. The index starts at Zero. This means if the text Opt3" is in the second column then you would use Me.Box1.Column(1).


The above code can be simplified to this:



Me.txt1.Visible = ( Me.Box1.Column(0) = "Opt3")



NOTE: When viewing existing records, the above code from the after update event will need to be executed in the Form's On Current event.



TIP: It really helps if you will give examples of your actual database design. Then we can give you the best advice on ho to handle it with your database design.

If this were my project, then drop down combo box would be based on a look up table. The look up table would also include a field for Txt1_required as a yes/no data type. This would be added as hidden column in the drop down list.

The code can be written like this:


Me.txt1.Visible = Val(Me.Box1.Column(2))


By using this method, it is table driven. This will allow you to easily modify the drop down list without having to edit the VBA code.