Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the default-value in a form

Status
Not open for further replies.

Larsson

Programmer
Jan 31, 2002
140
SE
Hi!

Some time ago I ask a question about how to change a default-value for a control in a form. I got the answer that it wasn’t possible.
I and my co-worker thought some more about this and he come up with an idea that worked. The solution is:

DoCmd.Close acForm, FormName
DoCmd.OpenForm FormName, acDesign
Forms!FormName.Control.DefaultValue = Chr(34) &_
StringExpression & Chr(34)
DoCmd.Close acForm, FormName, acSaveYes
DoCmd.OpenForm FormName

First we close the form and reopen it in design view. Then we change the value, (The Chr(34) stands for “ and it’s used to get the “” around the expression, this is necessary when dealing with string expressions. After that we close the form, save it and open it again.

There is something’s to think about with this solution. The open and close event will run because of that we close and open the form throughout the procedure. Therefore it is important to think about what those events do before using this procedure.

Any suggestions for improvements is always welcome, Markus Larsson
 
Well, I don't know why it is necessary to close the form?

I just did a test, placed a text box on a form, set it to YES/NO format, set it's default value to True, then in an event I coded me.textbox.defaultvalue = false and it changed the default value to false from true.

seems that one could do the same with other values, character for example, (me.textbox.defaultvalue = "A" for example)

james
 
excuse, to get the "" use the following:

me.textbox.defaultvalue = """Cash"""
or
Forms!frmInvoice!PaymentMethod.DefaultValue = """Cash"""
(example from microsoft help)

James
 
The problem is that the default-value must be saved for it to stay changed. The default-value will stay the same if you don't save the form.


I didn't know how to do that with """cash""". Thanks for that.

Larsson
 
Nice one Larsson,

One additional nicety .. ..
Start with
On Error Goto ( Error routine where you set Echo True )
Echo False

then do your changes

Echo True

.. .. so that the user does not see all this going on.


G LS
 
Yes ofcourse, I forgot that. I am an old C++ programmer and I am not used to handel errors.

Larsson
 
It will not work in Mde File
 
Thats possible, I only work with mdb-files.

Larsson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top