OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Solved] Some simple macro examples

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base
View previous topic :: View next topic  
Author Message
Hvanderven
General User
General User


Joined: 01 Oct 2007
Posts: 37

PostPosted: Fri Nov 02, 2007 4:33 pm    Post subject: [Solved] Some simple macro examples Reply with quote

A couple of simple macro's. If you like them, use them. If you have other examples, please publish. Macro's are difficult to develop (at least for me), and all help is appreciated.
Thanks
Henk

The following macro I use to filter the records of a table in a form based on the value in textbox "FilterName". It finds al records that contain the content of "FilterName" in the table-field "Name":

Sub FilterName(Event As Object)
Dim Form As object
Dim Control As Object
Form=Event.Source.Model.Parent
Control=Form.GetByName("FilterName")
Form.Filter="Name LIKE '*" & Control.Text & "*'"
Form.ApplyFilter=True
Form.reload()
End Sub

The following macro I use to filter the records of a table in a form based on the value in textbox "FilterName". It finds al records that start with the content of "FilterName" in the table-field "Name":

Sub StartName(Event As Object)
Dim Form As object
Dim Control As Object
Form=Event.Source.Model.Parent
Control=Form.GetByName("FilterName")
Form.Filter="Name >= '" & Control.Text & "'"
Form.ApplyFilter=True
Form.reload()
End Sub

I use the macro below to copy database fields (txtEmail, txtphone) in textboxes in the same form (email, phone). After using this macro, the user can scroll to a next database record and copy the fields from the temp-fields(email, phone) to the database fileds using the macro below this macro:

Sub CopyDataToTemp(Event As Object)
Dim oForm As object
oForm=Event.Source.Model.Parent
oForm.GetByName("email").Text=oForm.GetByName("txtEmail").Text
oForm.GetByName("phone").Text=oForm.GetByName("txtphone").Text
End Sub

Sub CopyEmailToData(Event As Object)
Dim oForm As object
Dim email As object
oForm=Event.Source.Model.Parent
email=oForm.GetByName("email")
oForm.getByName("txtEmail").BoundField.updateString(email.Text)
End Sub

The following macro refreshes the data in a form (usefull if this data is changed in another form):

Sub FormRefresh(Event As Object)
Dim Form As object
Form=Event.Source.Model.Parent
Form.reload()
End Sub

The following macro can be linked to the "when loading"-event in the Form. It will maximize the form to a full screen-form. The macro's after this one are needed too:

sub onWhenLoading( oEvent as object )
MaximizeFrame( oEvent.Source.Parent.Parent.CurrentController.Frame)
end sub

Declare Function ShowWindow Lib "user32" _
(ByVal lHwnd As Long, _
ByVal lCmdShow As Long) As Boolean

sub MaximizeFrame( aFrame as object )
dim frame
dim window
dim handle
window = aframe.getContainerWindow()
handle = window.getWindowHandle(dimarray(), 1)
REM 1=WIN32
ShowWindow( handle, 3 )
end sub

The following 2 macro's will open a form or a report and need the following macro's:

sub OpenFormPersons
OpenForm(getFormsTC, getConnectionTC,"Persons")
End Sub

sub OpenReportCalender( oEv as variant )
OpenReport( getReportsTC, getConnectionTC, "Calender" )
end sub

function OpenForm( formContainer as variant, oConnection as variant, sFormName as string) as variant
Dim aProp(1) As New com.sun.star.beans.PropertyValue
aProp(0).Name = "ActiveConnection"
aProp(0).Value = oConnection
aProp(1).Name = "OpenMode"
aProp(1).Value = "open"
OpenForm = formContainer.loadComponentFromURL(sFormName,"_blank",0,aProp())
end function

function getFormsTC() as variant
getFormsTC = thisComponent.Parent.getFormDocuments
end function

function getConnectionTC() as variant
getConnectionTC = thisComponent.Drawpage.Forms(0).ActiveConnection
end function

function OpenReport( reportContainer as variant, oConnection as variant, _
sReportName as string) as variant
Dim aProp(1) As New com.sun.star.beans.PropertyValue
aProp(0).Name = "ActiveConnection"
aProp(0).Value = oConnection
aProp(1).Name = "OpenMode"
aProp(1).Value = "open"
OpenReport=reportContainer.loadComponentFromURL(sReportName,"_blank",0,aProp())
end function

function getReportsTC() as variant
getReportsTC = thisComponent.Parent.getReportDocuments
end function
Back to top
View user's profile Send private message
sonborj
Newbie
Newbie


Joined: 09 Nov 2007
Posts: 1
Location: Davao City , Philippines

PostPosted: Fri Nov 09, 2007 6:33 pm    Post subject: macros Reply with quote

Thanks for this.
_________________
Filipinos , be Proud!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
keithclark1966
OOo Enthusiast
OOo Enthusiast


Joined: 18 Jan 2007
Posts: 189

PostPosted: Mon Nov 12, 2007 12:14 pm    Post subject: Wonderful Reply with quote

Wonderfully done! We need more of this!

Keith
Back to top
View user's profile Send private message
sca957
General User
General User


Joined: 04 Nov 2007
Posts: 24

PostPosted: Thu Nov 29, 2007 5:12 pm    Post subject: Reply with quote

Could I get a little help please?
When I run the first macro example I get an error at:
Form=Event.Source.Model.Parent
of
BASIC runtime error,
Property or method not found.

Here's how the macro looks on my machine:

Sub New_Search(Event As Object)
Dim Form As object
Dim Control As Object
Form=Event.Source.Model.Parent
Control=Form.GetByName("Item")
Form.Filter="ItemA_Info LIKE '*" & Control.Text & "*'"
Form.ApplyFilter=True

What else do I need to change?
Thanks
Form.reload()
End Sub
Back to top
View user's profile Send private message
sca957
General User
General User


Joined: 04 Nov 2007
Posts: 24

PostPosted: Thu Nov 29, 2007 5:14 pm    Post subject: Reply with quote

Of course it actually looks like:

Sub New_Search(Event As Object)
Dim Form As object
Dim Control As Object
Form=Event.Source.Model.Parent
Control=Form.GetByName("Item")
Form.Filter="ItemA_Info LIKE '*" & Control.Text & "*'"
Form.ApplyFilter=True
Form.reload()
End Sub

Smile
Back to top
View user's profile Send private message
Landroving Linguist
General User
General User


Joined: 03 Dec 2007
Posts: 8
Location: Addis Ababa

PostPosted: Thu Dec 06, 2007 5:50 am    Post subject: Reply with quote

sca957 wrote:
Could I get a little help please?
When I run the first macro example I get an error at:
Form=Event.Source.Model.Parent
of
BASIC runtime error,
Property or method not found.

What else do I need to change?
Thanks
Form.reload()
End Sub


I was having the same thing going on, and it drove me crazy for the last two days. I finally figured out that I had to replace

Form.Filter="ItemA_Info LIKE '*" & Control.Text & "*'"
with
Form.Filter="ItemA_Info LIKE '*" & Control.CurrentValue & "*'"

That did it for me. I don't know why some people can use one thing and others have to use another. Maybe it is different versions of OO? I have 2.3.

Landroving Linguist
Back to top
View user's profile Send private message
sca957
General User
General User


Joined: 04 Nov 2007
Posts: 24

PostPosted: Sat Dec 15, 2007 9:20 am    Post subject: Reply with quote

Sorry for the late reply...
Code:
Form.Filter="ItemA_Info LIKE '*" & Control.CurrentValue & "*'"

That works for me too!
Thanks
Back to top
View user's profile Send private message
gajanan.palsule
Newbie
Newbie


Joined: 29 Nov 2007
Posts: 3

PostPosted: Wed Dec 19, 2007 2:40 pm    Post subject: Reply with quote

One more simple one for running any query

Function fnRunSql (vDbName AS String, vSQL AS String) AS Variant

BasicLibraries.loadLibrary("XrayTool")'xray thisComponent

oDBContext=createUnoService("com.sun.star.sdb.DatabaseContext")

If Not oDBContext.hasByName(vDbName) then
msgbox ("The database " + vDbName +" is not a registered datasource")
else

oDBSource=oDBContext.getByName(vDbName)
oConnectToDB=oDBSource.GetConnection ("","")

SQLStatement=oConnectToDB.createStatement

If Left(vSQL,6) = "SELECT" Then
Result1=SQLStatement.executeQuery(vSQL)
fnRunSql()=Result1
'XRay Result
Else
Result2=SQLStatement.executeUpdate(vSQL)
fnRunSql()=Result2
'XRay Result
End If
oConnectToDB.close
oConnectToDB.dispose()
'fnRunSql() = Result
end if

End Function

----------------------------------

And this one to change the form to full screen

sub FullScreen
rem ----------------------------------------------------------------------
rem define variables
'dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "FullScreen"
args1(0).Value = true

dispatcher.executeDispatch(document, ".uno:FullScreen", "", 0, args1())

end sub
Back to top
View user's profile Send private message
NiksaVel
General User
General User


Joined: 25 Jan 2008
Posts: 14
Location: Croatia

PostPosted: Sun May 11, 2008 6:20 am    Post subject: Reply with quote

I am trying to have my form switch to fullscreen when it is opened and the macro provided here seems to do just that, but I can't get it to work...

I succesfully use the openform macro and the second form opens, and even though I've mapped the fullscreen macro to the "when loading" event of the form, nothing at all happens, it opens in a regular window...


help pls?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
mmetelko
General User
General User


Joined: 12 May 2008
Posts: 16

PostPosted: Sun May 18, 2008 4:21 am    Post subject: Reply with quote

Okay i copied everything i need to open a form or report with macro. But i never did something like this before and i need some help how to properly apply macro to a button which will open a report or form.

I already assigned openform when mouse button released. I think i did it right because when i test it it returns error message: wrong number of paramters.

So where do i assign paramters?

Thanks in advance.
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Mon May 19, 2008 1:17 pm    Post subject: Reply with quote

If I understand it good you have two files
file A with the macro for opening file B
The macro for full screen must be in file B and does not ask for parameters.
I have try it and looks nice

Romke
Back to top
View user's profile Send private message
djskafish
General User
General User


Joined: 24 Nov 2009
Posts: 38

PostPosted: Thu Dec 03, 2009 4:16 pm    Post subject: Reply with quote

I'm a noob, so this may be a stupid question but...

Using the macros for opening a form, I get the following error:

BASIC runtime error.
Object variable not set.


on this line:

getFormsTC = thisComponent.Parent.getFormDocuments


Last edited by djskafish on Thu Dec 03, 2009 5:23 pm; edited 1 time in total
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Thu Dec 03, 2009 4:26 pm    Post subject: Reply with quote

Hello

Since OOo 3.1 you can use more easy code for opening a form.: see here
There nothing wrong with code in this thread but the new code is more easy.

Romke
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Base All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group