How to set the default value to the last entered field
nounours4
Posted messages
47
Status
Member
-
nounours4 Posted messages 47 Status Member -
nounours4 Posted messages 47 Status Member -
Hello,
How can I set the default value of a new field to be the same as the last entry for that field?
I am currently inputting a collection, and for convenience, I would like the default value displayed for a new record to be equivalent to that of the previous record (for certain fields only). I would then only need to change the field I am interested in before saving it.
Thank you in advance for your attention to my topic.
Nounours
How can I set the default value of a new field to be the same as the last entry for that field?
I am currently inputting a collection, and for convenience, I would like the default value displayed for a new record to be equivalent to that of the previous record (for certain fields only). I would then only need to change the field I am interested in before saving it.
Thank you in advance for your attention to my topic.
Nounours
17 answers
-
Hello Nounours4, Hello leMéruvien,
By taking a personal base that I wanted to improve, I found this piece of VBA code that is exactly what you're looking to do: set the last entered value as the default value.
Function DerniereValeur()
On Error GoTo Sortie
SACV = Screen.ActiveControl.Value
Screen.ActiveControl.DefaultValue = SACV
Sortie:
End Function
You can paste it as is without any modifications anywhere in any VBA module, then Copy/Paste the title "Function DerniereValeur()" preceded by the sign "=" into the "After Update" property of your control. This does not change anything in the formula provided by LeMéruvien, which must also be entered.
The code provided here has the advantage of being universal, i.e., it is valid for all controls; indeed, it takes as the default value the last entered value, the one just typed.
Good luck and take care. -
Hello,
Enter the default value for the control using the formula: DLast("Field", "Table")
but be careful because the value retained will be the one valid at the time of opening the form.
To always have the last entered value, you need to add a line of code VBA:
Function FindDefaultValue()
...Control.DefaultValue = DLast("Field", "Table")
End Function
The function should be placed in the property "Before Update"
Note: The French syntax for direct entry in the property "Default Value", and the English syntax for VBA are different; with " , " in English, and with " ; " in French.
Best regards. -
Thank you for your interest in my problem.
I just inserted your two complete formulas (one after the other) into the default value of the field "dén_pays" in the "Pièces" table, changing the field and the table to their names, and then without changing them, I still get a message saying that the DLast function is unknown or incorrect. My Access 2007 is in French. Maybe I did not put the formula in the right place?
Thank you in advance. -
Re-Hello,
I said the control and not the field, meaning that the formula must be entered at the level of the form and the corresponding control for the field.
I just checked, and indeed Access does not recognize the formula directly on the tables but perfectly in the forms.
Good continuation. -
Good evening
I created a form. by selecting the field, I have in the properties sheet of the control. If I enter one of your two formulas, nothing appears in the field anymore. I don't see any other control, apart from an ActiveX control that offers me to add a video to my form...
I already tried this afternoon to create a form to find other possibilities. I can't figure out how to put your formula in. I even tried another one from a similar topic:
https://forums.commentcamarche.net/forum/affich-29143752-mettre-une-valeur-par-defaut-a-partir-du-dernier-enregistrement
but I can't get anything to work.
While waiting for your news, I'll keep testing everything I can find...
Teddy bear -
I don't understand, neither what you're doing nor what is wrong.
For the control, you need a "Text Box" control, if necessary using the assistant "Add Existing Fields" from the "Design" tab. Under no circumstances should you use an "ActiveX" control, which are special controls that require software other than Access, such as video or music or others.
The source of the control must be the field from the underlying table, and the formula I provided should be set on the "Default Value" property of this control, if necessary preceded by the symbol "=". -
Hello, here is a template of what you want:
https://www.cjoint.com/?3Kmpm6bMWyw
Take a good look at the "default value" property of the field [compteur_ancien], and put the same thing in your form, in the default property of your field.
Roger -
Hello, yes, it's in the data tab, but to avoid searching everywhere, I always systematically use the "all" tab.
Finally, in Access 2003, I'm not sure if it's the same in Access 2007.
Roger -
Hello to both of you.
With your explanations and the example, I just tried. Since I fiddled with my form in every way, I’m creating a new one based on the table model that suits me well for entering data in sequence and occasionally copying and pasting the identical items from the previous year, I only need to change the locations. The first field is the auto reference, the second is a dropdown list referring to a table, where I'm trying to have the same encoding as the previous one by default. It’s not working, but instead of an empty box, I get the first entry from my list of countries. After countless attempts, I think never mind, I’ll also try on my third field which is the year field that also refers to a table and is also a dropdown list and was also empty on the new entries. Strangely, it works for the second and third fields while the second remained unresponsive to my various requests. EXCEPT THAT... my form being based on the table model, the next entry appears as soon as I leave the automatic reference field... so the default field is indeed updated but with each time a delay in the entry... However, if I enter, for example, Finland, and I have Monaco appearing in the future entry to be encoded when I leave the form after the Finland line and then come back, I then have Finland instead of Monaco in the new pending entry.
Any ideas?
I know I’m a puzzle, and my Access 2007 must be as special as I am...
See you soon
Teddy -
Hello,
... I'm having a hard time understanding your explanations. The simplest thing would be for you to send the database through cjoint.com so we can take a closer look.
(You open and follow the instructions; by selecting a file, you upload your database. It's very easy. Don't forget to anonymize people's names if there are any)
Have a good continuation -
Hello,
Here is the link
https://www.cjoint.com/?0KrkOtTzYzk
The form is form 99 in Pieces.
You can then see if you enter a piece, the future registration appears as soon as you leave the auto num field. So the following registration already has the fields filled with your previous registration and not with the one you are currently completing, unless you close your database and reopen it; then your new registration is properly updated. The fields I would have liked to see updated are the 2nd and 3rd, namely den_pays and year.
If you find it, I would like you to tell me what you did, not just do it and send me back the database. (Because I continue to enter pieces, and also for the purpose of learning.) I might then stop asking questions in two or three years... ;-)
Thank you in advance for your interest
Teddy Bear-
https://www.cjoint.com/?0KrmY0zZ5QB
The first file didn't work, I don't know why, probably a saving error. This one works as I explained above.
Teddy bear
-
-
Hello,
I have installed the VBA code snippets I mentioned earlier.
I'm sending you the database back. There's not much to explain, just take a look. Open the form in Design mode and go to the "Events" tab. To open the corresponding code, you need to click on the small button at the end of the line.
I have to leave. I'll read you tonight.
http://cjoint.com/?3KrmtyIPHXz
Take care -
Re hello,
My computer must be possessed, it works for year but not for country in your version. I tried to copy it to my version, it didn’t work either...
Since it works for year, I tried to do the same for note. I get the same result as for country, i.e.: "#Name?"
Try: you open your corrected version, you enter a line. The country is correct, so it works upon opening. When entering the second line, you get this for country: #Name? while year displays the correct year. Doing the same for note I also get "#Name?"
I tried moving the code to before update, during entry, upon opening... nothing works... -
Good evening,
I'm sorry, I've seen the problem; but even after more than 2 hours turning everything upside down, I can't find what's blocking. The best option would be for you to stick with the first solution, even if that means having the second-to-last input as the default value.
Best of luck. -
Good evening,
I don't know the codes yet, but isn't there in your code an instruction that only accepts numeric values and that could be replaced, or an instruction that excludes text?
Good night and thank you again.
teddy bear -
Hello,
No! There can't be an issue with the code I used, 1°) because the command is: "Take the value that has just been entered in the control and make it the default value of that control", and 2°) because I use it for several controls in a personal form and I have no problems.
I believe the difficulty lies elsewhere, maybe a data format issue but I haven't found it. -
Hello,
the future will probably provide us with the answer at a completely unexpected moment...
or
akounamatata...
See you and thanks for all the efforts already made.
Teddy bear