inability to change unbount textbox default value programmatically

B

Bob Quintal

(e-mail address removed) wrote in
I'd like to be able to make the current value of a textbox on
a form the default value. Using the following code:

Debug.Print "orig", Me.tbxYear.DefaultValue
Me.tbxYear.DefaultValue = Me.tbxYear.Value
DoCmd.Save
Debug.Print "new", Me.tbxYear.DefaultValue

I am able to apparently change the default value... that is,
the default value change shows up in the immediate window, and
after the code has run, I can still print the control
defaultvalue property in the immediate window... (still
changed), but when I close and reopen the form, the default
value has reverted to it's original value. I CAN effect a
change by changing the default value in the properties dialog
box. Why can't I do this programmatically?
two things to try: 1) surround the date with double quotes. I
seem to recall an issue with the default value that requires a
string even for numeric data.

2) Docmd.Save allows optional parameters for the object type,
object name. Specify them.
 
J

james.stenzel

I'd like to be able to make the current value of a textbox on a form
the default value. Using the following code:

Debug.Print "orig", Me.tbxYear.DefaultValue
Me.tbxYear.DefaultValue = Me.tbxYear.Value
DoCmd.Save
Debug.Print "new", Me.tbxYear.DefaultValue

I am able to apparently change the default value... that is, the
default value change shows up in the immediate window, and after the
code has run, I can still print the control defaultvalue property in
the immediate window... (still changed), but when I close and reopen
the form, the default value has reverted to it's original value.
I CAN effect a change by changing the default value in the properties
dialog box. Why can't I do this programmatically?
 
G

Guest

You have to save changes when you close the form
Rather than
Docmd.Close
Use
Docmd.Close acForm, Me.Name, acSaveYes

That will save any changes made to the form design, not the data in the form.
See VBA Help for the Close method for other options
 
M

Marshall Barton

I'd like to be able to make the current value of a textbox on a form
the default value. Using the following code:

Debug.Print "orig", Me.tbxYear.DefaultValue
Me.tbxYear.DefaultValue = Me.tbxYear.Value
DoCmd.Save
Debug.Print "new", Me.tbxYear.DefaultValue

I am able to apparently change the default value... that is, the
default value change shows up in the immediate window, and after the
code has run, I can still print the control defaultvalue property in
the immediate window... (still changed), but when I close and reopen
the form, the default value has reverted to it's original value.
I CAN effect a change by changing the default value in the properties
dialog box. Why can't I do this programmatically?


As Dave said, you can change it, but it would have to be a
design change to keep it for the next time the form is
opened. However, making design changes to a running
application is a really bad practice for many reasons.

Instead, you should save the default value in a table. Then
you can set the text box's default value in the form's Open
or Load event.
 

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