vba userform checking the GroupName instead of an individual button - vba

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

Related

Activate form button subject to some condition

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

Conditionally run VBA code

Is it possible to have multipe buttons, lets say 'Button 1' and 'Button 2' run the same VBA code but yield a different result based on the button that was pressed?
For instance when I press button 1 I want it to go to a website, load data and put it on to Sheet 1. But when I press button 2, it goes to the same site and loads it to Sheet 2.
I know I can have multiple instances of the same VBA code (with different names) however I am hoping to simplify the code and prevent it from being overly complicated.
If you are using a Forms button you can assign the same macro and use Application.Caller to return the name/id of the calling button.
Sub Test()
MsgBox Application.Caller & " was pressed"
End Sub
Create one sub to do the work and pass the sheetname as an argument to that sub. I did it with a string variable, but you can do it with a worksheet variable as well.
Sub Button1_Click()
LoadWebsiteToSheet "Sheet1"
End Sub
Sub Button2_Click()
LoadWebsiteToSheet "Sheet2"
End Sub
Sub LoadWebsiteToSheet(sName as String)
'... code to load website to Worksheets(sName)
End Sub
I was leaning more towards brettdj's solution as well
If you assign the same macro to many buttons, you will get different results based on the button name.
You could name the button to the sheet you wanted. Practice with this. Add several buttons and assign this code to them. Click each button to see what happens.
Sub GetButtonName()
Dim Btn As String
Btn = ActiveSheet.Shapes(Application.Caller).Name
MsgBox Btn
End Sub

What's the best practice for getting a value from a combobox on a userform?

There's a lot of information and a lot that you can do with userforms but I can't really find a standard way to use them. Let's say I have a userform with a standard dropdown list that asks someone to choose a fruit. In the userform code I will put the below code after adding a combo box called fruitcombo:
Private Sub UserForm_Initialize()
userform1.fruitcombo.AddItem "Peach"
userform1.fruitcombo.AddItem "Pear"
userform1.fruitcombo.AddItem "Grape"
End Sub
I will also add a commandbutton which will be labeled "Submit" and in that event:
Private Sub Submit_Click()
Me.Hide
End Sub
That's where it starts to get hazy. What's the best way to capture the answer that was selected? One way I can think would be to make a global variable called fruitanswer and then instead of the Me.Hide we can skip straight to Unload Me
Ex:
Private Sub Submit_Click()
fruitanswer = fruitcombo.value
Unload Me
End Sub
Or we can have fruitanswer as a private variable in the module where the userform is called and then unload it in there. There are also multiple ways to initialize the userform. I'm also wondering the best way to initialize it. The Show method will automatically initialize it, but the Hide method WON'T automatically de-initialize it. For that, the Unload statement is necessary. So does anyone initialize it before calling the Show method using the Load statement?
Ex:
Load userform1 'Any point to including this?
userform1.show
'user chooses a fruit and clicks submit button
'userform is hidden by commandbutton but not unloaded yet
fruitanswer = fruitcombo.value
Unload userform1
Out of these options, which is the best method? Is there anything to make it more efficient?
You can wrap the whole thing in a single function call:
strFruit = UserForm1.GetFruit()
Then, in your UserForm, have it do the work of displaying and unloading itself like so:
Private bOK As Boolean
Public Function GetFruit() As String
bOK = False
Me.Show vbModal
If bOK Then GetFruit = ComboBox1.Text
Unload Me
End Function
Private Sub cmdOK_Click()
bOK = True
Me.Hide
End Sub
Private Sub cmdCancel_Click()
Me.Hide
End Sub
This assumes you have buttons named cmdOK and cmdCancel and a combobox named ComboBox1.
Since Show() is being called modally, the code after it won't execute until the form is closed or hidden. When either button is clicked (or the form is closed by other means) then the code continues, the selected text is returned (if OK was clicked), and the form unloads itself.
The beauty of doing it this way is that your calling code doesn't need to worry about instantiating and destroying the form each time it's called. It's just a single statement to load the form and get the return value.
Of course, you'll need to add the code to populate the combobox with whatever items you wish to display.

Can you set events within Tab Control for MS Access?

I have been searching the internet for this all day. But no luck.
Can you setup events for when you enter/leave a tab.
I.E.
OnExit(Tab1)
Do something
Thanks
Depending on your program flow, you might try:
Private Sub Combo3_Exit(Cancel As Integer)
If IsNull(Me.Combo3) Then
MsgBox "No exit"
Cancel = True
End If
End Sub
Private Sub Form_Current()
Me.Combo3.SetFocus
''Or to refer to a subform from the main form
Me.subformcontrolname.Form.Combo3.SetFocus
End Sub
Does the tab contain a subform or only controls from the main form?
A subform has an Exit event, so if you are only concerned that once you have entered the subform you should not leave without completing data, you could:
Private Sub subformcontrolname_Exit(Cancel As Integer)
If IsNull(Me.subformcontrolname.Form.Combo3) Then
Me.subformcontrolname.Form.Combo3.SetFocus
MsgBox "No exit"
Cancel = True
End If
End Sub
While this is not exactly what you want, you could instead of handling the exit simply prevent the user from clicking somewhere else. To do this, attach some code to your combobox1 that bascially set .Enabled=Xfor all elements outside your tab view - where X is determined by the state of the combobox...

Getting contents of textbox in custom form

Currently I've attached a function to a command button on my form which retrieves what the user inputs into the textbox on the form; this works fine but I want to emulate this behaviour with the ENTER key. How can I go about doing this?
I tried the Enter Property on the textbox but that only occurs when focus has been transfered to the textbox, not when i press enter after input.
I've read about the KeyUp event for VB but there's got to be an easier way - I've googled this but can't find what Im looking for. Any suggestions?
Thanks
In many cases, the After Update event is suitable for working with data entered into a textbox, for example:
Private Sub txtFilter_AfterUpdate()
Me.Filter = "Content " & Me.txtFilter.Text
Me.FilterOn = True
End Sub
However, if you need to edit or validate the data, Before Update is more suitable.
Private Sub txtText_BeforeUpdate(Cancel As Integer)
If Me.txtText = "Invalid" Then
Me.Undo
Cancel = True
End If
End Sub