How would I go about setting the 'value' of a Form Command Button to either True or False, so that say it's initial value is set to False and when clicked, it's value changes to True and then back to False again when clicked again.
Many thanks
How would I go about setting the 'value' of a Form Command Button to either True or False, so that say it's initial value is set to False and when clicked, it's value changes to True and then back to False again when clicked again.
Many thanks
By "Form" do you mean a Forms button, or do you mean an ActiveX commandbutton on a Userform?
I'll assume the latter. Set the Caption to False in the properties window. Then this is the click event handler code:
But are you sure that a commandbutton it the correct control to use? How about a checkbox?![]()
Private Sub CommandButton1_Click() CommandButton1.Caption = CStr(Not (CBool(CommandButton1.Caption))) End Sub
Hi Colin
I actually meant a Form Command Button rather than an Active X one. It is the 'State' rather than the caption that I need to change to either True or False, the logic being that when the 'State is 'True, it will show something and when clicked again, the 'State' should change to 'False', to hide the object. I hope that makes sense... The button text needs to change also...
Hi Hangman,
It sounds like a checkbox would be a more logical choice of control?
I might have misunderstood what you mean by 'state', but I've attached an example where there are two forms buttons on a worksheet. Clicking on button 1 toggles its caption between True / False and shows/hides the other button. The code is:
![]()
Sub Button1_Click() Dim btnCaller As Button Dim blnNewState As Boolean Set btnCaller = Sheet1.Buttons(Application.Caller) blnNewState = Not CBool(btnCaller.Caption) btnCaller.Caption = CStr(blnNewState) Sheet1.Buttons("Button 3").Visible = blnNewState End Sub
Many thanks Colin. I'll take a look and see if this will work for what I'm trying to do...
On first view it looks as though it will. Thank you for taking the time to look at this. I'll report back tomorrow.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks