I am using a form with two combo-boxes and a button. I want button to be disabled initially and it should enable only when the user selects a valid value in both the combo-boxes.
I tried to disable the button using the form initialize sub-routine but the button appears to be active. What can be the issue? Also how to enable the button using if conditions?
Private Sub UserForm1_Initialize()
Me.Shapes("ButtonName1").ControlFormat.Enabled = False
ActiveSheet.Shapes("ButtonName1").Font.ColorIndex = 16
End Sub
You made a couple of mistakes in your code.
UserForm1_Initialize should be UserForm_Initialize
You can use Userform.ButtonName1 to access the properties of your button
Use TextFrame.Characters.Font.ColorIndex to access the text on a button on the worksheet
The code below should work
Private Sub UserForm_Initialize()
UserForm1.ButtonName1.Enabled = False
ActiveSheet.Shapes("ButtonName1").TextFrame.Characters.Font.ColorIndex = 16
End Sub
Related
Currently implementing toggle buttons that controls the visibility of tabs in a subform. When opening the subform on its own, the toggle buttons work, however when I am on the main form, the toggle buttons don't work anymore.
Private Sub Toggle53_Click()
If Me.Toggle53.Value = True Then
Me.IDD.Visible = True
Me.IDS.Visible = True
Else
Me.IDD.Visible = False
Me.IDS.Visible = False
End If
I also tried implementing this code in
Private Sub Form_Current()
for the subform's current event, but it doesn't work either.
Any help is appreciated thanks!
Don't know the structure of your forms, but to hide a tab using a toggle button, you need to reference the tab by index as shown below. Obviously, if the tab control is located in a subform control, you will need to change the reference to it.
Private Sub Toggle1_Click()
TabCtl.Pages(0).Visible = Toggle1.Value
End Sub
If the tab control is located in a subform control:
Private Sub Toggle1_Click()
SubformControl.Form.TabCtl.Pages(0).Visible = Toggle1.Value
End Sub
If all the forms are on the page you could add condittional If on them with booleans to show them, so on toggle click just set everyones boolean to false except the item you want to be visible.
For example create boolean called IDDbool=false, bind it to the subform and change it on click to true to show it.
Also option is to create method on the sub form that controls the tabs and call in that method from the main one.
I have a MS Access database that has a switchboard on it that opens different forms within the database. For some reason, some of the users like to double click the buttons that open the different forms. This is a problem because one of the forms that opens happens to have a checkbox right where the button that opens it is, so when they double click the button to open the form, it causes the checkbox to toggle and change it's value. It's a constant problem.
I tried adding a double click event handler that would essentially do nothing, but it's not firing and the form still opens and the checkbox keeps getting toggled.
I've tried user education, I've tried telling them that it's causing problems, but they don't seem to get it. Is it possible to only let a button work when it's clicked one time, and if it's clicked twice it just doesn't work, or it throws an error at them to shame them?
One way to solve this is:
When the second forms opens set the following properties in Form_Open:
Private Sub Form_Open(Cancel As Integer)
Me.AllowEdits = False ' Will prevent form from being edited
Me.TimerInterval = 500 ' Will trigger Form_Timer after 500 miliseconds
End Sub
and in the Form_Timer event (kicks in after 500 miliseconds)
Private Sub Form_Timer()
Me.AllowEdits = True ' Allow edit again
Me.TimerInterval = 0 ' Stop timer
End Sub
With this the second form will not be possible to edit the first half a second while the second click comes.
You can try this code under the double click event of the button
Private Sub Command1_DblClick(Cancel As Integer)
Cancel = True
End Sub
In my case the command button name is Command1
In my subform (VBA in ms-access) there is a button called "Test" and this button has to be set invisible after it was clicked.
So I tried:
Private Sub Test_Click()
'Do some stuff
Me.Test.Visible = False
End Sub
But this does not work. It raises an error because it is not possible to make a button invisible, which has the focus. Unfortunately the button is the only button on the form (the other controls are lables). As the button is in a subform, I tried to set the focus to a button in the parent:
Private Sub Test_Click()
'Do some stuff
call me.parent.schclose.SetFocus
Me.Test.Visible = False
End Sub
But this did not work either. 'call me.parent.schclose.SetFocus' does not raise an error but 'Me.Test.Visible = False' does like before.
So, how to make the button invisible?
What you can do is expand your user form and add a button that is visible by default. Let's say currently your user form has height of 300 and a width of 300. Expand the form to be 500X500 and place the new button on the bottom right of that form (keep the visible property set to Yes). Next add this code to the form:
Private Sub UserForm_Initialize()
Me.Height = 300
Me.Width = 300
End Sub
Now when you click on your test button it will be invisible and the new button will still be hidden because it's not within the height you set above.
Inspired by the answer of VBA_SQL_Programmer I created a empty read-only edit field with a transperent boarder (which means it is basicly invisible) in my form to set the focus to.
So my code looks like this:
Private Sub Test_Click()
'Do some stuff
call me.invisibleTextBox.SetFocus
Me.Test.Visible = False
End Sub
This works without manipulating the size of the form.
But there is a problem too, because as the focus is set to the textBox, the user can see a blinking cursor.
Currently using vba with excel 2007...
I am currently testing the capabilities of functions and am a little stuck using buttons. I have two buttons, named ONE and TWO. Pressing either calls up a function Calc with each button passing the variable of the others name. As follows:
Private Sub ONE_Click()
Calc TWO
End Sub
Private Sub TWO_Click()
Calc ONE
End Sub
Function Calc(B As CommandButton)
B.Enabled = False
End Function
My understanding is that pressing button ONE passes the variable TWO to the Function Calc, and then disables button TWO.
I also have a button labeled Reset which serves as follows:
Private Sub Reset_Click()
ONE.Enabled = True
TWO.Enabled = True
End Sub
The results of this, if I press button ONE, button TWO greys out and appears "disabled". However when I hit the reset button, it stays grey. Investigating the properties of the button reveals that it doesn't actually get disabled. I installed another button that directly disables button two, i.e. TWO.Enabled = False, instead of using a variable to do so.
When the button is directly disabled using the direct button, resetting enables the button as it should, and the properties reflect that the button is disabled.
Would anyone know why using a variable to disable a button like this results in an illusionary disabling?
And better yet, how to overcome the issue?
If you have trouble calling through an indirrect reference, then use a static parameter.
Private Sub ONE_Click()
Calc "TWO"
End Sub
Private Sub TWO_Click()
Calc "ONE"
End Sub
Now you can use that parameter to act on the object.
Function Calc(B As String)
If B = "ONE" Then
ONE.Enabled = False
ElseIf B = "TWO" Then
TWO.Enabled = False
Else
Exit Function
End If
End Function
I'm using a VBA Userform. The MassPrompt userform has a set of six GroupNames and some text boxes. Each GroupName contains two or more radio buttons.
I'd like the following code to be triggered anytime any element within the GroupName "GROnly" changes. If the user made an inappropriate button choice in "GROnly" based on the choice in another group, I'd like to display a warning message and present the MassPrompt userform again.
Right now, I've assigned the code to the one button "GROnly_yes". It works, but only when that one button is clicked. How do I position this within the UserForm code to trigger anytime a button with the GroupName "GROnly" is clicked? Thanks for looking at this.
Private Sub GROnly_yes_Click()
'Prompt if the GROnly is true and it's inappropriate for the GSetting choice
If GROnly_yes = True And GSetting_renewal = True _
Then
GROnly_yes = False
GROnly_no = True
MsgBox ("The GROnly can't be chosen with a Renewal." & vbNewLine & _
"The GROnly button has been changed to 'NO'.")
UserForm_Initialize
End If
'Other IF statements here.
End Sub
If I understood well, GRonly is the GroupBox that contains (let's say) the radio_button_1 and radio_button_2.
The reason why the code doesn't trigger is that when he/she changes the value of one radio-button is not clicking on the GroupBox, but rather changing the value of that single radio-button.
You will have to add the code to the _Change event of the radio button objects. This is an example:
Sub myFunctionalCode()
'your code here
End Sub
Private Sub radio_button_1_Change()
myFunctionalCode
End Sub
Private Sub radio_button_2_Change()
myFunctionalCode
End Sub
Thanks for the reply. That would work.
In the meantime, I came up with another alternative as well. I removed the code from:
Private Sub GROnly_yes_Click()
And I moved it to the button that the user clicks to submit the form:
Private Sub ContinueButton_Click()
In my original code I had to change UserForm_Initialize to Exit Sub to make the code work in the "submit" button.
Thanks again for your help.
Phil