Adding text to a combobox - vba

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"

Related

Hide DropDown list for ComboBox without losing focus

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

VBA: Show listbox on click

I searched for a way to show a listbox on certain event, here a click, but wasn't able to find it
If MsgBox("Souhaitez vous reprendre un bordereau déjà édité?", vbYesNo, "Edition Bordereau") = vbYes Then
PreCheckPlot
Else
rest of commands
And the sub where I want to show the listbox
Sub PreCheckPlot()
ListBox2.Visible = True
End Sub
This doesn't work, and ListBox2.Show doesn't either, it throws an error.
Is it possible to show a listbox on a click, and if yes, how would I write it?
Thank you in advance.
You need to refer to the Sheet as well.
So if your ListBox2 is in Sheet1 then you need to use:
Sheet1.ListBox2.Visible = True
Does it go into PreCheckPlot when you step through?
What is the error?
If you create a userform, put a listbox on it and a button which when pressed shows your message your code then works fine.
I wonder if you are trying to set ListBox2.Visible from outside the form (where it will not know what the form is)
This assumes it is an ActiveX listbox - or is it a forms listbox?

My ComboBox doesn't display the values I've added in VBA

I'm trying to add options to a combo box in a userform. When I run the code, Excel doesn't give any errors, however when the userform shows up it doesn't display the entities I have added to the combobox previously. That is, when I click on the combobox, it doesn't show any options, only one blank row, as if no items were added to it.
Here is the code I'm using:
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub
I am using the following code to call the user form within a macro:
UserForm.Show
The code given in the question works perfectly well. In my case the code didn't work because I manually entered this part of the code into VBA:
Private Sub UserForm_Initialize()
If you make Excel create this module for you instead of writing it on your own, your code should work perfectly. Excel did not have "Initialize" as a default form so I tried "Activate" and it worked.
To create this module you have to do the following steps:
Right click on user form
Click on view code
On top you will see two categories you can pick, you should pick "Userform" and "Activate", after completing this step excel must add a new module to your code.
In this module you may code everything you want about the content of the combobox.
You should also be careful with the spelling of your combobox, if you spell it incorrectly, you may be unable to see the contents of the combobox.
Ensure that the code segment you have posted is in the userform.
Right click on the user form in the VBA view and choose "View Code". Is this where the code is?
Are you sure that the User Form is called 'UserForm' and not 'UserForm1'? 'UserForm1' is the default, similar to 'ComboBox1'.
The below works for me.
'in the UserForm1 code
Private Sub UserForm_Initialize()
ComboBox1.AddItem "xxx"
ComboBox1.AddItem "yyy"
ComboBox1.AddItem "zzz"
End Sub
The below will display the form.
UserForm1.Show
Is this the only form in the workbook? Create a new one and see if it does the same thing.
Same problem here, but I solved setting ColumnWidths property to -1 (it was set to 0).

Excel VBA Userform - How do I add a value to a combo box from a list in the userform?

Is there a way to do the following:?
I would like a manual text box that when filled in and the user clicks "Add" and it adds the value to the combo box. However, I will also have this list stored somewhere so that the next time the user loads the form they can select "Previously Used" items from a list and these will then also be added to the combo box.
I will be adding a button to "Add all" of the previous items but it must still have the option to add items manually each time.
Even more useful would be to have an option to check items in and out of the combo box as well as the manually text input option.
I tried to add an image I found to better explain this but I need 10 rep points. Therefore, I hope this makes sense but please feel free to comment for more details and a better explanation if needed.
Link to an example of what I am thinking of but without the manual addition field:
http://kb.blackboard.com/download/attachments/14057766/bbui_multi_select.gif?version=1&modificationDate=1202823953747
Use this
combobox1.additem txtValue.Text
Sheets("SheetName").Cells(lastRow+1,col).value=txtValue.Text
Also add below code in Form_activate
For i=1 to lastrow
combobox1.additem Sheets("SheetName").Cells(i,col).value
next i

Placing Label Over Combo-box in Ms-Access

Background:
I am trying to place a label in Ms-Access that is being used as a button over a combo box. The user will select from the combo-box list and if they want to erase their selection they can click on the label to clear the selection.
Question:
Is it possible to get the label to show above the combo? It seems like the label is being sent behind the combo even though I specifically send it to the from etc...
Instead of creating the overlaying label to clear the selection, I would create an actual button. You will stack the button and the combo box on top of eachother in design mode, but set the button visibility to "No" in the format properties. Let's say you call the combo box "cmbSelect" and the clear button "cmdClear", use the following to show the button after the combo box is selected:
Private Sub cmbSelect_AfterUpdate
Me.cmdClear.Visible = True
End Sub
The following code will then clear the data from your table after the button is clicked:
Private Sub cmdClear_Click()
DoCmd.SetWarnings False
'Deletes record from your table
Dim Delete As String
Delete = "DELETE * FROM [TableName] WHERE (([TableName].KeyField)='" & KeyField & "')"
DoCmd.RunSQL Delete
DoCmd.SetWarnings True
End Sub
You could also have the form requery by running a requery on each field instead of using the delete string. Then you could build code for the button "after update" that makes the button invisible again and allows you to select a new entry from the combo box. There's a lot of possibility, but this should get you started. Let me know if you need any more explanation or help.