So, I assume you have a separate BB field and that the current value in that field would determine the valid range of NNN values, and then you want the next available NNN for that BB? What happens if you run out numbers within the BB's NNN range?
Or do I have this backwards and you really want this to drive off an incremental NNN, and you want to set the BB values based on how high the NNN is? If this is the case, what happens when you go over 999?