Autopopulate years and make previous years grey meaning you will not use

Local time
Today, 16:50
Joined
May 11, 2023
Messages
46
Hello everyone. I am trying to come up with a code to autopopulate years every 1st January every year and make previous years grey as in Lock Previous years. Kindly assist me with the code please. Thanks in advance.
 
when user changes record, change the color
Code:
Const kGRAY = 15132391

sub Form_Current
 if txtYear = Year(date()) then
    txtYear.forecolor = vbBlack
else
    txtYear.forecolor =kGRAY
endif
end sub
 
when user changes record, change the color
Code:
Const kGRAY = 15132391

sub Form_Current
if txtYear = Year(date()) then
    txtYear.forecolor = vbBlack
else
    txtYear.forecolor =kGRAY
endif
end sub
Thanks. Is there a way to lock it? Let's say to lock previous years every 31st December every year
 
your question is very vague

- what is 'it'?. a control called txtYear or similar? a complete record? and in a single form or a continuous form?
- what are you using to determine a prior year? a date field? a field which just contains the year?
- if a date field, does this also contain a time element?
- what does 'locked' mean? you can still view it (depending what 'it' means) but can't change it?

assuming it is a single control containing a year value and in a continuous or single form, use conditional formatting

expression is..... txtYear<year(date())

and set the format to Disabled

if a date field with or without a time element try

expression is..... txtDate<(date()-format(date(),"y")+1

This disables the control so you cannot select it - so it is locked
 
Kindly assist me with the code please. Thanks in advance.
You might want to learn how form level events work. They are the key to ensuring data is valid and users don't change history. Your question is to vague for us to offer specific instructions but you can use the form's BeforeUpdate event to keep a user from entering a previous (or future for that matter) year into a date field.

At this link you will find three videos as well as the database that was used in the videos. It will help you to learn how form and control events work and which ones are best for which purposes.

 

Users who are viewing this thread

Back
Top Bottom