Solved

Afterupdate event on a field not working properly

Posted on 2012-03-21
5
347 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 39

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

929 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now