Default value of a text box.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This morning I asked how I could save the value input by a user of a text box
on a form as the default value of that box. John gave me the following code:

Me.txtUnbound.DefaultValue = """Hello World"""

When I open the form the next time the Hello World does not appear. Does
anybody know what I am doing wrong.
 
Where was the code added? To the form? Did you save the form after the code
was added?

Where in the form was the code added? If added to the Form_Open event,
"Hello World" should appear when you move to a new record. If added to the
AfterUpdate event of a control, "Hello World" won't appear as the default
value until you make an entry in the control with the code and *then* move
to a new record.

The DefaultValue property only applies to New records although the same
value will also appear in any records that you created while that Default
was in effect without changing the default.

HTH,
 
Properties modified in form view (while it is running) are not presistent.
You can, however, save them with the form when you close it by using the
asSaveYes option in the Close method where you close the form.
 
If you want a change to the default value to persist, your code needs to
open the form in design view, make the change, and then pass acSaveYes
to DoCmd.Close acForm to save it.

Rightly or wrongly, I'm uneasy about making design changes a part of the
regular operation of a database. Instead, I'd store the value in a table
(along with any other settings that need to be stored between sessions)
and retrieve it in the form's Open event. If the settings table is
called tblSettings and has fields called SettingName and SettingValue,
you could have a record like this:
SettingName SettingValue
Default Value for txtUnbound "Hello World"

and retrieve the value with something like this:

Me.txtUnbound.DefaultValue = DLookup("SettingValue", _
"tblSettings", "SettingName='Default Value for txtUnbound'")
 
You are correct, John. I just ran a test, and changing the default value of
a control in form view and closing it with acSaveYes does not save the
default value, it reverts to the default value established in design view.

I am with you on making design changes during normal operation. You
solution would be the one I would use.
 
Back
Top