Solved

Afterupdate event on a field not working properly

Posted on 2012-03-21
5
346 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
Comment Utility
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)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

743 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

18 Experts available now in Live!

Get 1:1 Help Now