Question

I have an Access 2000 form that contains a combobox. The combobox is bound to a field in a table. When the value in the table is null, I want to set a default value on combobox without making the record dirty. Setting defaultValue does not work unless it is a new record. When I try to set the value, I get an error "You cannot assign a value to this object".

Any thoughts?

Me.cboName.Value = Me!cboName.Value ' This causes the error mentioned above

Me.cboName.DefaultValue = Me!cboName.Value 'This does nothing on an existing record.

Was it helpful?

Solution

The DefaultValue is entered when a NEW RECORD is created. To have the value display for an existing record... the easiest way I can think to accomplish this is to usean unbound control. For example, if the field you are using is theName in the Current event you'd use code like this:

Private Sub Form_Current()
     me.cboName.value = Nz(me.theName.value,defaultValue)
End Sub

Where defaultValue is your previously determined default value. This will effectively require you to have two controls for the name... One with the bound value and one with the displayed value. If you do this, you'll have to also add code to update theName, when you change cboName.

As Remou suggested, you should ask yourself if this is really what you want to do, as it is definitely at least a little messy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top