Executing SQL Statements from Forms with macros

Hello all,

I’ve been using some macros to automate certain SQL statements with the following code:

sub executeSQLcmd(SQLcmd as String)
      Dim oStatement As Object
      if IsNull(ThisComponent.CurrentController.ActiveConnection) then
       ThisComponent.CurrentController.connect
    endif
    oStatement = ThisComponent.CurrentController.ActiveConnection.createStatement()
    oStatement.execute(SQLcmd)
end sub

I’ve always used Tools > Macros > Execute to get the job done but I grew tired of this and tried to assign some buttons in a simple form to do the same but without success. The BASIC interpreter throws an exception at the line if IsNull(ThisComponent.CurrentController.ActiveConnection) then and says ActiveConnection is not found.

Perhaps there’s no ActiveConnection to be found in a Form? If so, what’s the better alternative to the class ThisComponent.CurrentController?

Hello,

I run SQL from forms quite often. Here is one method:

if IsNull(Thisdatabasedocument.CurrentController.ActiveConnection) then
    Thisdatabasedocument.CurrentController.connect
endif
oStatement = Thisdatabasedocument.CurrentController.ActiveConnection.createStatement()

Here is another:

oForm = ThisComponent.Drawpage.Forms.getByName("YOUR_INTERNAL_FORM_NAME")
oStatement = oForm.ActiveConnection.createStatement()

Both worked great! Thank you.