Setting control's default value

S

scottydel

Hello,

I'm using Access 2003. I have a form with a combo box control whose Row
Source is set to a query. On Form Load I had originally set the control's
initial value to a hard-coded value:

Me.cmbControl.Text = "Dave"

This worked fine, however I now need to set the control's .Text property
more programmatically, and hard-coding "Dave" will no longer suffice in the
long run. I have tried without success to access the control's Recordset:

Me.cmbControl.Text = Me.cmbControl.Recordset.Fields(2).Value

This yields the error:

"Object or with block variable not set"

However, if I add this field to the Watch, Me.cmbControl, and I expand that
field within the Watch to Recordset->Fields->Item 3->Value, then try and step
through, it works. On its own it does not recognize the object,
Me.cmbControl.Recordset.Fields(2).Value, but if I "help it along" by
expanding to this value in the Watch, it works fine (FYI, after I do this,
Me.cmbControl.Recordset.Fields(2).Value does in fact = "Dave" within the
Watch, which is the correct result from the query).

Just not sure how the control can arrive at this result on its own as the
form loads, instead of giving me the "Object or with block variable not set"
error.

Any ideas?

Thanks,

Scott
 
D

Dale Fye

Scotty,

I don't think you can set a controls text property unless that control has
the focus.

If you are doing this in design view, you may be running into a problem that
the controls recordsource has not yet been sourced when the form first opens,
therefor the "Recordset" would not be set yet.

Also, is your third field the bound column for this control. If not, trying
to set the controls value to that will not work, unless both colums are of
the same data type and have the same values.

If you really want to change the controls default value (at least for the
current session of this form), try putting the following in the afterupdate
event of the combo box

me.cmbControl.DefaultValue = me.cmbControl

HTH
Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top