Solved

Afterupdate event on a field not working properly

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 7 33
Access 2016 - query 23 60
How to use VBA to update a record in a table in MS Access? 12 36
Open VBA code while form is open and running. 4 24
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

770 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