Solved

Afterupdate event on a field not working properly

Posted on 2012-03-21
5
349 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
  • 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 47

Expert Comment

by:Dale Fye (Access MVP)
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 500 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

828 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