Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Afterupdate event on a field not working properly

Posted on 2012-03-21
5
Medium Priority
?
363 Views
Last Modified: 2012-03-21
I have a date field on a form and for the afterupdate event I have:

Me.fieldname.defaultvalue = me.fieldname

But then when a new record is entered using the form, which has not been closed, I do not get the value of the previous record in the field.  Instead I get12/30/1899 even though the previous record’s value was 3/23/2012.

What is wrong with the code?

--Steve
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 37747028
Steve,

In order for the default value to "stick", it must be set in the property sheet (in the form's design), not just through code.
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 37747043
Actually, the syntax should be:

me.controlname.defaultvalue = me.controlname

If you are not using a standard naming convention for your controls, you should be.  By default, Access gives bound controls the same name as the field that is their control source, but this can be confusing, so it is highly recommended that you use a naming convention so that when you are programming you can differentiate between controls and fields.

The value #12/30/1899# implies that the default value has not been set, or has been set to 0

You indicate: "But then when a new record is entered using the form, which has not been closed, I do not get the value of the previous record in the field"

When you refer to the "previous record", was the date field in that record actually changed?  If not, then the AfterUpdate event would not have fired.  If you are just scrolling through records without updating them, then the default value is not getting reset.

You could use the Form_Current event to check to see whether the current record is a new record, and if not, use the current event to set the default value of those controls.  Something like:

Private Sub Form_Current

    If me.newrecord = false then
        me.control1.Defaultvalue = me.control1
        me.control2.defaultvalue = me.control2
    end if

end sub
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37747046
You can accomplish the same thing by using the Current Event of the form:

If Me.NewRecord = True then Me.FieldName = DLookup("FieldName","TableName", "IDField =" & DMax("IDField","TableName") )

This is adequate unless the database has multiple users who would be adding records simultaneously.
0
 
LVL 40

Accepted Solution

by:
als315 earned 2000 total points
ID: 37747076
Try:
Me.fieldname.DefaultValue = "=Datevalue('" & Format(Me.fieldname, "mm/dd/yyyy") & "')"
Correct format according to your regional settings
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37747082
Another possible solution is to place the name of a User Defined Function in the Default Value property (right on the property sheet).  For example, put this in the Default Value property for your control:

GetLastRecordedDate()

And then place this function in a code module:
Public Function GetLastRecordedDate
       GetLastRecordedDate= = DLookup("FieldName","TableName", "IDField =" & DMax("IDField","TableName") )

End Function

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question