Creating macros in Base

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
jennymac
Posts: 7
Joined: Sat Dec 07, 2013 6:32 am
Location: Bendigo Victoria Australia

Creating macros in Base

Post by jennymac »

Hi,
I have been trawling the forum for macros to do functions that in need.
I have found some code but when I have inserted it into my macro library, it has deleted everything else that was all ready there.
I read a document I found called "Getting started with macros" and it may as well have been in Klingon for all the value I was able to get from it.
This is what I have done.
Open the DB > tools > macros > organize macros > open office basic.
At this point I highlighted the library (standard) so that 'new' became available.
I clicked 'new' which opened up the macros window and I copied whatever piece of code I had found.
The tricky bit then was there was no 'save' button just a 'save as'.
When I clicked 'save as' I got my new macro but all the others were gone.
Yes, I was working on a saved copy of the DB so could go back to what I had. ;)
Any suggestions about where I am going wrong?
One of the macros I want is a macro on a push button that opens a new/clean record in a particular form.
I found this code and was inserting it in the create macro window. I assumed I just need to copy and insert the whole piece of code. I did change the line with FormName in it to the form I need opened.
Many thanks in advance.
Jenny

Code: Select all

Sub AutoExec 
Dim LastFrame As Object 
Dim NumFrames As Integer 
Static FormDocs As Object 
Dim DBDoc As Object 
Dim ImpName As String 
Dim DataSource As Object 
Dim Conn As Object 
Dim Args(0) As New com.sun.star.beans.PropertyValue 
Dim FormName As String 
Dim FormDoc As Object 
Dim oFormulario as Object: Dim oFeld as Object 
On Error Goto HandleError 
FormName= "frm_CustomerDetails": 
ImpName="com.sun.star.comp.dba.ODatabaseDocument" 
NumFrames=StarDesktop.Frames.Count 
LastFrame=StarDesktop.Frames.getByIndex(NumFrames-1) 
If LastFrame.Frames.Count>1 Then: 
Exit Sub 
End If 
If Not (LastFrame.Controller.Model.ImplementationName=ImpName) Then 
Exit Sub 
End If 
DataSource=LastFrame.Controller.DataSource 
DBDoc=DataSource.DatabaseDocument 
FormDocs=DBDoc.FormDocuments 
Conn=DataSource.getConnection("","") 
Args(0).Name="ActiveConnection" : Args(0).Value=Conn 
if FormDocs.hasByName(FormName) Then: 
FormDoc=FormDocs.loadComponentFromURL(FormName,"_self",0,Args() ) 
FormDoc.CurrentController.Frame.ContainerWindow.setFocus() 
End If 
With FormDoc.getCurrentController().getFrame().getContainerWindow() 
.setPosSize( 100, 200 , , , com.sun.star.awt.PosSize.POS) 
.setPosSize( , , 1600, 842 , com.sun.star.awt.PosSize.SIZE ) 
End With 

HandleError: 
If Err<>0 Then 
Exit Sub 
End If 
End Sub
Open Office 4.0.0
Windows 7
Joomla 1.0 - 3.2
User avatar
DACM
Volunteer
Posts: 1138
Joined: Tue Nov 03, 2009 7:24 am

Re: Creating macros in Base

Post by DACM »

The code you've posted above is intended to open a Form upon opening Base. I haven't examined that particular code in detail but at first glance it could be problematic. It looks similar to the code we came to associate with the "Form Flush" issue related to opening a Form using the Customize > Open document event. Needless to say, I would proceed with some caution with that code.

But if all you want is...
jennymac wrote:...a macro on a push button that opens a new/clean record in a particular form.
...then perhaps take a look at these examples: These examples may also clear-up your questions about macro storage within the .odb document.
Last edited by DACM on Thu Apr 24, 2014 8:06 am, edited 2 times in total.
AOO 4.1.x; LO 4.2.x; Windows 7/8 64-bit
Warning: Avoid embedded databases --> Solution: Adopt a portable 'split database' folder
Soli Deo gloria
kmihalj
Posts: 2
Joined: Thu Apr 24, 2014 12:52 am

Re: Creating macros in Base

Post by kmihalj »

Nice ... I use macros from example in previous post, .... but...

What if I don't have filtered record in other form (no relation data created) .... I need macro to open filtered data in another form or to create new record if data is not existend ... well I can add data to for opened form, but trouble is listbox in which if I select data (for new record) I get error, and nothing is entered (unless I disable macro for listbox)

Now I use this...

Code: Select all

Sub Otvori_Upisni_List_iz_Maticnog_Lista (oEvent As Object) 'This opens another form with filtered data
	oForm = oEvent.Source.Model.Parent
	iID=oForm.getByName("ID_Maticni").text
	oForm2 = ThisDatabaseDocument.FormDocuments.getByName("Upisni") 
	oForm2.Open 
	oForm3 = oForm2.Component.DrawPage.Forms.getByName("Upisni_List")
	oForm3.Filter="( ""UPISNI_LIST"".""FK_Maticni"" = " & iID & " )" 
	oForm3.ApplyFilter=True 
	oForm3.Reload() 
end sub

Sub Refresh_MainForm (oEvent As Object) 'List Box > Execute (event)
   oForm = oEvent.Source.Model.Parent 
   oEvent.Source.Model.commit() 
   oForm.updateRow() 'this is point where I get error when entering data to new record via listbox, ... changing data on existing records works fine
   oForm.reload
End Sub 
CentOS 6.5, LibreOffice 4.2.x
User avatar
DACM
Volunteer
Posts: 1138
Joined: Tue Nov 03, 2009 7:24 am

Re: Creating macros in Base

Post by DACM »

kmihalj wrote:I need the macro to open another form [filtered] or create a new record if [there's no related data].
  • Code: Select all

    oForm.updateRow() 'this is point where I get error when entering data to new record via listbox, ... changing data on existing records works fine
Try:

Code: Select all

Sub Refresh_MainForm (oEvent As Object) 'List Box > Execute (event)
   oForm = oEvent.Source.Model.Parent 
   oEvent.Source.Model.commit()
   iRow = oForm.getRow 'Save the current row number
   IF oForm.isnew THEN oForm.insertRow() ELSE oForm.updateRow() 
   oForm.reload
   oForm.absolute(iRow) 'jump to the current row after form reload
End Sub
AOO 4.1.x; LO 4.2.x; Windows 7/8 64-bit
Warning: Avoid embedded databases --> Solution: Adopt a portable 'split database' folder
Soli Deo gloria
kmihalj
Posts: 2
Joined: Thu Apr 24, 2014 12:52 am

Re: Creating macros in Base

Post by kmihalj »

Thanx ..... this works !!!
CentOS 6.5, LibreOffice 4.2.x
Post Reply