If this code works for you but you need a better place to execute it/a place that will execute that is not tied to that form's definition you can run it in the AutoExec macro.
The AutoExec
macro is a macro named AutoExec
which runs when you start your Access app. You can use the RunCode
macro function to call your VBA function (it does have to be a function and not a sub).
This way you can (re)link your table before the form is even opened.