I suspect you would find it a lot easier if your usercode was split into it's different components - what happens if you have two usercodes of the same length? but you can try something like
Code:
SELECT TDepts.Dept, TDepts.DDescr, tUserSec.UserIDX
FROM TDepts INNER JOIN tUserSec ON TDepts.Dept Like tUserSec.[usercode] & "*"
WHERE Len(usercode)=(SELECT Max(len(usercode)) FROM tUserSec AS T WHERE TDepts.Dept Like T.[usercode] & "*")
Note: because of the join, this cannot be displayed in the query window, only the sql window
this might work instead and will show in the query window
Code:
SELECT TDepts.Dept, TDepts.DDescr, tUserSec.UserIDX
FROM tUserSec, TDepts
WHERE TDepts.Dept Like [usercode]&"*" AND Len(usercode)=(SELECT Max(len(usercode)) FROM tUserSec AS T WHERE TDepts.Dept Like T.[usercode] & "*")