Base - automatically running a macro on form opening

Hi Egbert,

If you are running on a Windows machine, I have found ways to do what you
were asking and I could make some suggestions if that would help.

Noel

Well, for sure you can fire a macro when a document opens. I expect it is the same for a form, but I have not tried it. Try this:

Tools > Customize > Events

You can set macros for Document loading finished (for example). There is a drop-down in the bottom for "Save in". Set this to the form, not LibreOffice since you only want this for your specific form / document.

Next, you want a macro that is triggered when you move to the next record. I don't remember off hand if you can do this (I have not looked at it in a few years). If I did, I probably mention it in AndrewBase.odt if I ever did this specifically. I just don't remember and I do not have time to look it up right now.

A long set of macros to open & close forms.

To open a form when the database starts
select "Tools > Customize > Events > Open Document.
Click Assign: Macro and assign the DBOpen macro.

HERE ARE THE MACROS.
As I can't send attachments you will have to copy and paste these into your Macro Library

'****** LOAD LIBRARY ******
'Loads all the tools libraries and opens the connection
Sub DBOpen(Optional poEvent As Object)
dim mydbdoc,CurrentDb,mymsg,metb
On Local Error GoTo Error_Sub
  mydbdoc = ""
  metb = ""
  ' load the access library containing the functions similar to MS Access
  If GlobalScope.BasicLibraries.hasByName("Access2Base") then
   If NOT GlobalScope.BasicLibraries.isLibraryLoaded("Access2Base") Then
    GlobalScope.BasicLibraries.LoadLibrary("Access2Base")
    mymsg = "Access loaded"
   End if
  End if
  REM Use some methods from the Tools library.
  If NOT GlobalScope.BasicLibraries.isLibraryLoaded("Tools") Then
    GlobalScope.BasicLibraries.LoadLibrary("Tools")
    mymsg = mymsg & " Tools loaded"
  End If
  'loaded the Standard library which applies to this database
  If NOT GlobalScope.DialogLibraries.isLibraryLoaded("Standard") Then
      GlobalScope.DialogLibraries.LoadLibrary( "Standard" )
      mymsg = mymsg & " Standard dialog loaded"
  end if

    mydbdoc = ThisDatabaseDocument
   ' msgbox "The following was loaded " & mymsg ,6 , "Loaded"
  Call OpenConnection(ThisDatabaseDocument)
  call openThisForm
Exit_Sub:
    Exit Sub
Error_Sub:
    TraceError("ERROR", Err, "Opening database at DBopen", Erl)
    GoTo Exit_Sub
End Sub

'********** Use this for opening a data form ***********
'we use this to open the main menu when the document first opens
Sub openThisForm(optional openname as variant,optional closefrmname as variant)
  dim sfrmName,openit
' This is the variable for the form that must be opened at the start of program
'the normal form to open is the "noticefrm", but for data entry on household surey
'we use the demo_menu form. Data entry is restricted this way.
if IsMissing(openname) then
   sfrmName = "noticefrm" ' "planningf/demo_menu" '
else
  sfrmName = openname
end if
   'Closefrmname is the form with the calling btn on it or one that was sent from a script
if IsMissing(closefrmname) then
   closefrmname = "noticefrm"
end if
   call thesearetheforms(closefrmname,sfrmName)
end sub

'**** Use this to open any form from another form ********
'Open a form that may be in a folder. The folders are defined by / i.e plan/planmenu
Sub openHierachForm(optional openname as variant,optional closefrmname as variant)

  dim sfrmName as string
  dim sfrmtoclosename as string
  dim openit, otestform
  'dim canIopenthis as boolean ' These are used to check tha the user is
  'canIopenthis = false "allowed to open & view this form
' openname is the variable for the new form that must be opened
if IsMissing(openname) then
   sfrmName = "noticefrm"
else
'the source is the button that was clicked. The button may have a tag in the Additional Information field
'This tag defines who is allowed to open the form. If there is no tag then anyone can open the form.
   otestform = openname.source.model
   'check to see if the user has permission to open the openname form
   'canIopenthis = checkstaffnames(otestform)
  'if canIopenthis = false then
  ' msgbox ("Sorry you are not authorised to view this form",6,"Not authorised")
  ' exit sub
  ' end if
  sfrmName = openname.source.model.name
end if
'the form we were on when we clicked the "open form" button
if IsMissing(closefrmname) then
   sfrmtoclosename = openname.source.model.parent.name
else
   sfrmtoclosename = closefrmname
end if
  call thesearetheforms(sfrmtoclosename,sfrmName)
end sub
'Changed 9 Sept 2015. The names of the forms to open and close
SUB thesearetheforms(sfrmtoclosename,sfrmName)
'open the new form before closing the original. Prevents "flash" of desktop.
if thisDatabaseDocument.FormDocuments.hasByHierarchicalName(sfrmName) then
  thisDatabaseDocument.FormDocuments.getByHierarchicalName(sfrmName).open
  else
  msgbox "Sorry I can't find the form " & sfrmName & chr(10) & "Please check the name!",6,"Form not found"
end if
if sfrmtoclosename <> sfrmName then
  if thisDatabaseDocument.FormDocuments.hasByHierarchicalName(sfrmtoclosename) then
    thisDatabaseDocument.FormDocuments.getByHierarchicalName(sfrmtoclosename).close
  elseif ThisDatabaseDocument.FormDocuments.hasbyname(sfrmtoclosename) then
    ThisDatabaseDocument.FormDocuments.getbyname(sfrmtoclosename).close
  else
    msgbox "sorry I can not close the form " & sfrmtoclosename,0,"Not closed form"
  end if
end if 'open & close forms have same name
end sub

Tony Bray
tonybsa@mac.me
MacBook Pro 15 inch Mid 2009
2.8 GHz Intel Core 2 Duo, 4 GB RAM,
Mac OS X 10.11.2
LibreOffice 4.4.5
Scribus 1.4
Parallels Desktop 11.02
Ubuntu 14.04
LibreOffice 4.4.4