Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command button visible, based on value in combo box.

Status
Not open for further replies.

nrugado

Programmer
Dec 10, 2002
42
US
I would like to have a command button that is visible only when the value in a list box is =1. When it is anything other than 1 I want the command button to be invisible. Is there an easy way to do this in VB? Anyone have any sample code? Any help would be greatly appreciated.

Thanks Nick
 
Nick,

It's quite easy. You'll want code in the AfterUpdate event of the combo box and the Current event of the form, so that the change happens in reaction to both changing the value in the combo box and moving to a new record.

The code's quite simple. I'll assume you've got a combo box named cbo1 and the button is btn1

Me!btn1.visible = (me!cbo1 = 1)

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Affordable Development, Professionally Done

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
I am not getting the results that I would have liked to.
I should have given more detail such as the data that is being displayed is in a subform. Does that make a difference? Which on current event should it belong to the main form or the sub form where the controls are?

The result that I am getting is that the command button is invisible on any condition.

Your answer also prompted me to look up visual basic help and I tried an if then statement to accomplish the same thing. That failed also.

Here are the two code examples that I tried....

***Me!cmdRESPONDTOTECH.Visible = (Me!selStatusID = 4)

***Private Sub Form_Current()
If Me!selStatusID = 4 Then
Me!Visible = True
Else
Me!cmdRESPONDTOTECH.Visible = False
End If

End Sub

 
The first solution should work so long as you got the object names right and got the value right (it's 4 now, you had said 1 in your first post).

The second one can't work, but could if it was rewritten to
Private Sub Form_Current()
If Me!selStatusID = 4 Then
Me!cmdRESPONDTOTECH.Visible = True
Else
Me!cmdRESPONDTOTECH.Visible = False
End If

End Sub

You should be using the names of the controls here, not the names of the fields to which they're bound.

Also, make sure [event procedure] shows up in the properties box for the event you're trying to use, and that clicking on the three dots takes you to the place where this code is.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Affordable Development, Professionally Done

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
I think the problem that you are running into is the caused by the "Me!" in the code.
In your case you will have to write something like this:

NameOfYourForm.NameOfYourCommandButton.Visible = False
and
NameOfYourForm.NameOfYourCommandButton.Visible = True

You will have to put this code into the appropriate events ofcourse.
 
JKPeus,

Why do you think that is the problem? As long as this is in the code behind the form there should be no problem with using "Me".

And if he were to use the full syntax it would be
forms!NameOfYourForm!NameOfYourCommandButton.Visible

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Affordable Development, Professionally Done

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top