Hello,
This code works as a toggle.  Attach to a button.  Click & table shown.  Click again and table removed.
Option Explicit
Sub GetGrid
    Dim dispatcher As Object
    Dim oFrame as Object
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    oFrame = ThisComponent.CurrentController.Frame
	dispatcher.executeDispatch(oFrame, ".uno:ViewFormAsGrid", "", 0, Array())
End Sub
Interesting note.  Question doesn’t mention sub forms or such.  The Button is to be places on the Form or Sub Form which is to be displayed.  The code remains the same for each.  Where the button is placed is where the toggle takes place.  So for a form with main and sub, two buttons - one on each and each connected to the same macro.