Autopopulate years and make previous years grey meaning you will not use (1 Viewer)

Local time
Today, 11:17
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.
 

Ranman256

Well-known member
Local time
Today, 04:17
Joined
Apr 9, 2015
Messages
4,337
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
 
Local time
Today, 11:17
Joined
May 11, 2023
Messages
46
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
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:17
Joined
Feb 19, 2013
Messages
16,670
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
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:17
Joined
Feb 19, 2002
Messages
43,484
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

Top Bottom