Page 1 of 1

[Solved] Form button to open url saved in a field?

PostPosted: Thu Mar 04, 2010 12:17 pm
by giooo
Hi everybody,

Say I have a table with a field containing web addresses.

In a form I'd like to add a button to open the browser to the web address stored in that record.
I thought of using a button with "open document/web page", but in that case the link is static.
See attached file for example. There's one table and one form.

Is it possible to pass somehow a field value to a button?
Any other strategies?

Maybe it's a trivial problem and I am missing something...
Thanks for any suggestions/help!

giooo

Re: Form button to open url saved in a field?

PostPosted: Thu Mar 04, 2010 3:20 pm
by RPG
Hello

This routine must do what you asked You have to adjust some names.
For information how to connect a macro to an event see your help file

select first the help files.
then in the list box left in top : OpenOffice.org Base
tabpage index and type in : events;assigning scripts

Romke


Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****
' You need two controls in your form(document). They must be in the same form(as in navigator).
' One control is a text control and contains the pathname or url to the file.
' The second control is a button.
' The action is bound to the first event of the button control: Before commenting. ' Name before OOo3.2
' The name is new in OOo3.2 and later : Approve action
' The function can be aborted when the files does not exist.
' You have also change the properties of the button control.
' The action is :Open document/webpage.
' The small macro move the field value from the textbox to the button control url
function openurl(oEv as object)
'
   dim oForm,oTextBox
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname("TextBox")
   if FileExists ( oTextBox.text) then
      oEv.source.model.targeturl=oTextBox.text
      openurl=true ' Do the program when the file exists
      else
      openurl=false ' The file does not exist and do nothing
   end if
   'print  convertfromurl(oTextBox.text)
End function


 Edit: I have edit some bugs. 

Re: Form button to open url saved in a field?

PostPosted: Thu Mar 04, 2010 4:55 pm
by giooo
yup, it works! :-)

thank you very much!


giooo

Re: [Solved] Form button to open url saved in a field?

PostPosted: Tue Feb 11, 2014 9:31 pm
by tx42
For opening a web URL, "FileExists" will usually hang and may crash OO!

:alarm: I abandoned opening web URLs with the macro above because it always hanged. I've since brushed up on my OO basic, and found the problem. It makes no sense to check if the file exists if the file is a web URL. Just check if its an empty string and let the browser do any further checking, etc. Here is a better version for opening web URLs based on the above code:

Code: Select all   Expand viewCollapse view
REM **********************************************************************
REM Open URL from button
REM       https://forum.openoffice.org/en/forum/viewtopic.php?f=39&t=28228
REM **********************************************************************
' You need two controls in your form(document). They must be in the same form(as in navigator).
' One control is a text control and contains the pathname or url to the file.
' The second control is a button.
' The action is bound to the first event of the button control: Before commencing. ' Name before OOo3.2
' The name is new in OOo3.2 and later : Approve action
' The function can be aborted when the files does not exist.
' You have also change the properties of the button control.
' The action is :Open document/webpage.
' The small macro move the field value from the textbox to the button control url
sub openURL(oEv as object, aTextName as String)
   dim oForm, oTextBox
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname(aTextName)
   if (oTextBox.text <> "") then
      oEv.source.model.targeturl = oTextBox.Text
      openurl=true ' Do the programm when the file exists
    else
      openurl=false ' The file does not exist and do nothing
   end if
   'print  convertfromurl(oTextBox.text)
End sub

sub openURLtxtURL(oEV as object)
   openurl(oEv, "txtURL")
End sub


With these two subroutines, you can reuse the openURL subroutine for text boxes of different names by defining different stubs. The above stub called "openURLtxtURL" opens the URL in the text box called "txtURL".

Re: [Solved] Form button to open url saved in a field?

PostPosted: Mon May 15, 2017 1:36 am
by Koa
Thank you! I had some trouble figuring out what you meant by, "The action is :Open document/webpage.", but finally realized that in Control Properties General there was an Action property that I forgot about.

Re: [Solved] Form button to open url saved in a field?

PostPosted: Mon Feb 05, 2018 10:45 pm
by johnh009
I appreciate that this post is 'Solved', but I have a question that is linked that hopefully someone could advise me upon.

While the solution provided works for the original problem (open an URL), I would like the same functionality to work for a file (the file path being passed to the Push Button). I've tried with the given basic code (macro), but get the following message:

Code: Select all   Expand viewCollapse view
"/home/path/name" is not an absolute URL that can be passed to an external application to open it.


Must be some specific code within the macro, because when the path to a file is typed into the URL property of the Push Button, the file is opened.

Re: [Solved] Form button to open url saved in a field?

PostPosted: Mon Feb 05, 2018 11:37 pm
by Villeroy
Last weeks
johnh009 wrote:I appreciate that this post is 'Solved', but I have a question that is linked that hopefully someone could advise me upon.

While the solution provided works for the original problem (open an URL), I would like the same functionality to work for a file (the file path being passed to the Push Button). I've tried with the given basic code (macro), but get the following message:

Code: Select all   Expand viewCollapse view
"/home/path/name" is not an absolute URL that can be passed to an external application to open it.


Must be some specific code within the macro, because when the path to a file is typed into the URL property of the Push Button, the file is opened.

Last week's topic: https://forum.openoffice.org/en/forum/v ... 13&t=92140

Re: [Solved] Form button to open url saved in a field?

PostPosted: Tue Feb 06, 2018 8:27 pm
by johnh009
@Villeroy. Thank you for your prompt response. After following the link you provided and attempting to follow it, it all looked far more complex, with more functionality than I required. So I returned to the original code, had a little read of the Basic manual - the last resort - and discovered the following:

"Since OpenOffice.org is a platform-independent application, it uses URL notation (which is
independent of any operating system), as defined in the Internet Standard RFC 1738 for file
names. Standard file names using this system begin with the prefix file:/// followed by the
local path."

So amending one line of basic,concatenating "file://" to the retrieved pathname string, thus . . .

oEv.source.model.targeturl = "file://"+oTextBox.Text

. . . did the trick. Thanks again.

Having read a little more, the correct way is probably . . .

oEv.source.model.targeturl = ConvertToUrl(oTextBox.Text)

Re: [Solved] Form button to open url saved in a field?

PostPosted: Tue Feb 06, 2018 10:05 pm
by Villeroy
Today I got an inspiration for a slightly more simple version of that macro. I wrapped it into another example database together with an installer (a macro that installs another macro). https://forum.openoffice.org/en/forum/v ... 35#p438435
If you just have one or more columns of fully qualified system paths then you can simply install my new Python macro with one click, draw some button on your own form, change 2 button properties and assign a macro to the "Approve Action" event to "My Macros" > pyDBA > URLButton > FileButton_Approve (this variant includes the conversion from system path to file-URL).
You don't need to program anything at all. Just add the name of the column to the button's "additional info" property.
The new macro does not rely on naming conventions and it does not use form events. Just tell your button about the column name and which macro to call.