BASIC runtime error oDS=ThisDatabaseDocument.CurrentController

I keep getting a BASIC runtime error Variable not defined when doing the following
oDS = ThisDatabaseDocument.CurrentController
oDS.connect()
oConnection = oDS.ActiveConnection()

	I also tried the following which gives the same error
          oConnection = ThisComponent.DataSource.getConnection("","") 

the first line causes the error. The subroutine will run one time when i first open the data base, but will not run after that.
I am using FireBird embedded on a linux system
I am a newbe for sure
I am running this macro from the GUI and from the macro editor, not attached to any event

Demonstration of this type of error:

Option Explicit

Sub Test()
  v = 1
End Sub

The solution is simple: add variable declarations (not necessarily with types):

Sub Test()
  Dim v as Long
  v = 1
End Sub

Where did you save the macro code? Should be saved inside the database file.

Having a form (embedded of not) calling some macro (embedded or not):

Sub Some_Form_Event(ev)
oForm = ev.Source
oConnection = oForm.ActiveConnection

End Sub

Sub Some_FormControl_Event(ev)
oModel = ev.Source.getModel()
oForm = oModel.getParent()
oConnection = oForm.ActiveConnection

End Sub

All variables are declared as type object

I am trying to run an SQL update query, which sometimes works when this error does not occur

Could you upload a sample of the problematic file and provide the sequence of actions that cause the problem.