Solved How! auto fill a date Field by entering "Red light" in another field

RussPhi

New member
Local time
Today, 08:44
Joined
Jan 3, 2020
Messages
12
Hi
not the best at coding
I have a text Field (Combo) that you can select "Red light" or "Green Light" when the word Red light is selected I want todays date to go in to the RedLightDate Field and when the "Green Light" is selected I want the date it was selected to go to the GreenLightDate Field got any code for this.
Thanks for any help
Russ
 
What do you have as the "Row Source" property of the combo box
 
My Combo box has this "Red Light";"Yellow Light";"Green Light" in the Row Source
my combo is called [Status]
 
Try the following code in the combo's AfterUpdate event:
Code:
    Select Case Me.ActiveControl
 
           Case "Green Light"
               Me.GreenLightDate= Date
               Me.RedLightDate = ""
           Case "Red light"
               Me.GreenLightDate= ""
               Me.RedLightDate = Date
    End Select
 
Your best bet is the OnClick event that occurs when you make a selection from a combo box. However, it is not clear from your question how much you know about event routines. You might wish to study the subject of creating an event routine.

What you want is to activate the OnClick event of the Status combo box. If you create an event routine, it MIGHT look like this

Code:
Private Sub Status_Click()
    Select Case Status
        Case "Red Light"
            RedLightDate = Date()
        Case "Green Light"
            GreenLightDate = Date()
    End Select
End Sub

You didn't say what you wanted for YellowLight, but this is the general idea. You also didn't say whether you wanted the dates to be exclusive in case you changed the Status selection, so I didn't erase the other date fields.
Was this by any chance a class assignment?
 
@RussPhi - I answered the way I did because I've been on this forum for well over 20 years. I've seen class assignments before. This one as an early problem and the "used car salesman" project are quite common things for students to ask.

If you have trouble with a class assignment, understand that we will still answer questions - but we generally prefer to give hints or tell you the topics to look up. We don't get paid for doing your work and don't get grades either. If you are doing this to try to skate by on a class, just understand that someday it will catch up to you.

If this is NOT a class assignment, then OK, glad we could help. Though it is an odd-sounding problem.
 
Got it to work thanks guys. is there a way to lock the date field once it's got a Green light date like an after update code.
 
I'll answer the "lock" post. The answer is NO because anything that code can lock, code can unlock. However, if you wish to lock the COMBO box from manual action, you can do that via the .Locked property of that combo and thus prevent it from being selected again. There are some issues in doing this, however, because if you change to a new record, you would have to evaluate whether you needed to lock the control for the new record. If that is an issue, reply here.
 

Users who are viewing this thread

Back
Top Bottom