programmatically setting field's default value

rolaaus

Registered User.
Local time
Today, 02:01
Joined
Feb 12, 2008
Messages
84
I'm trying to change the default value of an item whenever the value in a field changes. I have the code executing just fine, but for some reason it's just not saving, even with the following line of code
DoCmd.Save ObjectType:=acForm, ObjectName:=Me.Form.Name

This is what I'm trying to accomplish whenever the value is changed, but it's just not happening
Me.fld_BankName.DefaultValue = "=" & con_Quote & Me.fld_BankName & con_Quote

Is the default value not modifiable in run-time?
 
Code:
DoCmd.Save ObjectType:=acForm, ObjectName:=Me.Form.Name
has nothing to do with saving data and hence has nothing to do with the default value for a field! This code saves any design changes that have been done to the form, such as changing the background color or adding controls to the form, etc.

I'm not really sure what con_Quote has to do with fld_BankName, but here's the general syntax for saving a control's current value as the default value for the same cntrol. The syntax varies according to datatype of the underlying field:

For Text fields

Code:
Private Sub YourTextControlName_AfterUpdate()
If Not IsNull(Me.YourTextControlName.Value) Then
  YourTextControlName.DefaultValue = """" & Me.YourTextControlName.Value & """"
End If
End Sub
For Numeric fields

Code:
Private Sub YourNumericControlName_AfterUpdate()
If Not IsNull(Me.YourNumericControlName.Value) Then
  YourNumericControlName.DefaultValue = Me.YourNumericControlName.Value
End If
End Sub
For Date fields

Code:
Private Sub YourDateControlName_AfterUpdate()
If Not IsNull(Me.YourDateControlName.Value) Then
  YourDateControlName.DefaultValue ="#" &  Me.YourDateControlName & "#"
End If
End Sub
 
Code:
DoCmd.Save ObjectType:=acForm, ObjectName:=Me.Form.Name
has nothing to do with saving data and hence has nothing to do with the default value for a field! This code saves any design changes that have been done to the form, such as changing the background color or adding controls to the form, etc.

I would think, from my many years experience, that changing a text boxes default value is considered a DESING change!, but maybe that's just me.

BTW, con_Quote is a constant with the value of """

And, yes the code you have written is what *SHOULD* work, but the reason I'm posting here is because it's not working as expected.
 
Brilliant
After over 4 years from posting it, your reply helped me a lot.

Thanks a lot

Asar
 
That's why we encourage posters to use meaningful titles for their threads...it makes it easy for searchers with a similar problem to find help!

Glad we could help!

Linq ;0)>
 

Users who are viewing this thread

Back
Top Bottom