Cannot get macro to open a from from a button press to work

I’m running a Mac with Mac OS Sequoia 15.5. I installed Libreoffice 25.2.4.3, created a new database, and added two forms. I configured one form to display some data from a table. I created a second form to be the Main form. I added a button to the Main form with a macro to open the other form. I have been getting errors when attempting to open the form with the button. I did some extensive troubleshooting that included resetting my user profile, reinstalling the Libreoffice software, creating another simple database with forms, buttons and the same macro. I uninstalled version 25.2.4.3 and installed version 24.8.7.2, created a fresh simple database, added forms, a button, and continue to get the same error. I have been using Google Gemini to help troubleshoot. Gemini tells me that: This error message is definitive: it means that when the macro runs, LibreOffice is identifying the active document as a LibreOffice Writer document (SwXTextDocument), and not as a native LibreOffice Base form (which would implement XOfficeDatabaseDocument).

Has anyone else had this issue with Libreoffice Base? If so how did you resolve it?

You may skip this post. I didn’t have/had this problem.
.
But if you seek help here, it could prove useful to tell the error message here, provide the code of your macro and maybe add a bit on your form. External form (maybe has lost connection to the database), internal form etc. You work with hsqldb, firebird, sqlite, mariaDB, Postgres?
.
At the current stage I can only wish you good luck with your AI-support…

Some more from the imagination of AI:
.
To convert a LibreOffice Writer form (ODT) into a Base form (ODB), the process is not straightforward or automatic within LibreOffice’s standard interface. However, it is possible using macros and some manual steps:

  • Macros are required: According to community discussions, you need to use a macro to import a Writer form back into a Base database file. There is no built-in menu option for this conversion.
  • Adapt the macro: The macro must be adapted to your specific form names and file locations. The macro is typically contained within a sample .odb file (such as ImporterForm.odb). You can access and modify this macro using the LibreOffice Basic macro editor.
  • How to proceed:
    • Download or obtain a sample .odb file with the macro (like ImporterForm.odb).
    • Open it in LibreOffice Base.
    • Go to ToolsMacrosOrganize MacrosLibreOffice Basic.
    • Locate and review the main or mainAndrew procedures in the macro code. These show how to perform the import from ODT to ODB.
    • Adapt the macro code to reference your Writer form and the destination Base file.
    • Run the macro to perform the import.

Important notes:

  • This process is somewhat technical and may require basic familiarity with LibreOffice Basic macros.
  • There is no official, fully-automated conversion tool provided by LibreOffice for this task.
  • If you encounter errors or need more guidance, community forums (like Ask LibreOffice) can provide additional help.

Here is the error message:
Debug Error 2: The current document is not recognized as a LibreOffice Base form or report.
Please ensure the form is fully opened in ‘Form View’ and is part of a Base database.
Detected document type (implementation name): SwXTextDocument

Here is the code in the macro:
"Sub OpenFormByName(oEvent As Object)
’ This macro opens a specified form in LibreOffice Base.
’ It can be assigned to a button’s “Execute action” event.
’ This code is compatible with LibreOffice version 25.2.4.3 on macOS Sequoia 15.5.

Dim oDatabaseContext As Object
Dim oDataSource As Object
Dim sFormName As String
Dim oForm As Object
Dim oForms As Object
Dim oCurrentDoc As Object         ' Represents the active document
Dim oCurrentController As Object  ' Represents the active view of the document
Dim sDocImplementationName As String ' For debugging the document type

' --- Configuration ---
' IMPORTANT: Replace "YourFormNameHere" with the actual name of your form.
' For example, if your form is named "Customers", change this to sFormName = "Customers"
sFormName = "MyNativeForm" ' e.g., "CustomersForm", "OrdersEntry"
' ---------------------

On Error GoTo ErrorHandler

' --- Attempt to get the current document more robustly ---
' This approach tries to get the document from the current desktop frame,
' which can sometimes resolve context issues with ThisComponent.
If Not IsNull(StarDesktop.getCurrentFrame()) Then
    If Not IsNull(StarDesktop.getCurrentFrame().getController()) Then
        oCurrentDoc = StarDesktop.getCurrentFrame().getController().getModel()
    End If
End If

' --- Critical Context Checks ---
' These checks are designed to pinpoint why the document might not be recognized as a Base form.

' Check 1: Is oCurrentDoc Null?
If IsNull(oCurrentDoc) Then
    MsgBox "Debug Error 1: The active document (oCurrentDoc) is Null. The macro could not determine the current document context.", _
           16, "Macro Context Error"
    Exit Sub
End If

' Get the implementation name for debugging purposes
On Error Resume Next ' Temporarily disable error handling for this specific property access
sDocImplementationName = oCurrentDoc.getImplementationName()
On Error GoTo ErrorHandler ' Re-enable error handling

' Check 2: Does oCurrentDoc have the expected database document interface?
' A LibreOffice Base form is part of an XOfficeDatabaseDocument.
If Not HasUnoInterfaces(oCurrentDoc, "com.sun.star.sdb.XOfficeDatabaseDocument") Then
    MsgBox "Debug Error 2: The current document is not recognized as a LibreOffice Base form or report." & Chr(13) & _
           "Please ensure the form is fully opened in 'Form View' and is part of a Base database." & Chr(13) & _
           "Detected document type (implementation name): " & sDocImplementationName, _
           16, "Macro Context Error"
    Exit Sub
End If

' Check 3: Is the document's controller active (meaning the form is fully open and visible)?
oCurrentController = oCurrentDoc.getCurrentController()
If IsNull(oCurrentController) Or Not oCurrentController.isConnected() Then
    MsgBox "Debug Error 3: The document's controller is not active or connected." & Chr(13) & _
           "Please ensure the form is fully loaded and visible in 'Form View' before clicking the button.", _
           16, "Macro Context Error"
    Exit Sub
End If

' Check 4: Is the DataSourceName available and valid for the current form?
If IsNull(oCurrentDoc.DataSourceName) Or oCurrentDoc.DataSourceName = "" Then
    MsgBox "Debug Error 4: The current form is not linked to a data source." & Chr(13) & _
           "Please ensure your form is saved and properly associated with a database.", _
           16, "Macro Debugging Info"
    Exit Sub
End If
' --- End Critical Context Checks ---


' Get the database context service
oDatabaseContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
If IsNull(oDatabaseContext) Then
    MsgBox "Debug Error 5: Could not create DatabaseContext service. LibreOffice installation issue?", _
           16, "Macro Debugging Info"
    Exit Sub
End If

' Get the specific data source by name
On Error GoTo DataSourceErrorHandler ' Temporarily switch error handler for this specific step
oDataSource = oDatabaseContext.getByName(oCurrentDoc.DataSourceName)
On Error GoTo ErrorHandler ' Switch back to general error handler

If IsNull(oDataSource) Then
    MsgBox "Debug Error 6: Data source '" & oCurrentDoc.DataSourceName & "' not found or accessible." & Chr(13) & _
           "Please ensure the database is registered and the name is correct.", 16, "Macro Debugging Info"
    Exit Sub
End If

' Ensure the data source is connected
If Not oDataSource.IsConnected Then
    oDataSource.connect()
End If

' Get the forms collection from the database document
If IsNull(oCurrentDoc.Forms) Then
    MsgBox "Debug Error 7: Could not access the Forms collection from the current document." & Chr(13) & _
           "Ensure this macro is called from a form document.", 16, "Macro Debugging Info"
    Exit Sub
End If
oForms = oCurrentDoc.Forms

' Check if the target form exists and open it
If oForms.hasByName(sFormName) Then
    oForm = oForms.getByName(sFormName)
    oForm.open()
Else
    MsgBox "Error: Form '" & sFormName & "' not found in the database." & Chr(13) & _
           "Please verify the form name in the macro code ('" & sFormName & "') matches an existing form.", _
           16, "Form Not Found"
End If

Exit Sub

DataSourceErrorHandler:
MsgBox "An error occurred while trying to get the data source: " & Err.Description & Chr(13) & _
"Error Number: " & Err.Number & Chr(13) & _
“This often happens if the database is not properly registered or the form is not saved/linked.”, _
16, “Macro Error - Data Source”
Exit Sub

ErrorHandler:
MsgBox “A BASIC runtime error occurred:” & Chr(13) & _
"Error Description: " & Err.Description & Chr(13) & _
"Error Number: " & Err.Number & Chr(13) & _
“If you are seeing this, it means one of the specific debug checks above did not trigger, " & _
“but a general error occurred later. Please double-check macro assignment and security.”, _
16, “Macro Execution Error”
End Sub”

The form is an internal form created using the Form Wizard. The macro resides inside the database. I’m working with hsqldb.

From the introducing comments I suspect the macro is generated by an AI?

Yes. The original macro that I borrowed from another database did not work. I asked Google Gemini to help troubleshoot and that AI rewrote the macro code. The AI wrote several iterations during the troubleshooting in order to isolate what was causing the macro to not work.

I found a solution that is much cleaner and easier to understand than my original macro. Here is the code for the button:

Sub Open_[FormName]
const sNewDocumentName="[FormName"
const sOldDocumentName=“frm_Main”
REM STORE? ThisDataBaseDocument.FormDocuments.getbyname(sOldDocumentName).store
ThisDataBaseDocument.FormDocuments.getbyname(sOldDocumentName).close
oNewFormDocument = ThisDataBaseDocument.FormDocuments.getbyname(sNewDocumentName).open
End Sub

Thanks for the help.

Can you put your code in a code block?

I put it in another reply and marked it as the solution.