Hello bonnydeory,
well first at all i want to say, that your statement and the code do not fits.
Either you have got this code from another place, or you are not just a beginner in coding.
But anyway, did you already checked both databases for their permissions???
Put all permissions of both DATABASES to the lowest level-security for TESTING.
And also make sure, that your datasets, are setted correctly (READ & WRITE)-CHECK.
The fact, that your code works in “Preview-Mode” tells that your code seems to be ok and that you just do not have permissions to enter the database.
(on my opinion).