Set DefaultValue with Code

  • Thread starter Thread starter JD
  • Start date Start date
J

JD

I need to set the default value of a textbox and combobox with the selected
value until it is changed, then the new value becomes the default value. I
have used this code in the AfterUpdate Event in the past but it is not
working.
Me.txtTextbox.DefaultValue=Me.txtTextbox.Text
Me.cboCombobox.DefaultValue=Me.cboCombobox.Value

I get the ?Name? error in the control on the form.

How do I fix it?
 
Remove the .Text and the .Value
you don't need them in Access. Just
Me.txtTextBox will return the current value of the control. The .Text
property is only available if the control has the focus.
 
JD said:
I need to set the default value of a textbox and combobox with the selected
value until it is changed, then the new value becomes the default value. I
have used this code in the AfterUpdate Event in the past but it is not
working.
Me.txtTextbox.DefaultValue=Me.txtTextbox.Text
Me.cboCombobox.DefaultValue=Me.cboCombobox.Value

I get the ?Name? error in the control on the form.


In addition to Klatuu's response re the .Text property.

The defaultValue property is a little tricky in that it can
contain an expression (which might contain a function call).
This means that whatever you put into the DefaultValue
property is evaluated and that means that you must use the
appropriate delimiters when you assign something to it.

For a numeric value use:
Me.txtTextbox.DefaultValue=Me.txtTextbox

For a text string use:
Me.txtTextbox.DefaultValue= """" & Me.txtTextbox & """"

For a date value use:
Me.txtTextbox.DefaultValue=Format(Me.txtTextbox,"\#m\/d\/yyyy\#")
 
Thanks, Marshall, I neglected to mention that.

Marshall Barton said:
In addition to Klatuu's response re the .Text property.

The defaultValue property is a little tricky in that it can
contain an expression (which might contain a function call).
This means that whatever you put into the DefaultValue
property is evaluated and that means that you must use the
appropriate delimiters when you assign something to it.

For a numeric value use:
Me.txtTextbox.DefaultValue=Me.txtTextbox

For a text string use:
Me.txtTextbox.DefaultValue= """" & Me.txtTextbox & """"

For a date value use:
Me.txtTextbox.DefaultValue=Format(Me.txtTextbox,"\#m\/d\/yyyy\#")
 
Thank you
Klatuu said:
Remove the .Text and the .Value
you don't need them in Access. Just
Me.txtTextBox will return the current value of the control. The .Text
property is only available if the control has the focus.
 
Thank you
Marshall Barton said:
In addition to Klatuu's response re the .Text property.

The defaultValue property is a little tricky in that it can
contain an expression (which might contain a function call).
This means that whatever you put into the DefaultValue
property is evaluated and that means that you must use the
appropriate delimiters when you assign something to it.

For a numeric value use:
Me.txtTextbox.DefaultValue=Me.txtTextbox

For a text string use:
Me.txtTextbox.DefaultValue= """" & Me.txtTextbox & """"

For a date value use:
Me.txtTextbox.DefaultValue=Format(Me.txtTextbox,"\#m\/d\/yyyy\#")
 
Back
Top