Reputation: 66851
This seem so simple but I can't figure it out. It seems like I'd want to set the Locked
property to Yes
, but that actually prevents them from selecting anything in the drop down!
When I say readonly, I mean having the combo box preloaded with values and forcing the user to only select one of those values without being able to type in their own.
Any ideas?
Upvotes: 0
Views: 5448
Reputation: 597
you can also add this code into presskey event of combobox
KeyAscil = 0
msgbox " please select from list"
this will prevent users from write into combobox and show message error
Upvotes: 0
Reputation: 23067
There are two parts to what you've requested:
a. I mean having the combo box preloaded with values...
For this, there's nothing you need to do -- just set the rowsource appropriately. To add values to the list, you'd have to do some work, but in order to prevent, you need do nothing at all.
b. and forcing the user to only select one of those values without being able to type in their own.
As explained in other answers, you need to set the LimitToList property to TRUE.
Now, the error messages you'll get from this are not all that great, so you'll likely want to define the NotInList event to be a little more friendly to the user. Something like this could be appropriate:
Private Sub MyCombo_NotInList(NewData As String, Response As Integer)
MsgBox Chr(34) & NewData & Chr(34) & _
" is not one of the choices in the list.", _
vbInformation, "Select from the list"
Me!MyCombo.Undo
Response = acDataErrorContinue
End Sub
If you wanted to add to the list, this is where you'd handle that, too, but since you don't, you need only concern yourself with informing the user of the problem (i.e., protecting them from the cryptic default error message).
Upvotes: 2
Reputation: 16003
The standard way to achieve this is to set the Limit To List
property to True
.
The user will be able to type in invalid text but if he tabs away or presses Enter a popup will say
The text you entered isn't an item in the list
And he will be forced back to choose something from the list.
It's not great, compared to the true dropdown list control you get elsewhere (VB6, Winforms, .NET, etc)
Upvotes: 3