tags:

views:

118

answers:

3

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?

+2  A: 

There is a Limit To List property you should set to Yes

DJ
First :) char char char
Remou
+2  A: 

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)

hawbsl
The Access combo box may not have default behaviors that you like, but it has a helluva lot more properties and events and is substantially more versatile. The Access combo box is designed for use in a database where you must restrict entry and inform users of erroneous entries. The other platforms aren't specifically designed for development of database applications, so they aren't necessarily as strict about how they handle non-valid input. This is a FEATURE in Access, not a BUG -- it's better that you have this behavior because it's appropriate to the purpose for which Access was created.
David-W-Fenton
The NotInList event is interesting and worth mentioning, I upped one your answer.Even so, it has to be a UI flaw to intercept and reject invalid input only AFTER the user has finished typing. Better UI is to cue the user he has to choose from the list by only allowing him to choose from the list.
hawbsl
If you want to intercept it *before* the AfterUpdate event, you have the OnChange and BeforeUpdate events in which to do it. The OnChange event isn't a very good place to evaluate against the combo box values, but the BeforeUpdate event is, as that's where you can pre-qualify the data and cancel the update. I'm not entirely sure the order of these in relation to the NotInList event. The point of Access events is that there are defaults that handle everything, and you can code whatever you like to override the default behaviors.
David-W-Fenton
+2  A: 

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).

David-W-Fenton