Get & assign Membership level

How can I dynamically set the membership level? For example I am registering a user and storing their 'access' level in a custom field. Upon login I want to check if they are assigned a membership level and if not, then use the meta data value to assign their membership level.