Hide DropDown list for ComboBox without losing focus - vba

I have created a userform similar to a google search. I'd like the drop down to show/hide depending on how many letters are in value.
Like this..
If Len(ComboBox1.Value) > 4 Then
ComboBox1.DropDown
Else
'ComboBox.DroppedDown = False (non-existent method)
End If
There are many versions of this question, most result in changing focus, but this should occur while the box is being typed in. I find it hard to believe it's not possible..
EDIT: Question pertains to hiding the list below, not the arrow to the side - as it is set to always hidden - the list should hide/unhide based on length of value.

The closest I can get is this, but for some reason if you delete characters it doesn't update. Plus it only starts working once there are more than 5 characters! Edit - amended as per Mistella's comments.
Private Sub ComboBox1_Change()
If Len(Me.ComboBox1.Value) > 3 Then
Me.ComboBox1.ShowDropButtonWhen = fmShowDropButtonWhenAlways
Else
Me.ComboBox1.ShowDropButtonWhen = fmShowDropButtonWhenNever
End If
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "A"
.AddItem "B"
.AddItem "C"
.AddItem "D"
.ShowDropButtonWhen = fmShowDropButtonWhenNever
End With
End Sub

If the answer is still important for you, I've managed to avoid showing the dropdown by consequent disabling and enabling the control.
In my case I wanted the combobox to call another userform by click on the arrow with subsequent pasting the result into the texbox part of combo. I also was irritated by the dropdown appearing.
I'm not sure it will do you any help, as subsequent .SetFocus causes dropdown to appear again..., but just in case, following helped me:
UserForm1.ComboBox1.Enabled = False
UserForm1.ComboBox1.Enabled = True

Related

Getting MS-Access form to save invisible combo boxes as a null or 0 value in query and table

I don't know that how I have built my form is necessarily the best way that I can do it, but it was the way that I could get it to work, at least partially. I have built a form in ms-access 2007 that uses vba to either hide or make available certain combo boxes. The first choice and the one on which the rest of the form is based is a yes/no option, being that either the customer requires outside services for their job or not. Once that is selected the user can then choose from the outside service options(Which are the combo boxes, either visible or no based on the first choice). So this is where the problem comes in, I have code written so that if the user chooses no in the very first box the rest of the boxes are made invisible. However if the user chooses yes they must then choose values, again yes or no to either retain or remove other options for the remainder of the form.
What I am looking to do is to make it so that when the user returns to the form what choices they made are still there. So if they chose no then the form would basically be blank and if they had said yes initially than that answer along with only the other choices they made would be available.
What I am currently using is a simple if-then statement to make the boxes either visible or not.
Private Sub Combo36_AfterUpdate()
If Combo36.Value = "No" Then Me.Combo18.Visible = False
If Combo36.Value = "Yes" Then Me.Combo18.Visible = True
If Combo36.Value = "No" Then Me.Combo20.Visible = False
If Combo36.Value = "Yes" Then Me.Combo20.Visible = True
End Sub
Obviously I am not experienced with access and have stumbling my way through it. I am sorry if any of what I have said above is confusing. If clarity is needed please let me know.
Well for a start, "Flase" should be updated to "False".
Instead of storing and then repopulating the selected values it might be easier to turn the visibility of the whole form true/false based on selection which would keep the last values the user selected.
For showing visibility of the controls try:
Private Sub Combo36_Change()
If Me.Combo36.Value = "No" Then
Me.Combo18.Visible = False
Me.Combo20.Visible = False
ElseIf Me.Combo36.Value = "Yes" Then
Me.Combo18.Visible = True
Me.Combo20.Visible = True
End If
End Sub
Your initial code is okay, to be able to have the form retain the choices then copy the code you have under the Combo36_AfterUpdate() event into the Private Sub Form_Current() event. This will do it.

How to use VBA to show / hide multiple combinations of tables in MS Word? - explained further within

I have a word 2010 document with some basic VBA code attached to a number checkboxs that show / hide a sections throughout the document.
There are a number of these throughout the document however they all share the same basic code which appears to be working well enough (example below)
Private Sub PlanningBox_Click()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
End Sub
The issue i am having is that some of these sections then have checkboxes within them that show / hide further section which is fine, however if the user unchecks the first initial checkbox (hiding all sections) then re-checks it, it will open up the initial section again but not the ticked subsections - in order to do this they have to uncheck/check the subsection again. I know this seems minor but i need to ensure the form is as fluid and user acessible as possible..
For example let's say there is a single row table with the text 'Have you spoken to anyone for feedback?' (checkbox1) - the user will tick the check box which will unhide a section of text below giving directions, asking some questions, general blurb etc. At the bottom of this is another question 'Who did you speak to?" - and then multiple checkboxes 'mother','father', 'Child' (checkbox A,B,C,etc). Checking any of these boxes opens up a table with additional questions for each one selected, the user can check any number / combination of the checkboxes.
Now if the user unchecks the initial checkbox 1. all sections will be hidden no problem, but then if they then check it to reveal the section again the mother, father etc boxes remain ticked but will not reveal the sections without needing to be unticked/ticked again. Is there a way so that ticking the intial box will also reveal any previously unhidden sections again?
My knowledge of VBA is very limited, i have considered using If + ElseIF statements but my understanding is that i would need an ElseIf for every potential combination. Is there a more sophisticated way around this at all?
Hopefully i have articulated this well enough but happy to provide further information. Thank you for any assistance given
You could create a common routine that evaluates every checkbox and sets the corresponding section accordingly. Call that one routine whenever a checkbox is clicked.
Private Sub PlanningBox_Click()
SetRangeVisibility
End Sub
Private Sub SomeOtherBox_Click()
SetRangeVisibility
End Sub
Public Sub SetRangeVisibility()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
If SomeOtherBox.Value = False Then
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = False
End If
' Etc...
End Sub
Further, if you use an array of check boxes you'd only need to write a single SomeBox_Click event procedure. Here is some information on Control Arrays in VBA.

VBA Radio buttons: Select at least one option

I have a userform with with 6 different categories in which the user must select an option between 0-4. Each option is selectable with a radio button with a set value that will be written in the parent sheet. Each set of radio buttons that belong to a category are grouped (a-e).
Could someone please help me to implement a mechanism that will make it mandatory for the user to select at one option in each category by displaying a message prompting the user to select an option?
The workflow is as such:
User selects options --> Selected options are written in the sheet --> Unload Me on the form
Ideally it would be best if the macro to write all is "paused" (not proceeding to Unload Me). Instead the message is displayed, and then the form remains as it was so that the user can go back and continue, without having to redo the whole process.
EDIT:
Thanks for all help. Hope this makes it easier:
The code I'm using is being triggered by a "Register score" button that I've placed on the UserForm that contains all the radio buttons. The button contains the following code (where aa = first radio button):
Private Sub CommandButton1_Click()
If aa.Value = False Then
MsgBox ("Please select at least one option")
Else
Range("A" & Rows.Count).End(xlUp).Offset(1).Value = "Category A"
Range("B" & Rows.Count).End(xlUp).Offset(1).Value = "0"
End If
Unload Me
End sub
I now repeat this code for every radio button. Problem is, if the user for example selects a radio button properly in every category but forgets the first, the form will reset meaning that the user must re-do the entire thing. I can of course also add (I've only included the radio buttons here for sake of simplicity, but the correct code would of course include all buttons):
Private Sub CommandButton1_Click()
If aa.Value = False and ab.Value = False And ac.Value = False Then
MsgBox ("Please select at least one option")
In this case the user must select one option every category, but it will also reset the entire form, meaning that you have to redo everything.
How about something like:
If Radio1.value = False And Radio2.value = False And Radio3.value = False And Radio4.value = False then
Msgbox "Please select something from the first four Radio Buttons"
Exit Sub
End if
To ensure one is selected simply add a section of code in the OK/Close button's event that checks that at least one button is selected - if not display message and then either Exit Sub to abort the routine, or implement the checks as a function that returns a boolean and only unload if it returns true.
Incidentally, on your proposed workflow, if you unload the form how are you going to then write the user's choices into the sheet?

Adding text to a combobox

Currently I have a simple ComboBox that populates with 3 items when clicked:
With FunctionBox
.AddItem "Add Blank Issue"
.AddItem "Move Existing Issue"
.AddItem "Reorder Issues"
End With
However, the combobox is empty before the dropdown arrow is selected.
When the combobox appears I want it to show something like, "Please select one of the options below".
I tried setting the 'Value' of the combobox to 'test'. Test shows up in the editor, but does not when I run the application.
I also want to make sure the string goes away when the down arrow is selected and the user cannot interact with it.
Any advice?
I can provide screenshots if this is somewhat unclear.
Thanks!
Goto design view of your form, click on the combo box in question and change the text value to what you want it to say.
Edit - didnt see the 2nd part of your question
put this in the dropbuttonclick event. changing combobox1 where needed
Private Sub ComboBox1_DropButtonClick()
ComboBox1.value = ""
End Sub
To have a default value in combobox, add this code in UserForm_Initialize():
FunctionBox.AddItem "Please select one of the options below"

How to determine next tabstop in VBA UserForm?

I have a UserForm with some textbox entry fields on it that are enabled/disabled by a checkbox. When a checkbox is clicked to check it, I'd like to move the focus into the now-enabled textbox.
The textbox is the next control after the checkbox in the tab order, so it seems like using the tab order to find the appropriate textbox would be a good idea.
But... how can I find the next control in the tab order after a given control? Is there a method to do that, or do I have to enumerate all the controls and figure it out for myself?
I appreciate that this comes under the heading of "enumerating all the controls" but it's pretty simple and I attach the code for completeness:
Private Sub CheckBox1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.TabIndex = Me.ActiveControl.TabIndex + 1 Then
ctl.SetFocus
Exit For
End If
Next
End Sub
As a different way of looking at this.
Can you not rather use the textbox you want focussed, and set that name in the checkbox.tag
then in you vba code use
DoCmd.GoToControl CheckBox.Tag
Where the CheckBox.Tag is the Textbox.Name?
EDIT:
OK, I found this
SendKeys "{Enter}", True
at VBA code for moving to next control? It must be eeeasy
I had trouble with
SendKeys "{Enter}", True
With a little experimentation I found this works
SendKeys "{TAB}", True
One caveat...if you're in the VBE stepping through the code, and watching it on the form, SendKeys is executed is executed in the code. Confused the heck out of me at first why my code started to look odd, e.g. extra spacing and extra lines!
With all due respect to the kind advice offered thus far, this issue shouldn't be addressed through programming when there's a way to address it within the userform itself.
Click on the userform itself, then right click and select "tab order". You can then move each element within the userform to whatever position you want without having to resort to complicated and unstable programming tricks.