Link to home
Start Free TrialLog in
Avatar of Nicola Siotto
Nicola SiottoFlag for United Arab Emirates

asked on

On next/new record, copy specific field data of previous record

I have a form which contains a number of fields. One of these is the department. When I click next it starts a new record but I would like to write on the same field by default the name of the department which can be copied from the previous record. How to do that?

In the attached screenshot video there is a sequence. At first sight on the top left the division is mentioned: Fit Out. When clicking on the next record it disappears, while I'd like to insert automatically the same.

Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Use the Current event and a Public Variable to store the Department
e.g.
Dim department

....
On Some event e.g
AfterUpdate
department = me.department

then on the Current
if Me.NewRecord Then
If department <> 0 then
me.department = department
End if
You can use the the AfterUpdate event of your division field to set the default value for that field, something like:
Private Sub txt_Division_AfterUpdate
    me.txt_Division.DefaultValue = me.txt_Division
End Sub

Open in new window

You should also use the Form_Current event, like:
Private Sub Form_Current
    if not me.NewRecord then
       me.txt_Division.DefaultValue = me.txt_Division
    end if
End Sub

Open in new window

This will make sure that when you enter an existing record, it sets the default to that if you then go to a new record, it has the default value
Avatar of Nicola Siotto

ASKER

Dear @John and @Dale,
Thank you for your answers and sorry for being late but for three days I was out of station.
In both cases the solution doesn't work for some reasons.

When accessing the form before it opens it gives an error:

User generated image
Please note that the field "activitytype" is automatically populated from a previous form used as a switchboard.
It worked fine, until I have updated with the formulas.
Could you upload sample DB with this form and related table with dummy data?
Based on the nature of that error, I would assume that you either referred to a control which does not have a value (a label perhaps).  I [ActivityType] a valid control name on your form?  

From the note at the bottom of your reply, it looks like this might be a field, not a control.  If you don't already have a control bound to the [ActivityType] field, create one, and give it a meaningful name (txt_ActivityType, cbo_ActivityType).  Then set the default value of this control.

Normally, when you want to set the default value of a control, it is when you just finished entering data for a record and you want to repeat many of the values when you create the next record.  In that case, a better event for setting the default value of a control is in the AfterUpdate event of that control.

Dale

Tomorrow I will try to upload a sample. Thanks.
@als315 as advised yesterday I have created a sample.

There is only one form and if opened there is a combo with the activities that are created from the main table. Now, what I wish is that when pressing NEXT on the bottom of the form to create a new record, automatically the control named [activitytype] is populated with the same content of the previous record. Is that possible?

Sample.mdb
How should it work? Form looks not very likely to your form on video.
Should I select Division (activitytype) and press new record button?
Is expected behaviour - fill  activitytype in new record equal to selected on form?
Take a look
Note : I made some changes
Sample.zip
@als315 correct. It’s like you wrote, exactly.
What Division should be copied to new record? From last selected record? Or from last record in table?
@als315
from last record
@john
I'm not at my pc right now so I cannot test it. Will do so later. Thabks.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@John the sample that you have sent doesn't do the job. When I go to the new record the control is empty.
@als315 I have opened the form frm_main and it works perfectly.
Is there any reason why it didn't work on the form _CPR_NEW?
Form _CPR_NEW is strange. It is limited only to new record, but I was not able to find reason in reasonable time. It's take too many time to analyze difference between forms.
In any case I have replicated the functions and works fine. Thanks.
It is limited only to new record
This is because of the Data Entry flag.