Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox & Textbox question: How to show a Default answer? 1

Status
Not open for further replies.

rlee16

Technical User
Jan 19, 2003
44
HK
Currently, my comboBoxes and TextBoxes in Excel VBA userform start out blank. One must click on the arrows to see the choices. How can I have the boxes show a default choice?

Thanks in advance,

Richard
 
Use the Userform_Initialise command to set the default values of your various input boxes.

With the VBE open, click on your user-form to see the code (or use the right click function). Select the action "Initialise" in the top right hand box to create a private sub Userform_Initialise.

Write code such as

with me
.MyComboBox.value = "My Default Value"
.MyListBox.value = "First Record"
.MyCaption.caption = "Hello"
end with

When the userform initialises (loads) then the code will run and the default values will be set.




 
Hi rlee,

This had me stuck for ages also in Access though, sounds like you just want a value in there - and that's normally by default the first value.

Dont know if this'll work with Excel, but try this in the 'Default Value' property of the control...

[mycontrolname].[ItemData](0)

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
Thanks PBAPaul and Darrylles for your responses,

PBAPaul, your code works great, thanks!

Darrylles, thanks for the input!

Richard
 
Oops,

The combobox code works fine, but not the one for listboxes. Any help?

Richard
 
Hi,

Again,

mylistbox.defaultvalue = [mylistbox].[ItemData](0)

Regards,

Darrylle "Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
Darrylles,

I have tried the above code and with many permutations, but I cannot get it to work in Excel (although most likely a function of me rather than the code). Thanks for the help anyways.

Richard
 
Hi,

No prob. rlee. I am looking at Access development controls for this anyway (as I said in my first post).

Regards,

Darrylle. "Never argue with an idiot, he'll bring you down to his level - then beat you with experience." darrylles@totalise.co.uk
 
Hi Richard,
for list and combo box use [tt].ListIndex[/tt] property, set =0 for 1st element, =1 for 2nd, etc. If =-1 no item is selected. Use to set/get element.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top