The only reason I can think of as to why this occurs is that the Save Data with Report option was selected. This means when there is no RecordSelection formula, the report does not need to connect to the database but once you add the formula it is trying to refresh the data.

Try this to see if the above is the cause.

Comment out the RecordSelectionFormula
After the OpenReport statement add
Rep.DiscardSavedChanges (not sure if that is the exact name of the method name)

If you get the same error then you need to ensure a login has been set for all databases used by the report.