With a bit of difficulty. (Unless someone can think of something I'm missing.)
First, no lock I'm aware of on Hubitat supports literally enabling or disabling a code; they are just added and removed from the device's own list of lock codes. (I think Z-Wave actually has provisions for built-in schedules, but I'm not sure how many locks actually implement this and don't know of anything on Hubitat that would use it.)
Second, I'm assuming the above is what you mean by a lock code being "enabled"--being available, on the lock, for use. In this case, no stock app I'm aware of (and not Rule Machine as far as I can tell) allows automating things based on the value of the
lockCode attribute for that device, which is what you'd need to read. This isn't impossible to do; you'd just need a custom app to read the attribute and see if whatever criteria you're looking for are met (a code matching a specific name? a code matching a specific value/code?). But it would probably be easier to incorporate the switch into whatever automation you're using for the codes in the first place.
But ... perhaps you just mean when a lock code is used. That's easy and can be done with, among other possibilities, Rule Machine: just trigger on a specific lock code entered, then configure your actions to turn on that switch. You'd also need to figure out how to turn it off (maybe if the door is unlocked without a code, as it might be done from the inside? or automatically at a certain time every day if you have a predicable schedule? until some other code is used?).