Programming a button on a form

Discuss the database features
Post Reply
dreamquartz
Posts: 898
Joined: Mon May 30, 2011 4:02 am

Programming a button on a form

Post by dreamquartz »

Hello All,

Can a button be programmed to goto a website, wich comes from a table in a database?
I know a button can goto a website, but that is in a form, so getting a new record, does not change the website.

Any thoughts?

Dream
LO 7.x, HSQLDB 2.7.x & Ubuntu 24.04 LTS.
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Programming a button on a form

Post by Villeroy »

A quick Python snippet:

Code: Select all

def on_record_change(oEv):
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("btnHyperlink")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = field.getString()
oEv.Source is the calling form.
"btnHyperlink" is the name of the button in question on that form.
That button keeps the field name as "Additional Info" (the Tag property).

Every time you change the record, the button will get another target URL.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dreamquartz
Posts: 898
Joined: Mon May 30, 2011 4:02 am

Re: Programming a button on a form

Post by dreamquartz »

Villeroy wrote:A quick Python snippet:

Code: Select all

def on_record_change(oEv):
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("btnHyperlink")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = field.getString()
oEv.Source is the calling form.
"btnHyperlink" is the name of the button in question on that form.
That button keeps the field name as "Additional Info" (the Tag property).

Every time you change the record, the button will get another target URL.
Uhm, now what?
I have never used this principle, so how to use it?
I can immagine a macro, but that is it.
I want to avoid macros at all cost, if at all possible.

Can you please provide me with a little more info?

Thanks in advance

Dream
LO 7.x, HSQLDB 2.7.x & Ubuntu 24.04 LTS.
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Programming a button on a form

Post by Villeroy »

dreamquartz wrote:I want to avoid macros at all cost, if at all possible.
The subject line "Programming a button on a form" is misleading.
Keep the URL in an ordinary text field and drag it (or copy&paste) into a browser window to open the URL.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Programming a button on a form

Post by Villeroy »

Oh, yesterday's solution works with Calc's HYPERLINK function too. Dump the data you are interested to a Calc report and add a HYPERLINK field.
viewtopic.php?f=47&t=71983
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dreamquartz
Posts: 898
Joined: Mon May 30, 2011 4:02 am

Re: Programming a button on a form

Post by dreamquartz »

Villeroy wrote:
dreamquartz wrote:I want to avoid macros at all cost, if at all possible.
The subject line "Programming a button on a form" is misleading.
Keep the URL in an ordinary text field and drag it (or copy&paste) into a browser window to open the URL.
I am dealing the FaceBook and Twitter buttons alike.
The Client would like to see them on a form that presents their client data.

If there is no other way than the snippet you gave me, than it is what it is, and I have to get to the Client and explain that.
The method you gave here, is the one that I am using now, but that really looks awful, so I wanted to improve on it a little, by using a button.

Concerning the snippet you gave me, I have no idea how to use it.
I will have a look at your other post as well.

Thanks,

Dream.
LO 7.x, HSQLDB 2.7.x & Ubuntu 24.04 LTS.
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Programming a button on a form

Post by Villeroy »

Windows: go to folder %APPDATA%\OpenOffice\4\user\Scripts
LInux: the path is ~/.openoffice/4/user/Scripts
Drop the attached zip file in that directory.
Right-click the zip file and choose "Extract Here" (or similar command).
This should extract ./python/pyDBA/URLButton.py into the current Script folder. If not, open the archive and tell your zip-program to do the right thing.

Open the attached biblio.urlbutton.odt and enter some test URLs into the Address field. The form data will be written into your Bibliography.biblio.Address field. You may also use the data source window for data entry. Valid test URLs start with either one of:
http://
https://
ftp://
mailto:
The list of valid protocols can be expanded in the macro source code using any capable text editor. It has UNIX line feeds because I saved it under Linux but it works with Windows or Mac line feeds too.

Use the navigation tool bar to scroll back some records. For each record having a valid URL, the button will be enabled. Records with no valid URL will disable the button (which is a new feature I've added to the first version).

That's all. Simply drop the Python archive into the Script folder of the user profile and extract the contained subfolder(s) with the Python code. Then open the form which is linked to the registered Bibliography database having an event driven macro pointing to some code in <profile>/Scripts/python/pyDBA/URLButton.py

Now for the form which is simply bound to the biblio table of your Bibliography example database which is part of every default installation of Open/LibreOffice.
Turn on edit mode and inspect the form object. On the Event tab you see that the macro is triggered after each record change.
Inspect the button object. It is named "URLButton1". I modified the macro so you can have many such buttons, each ones name starting with "URLButton".
The additional info of the button is "Address" which is the exact field name having the URL.

On your own form you may define a button "URLButton_WWW" with additional info "web" and another button "URLButton_Mail" with additional info "email" where "web" is the name of the field name having some web-URLs to be opened by the "URLButton_WWW" and "email" is the name of the field with email addresses to be opened by your "URLButton_Mail".
-- The form event "After Record Change" triggers the macro which had been extracted to a well defined location in the user profile.
-- Button names start with "URLButton" and each one has an existing field name as additional info.
That's all it takes. The exact same macro can be triggered by any other input form with some "URLButton"(s) having field names as additional infos.

Now you may have http://www.myfriend.org/index.html in the web field and charlie@myfriend.org in the email field. The latter is not a valid URL because the mailto: protocol is missing. No hyperlink without protocol. In this case you simply bind your form to a query like
SELECT *, 'mailto:'||"email" as "email" FROM "Table"
which takes care of the missing mailto:
Attachments
biblio.urlbutton.odt
office document with form bound to Bibliography.biblio and triggering MyMacros.pyDBA.ButtonURL.AfterRecordChange
(12.11 KiB) Downloaded 408 times
URLButton.zip
python/pyDBA/URLButton.py to be extracted in folder Script of the user profile.
(553 Bytes) Downloaded 216 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dreamquartz
Posts: 898
Joined: Mon May 30, 2011 4:02 am

Re: Programming a button on a form

Post by dreamquartz »

    Going to try this out.
    Will take some time though.
    Bear with me.

    Dream
    LO 7.x, HSQLDB 2.7.x & Ubuntu 24.04 LTS.
    Post Reply