|
Adding a Button To Open Another Form
From NeoWiki
Revision as of 17:52, 25 April 2008 (edit) Lorinda (Talk | contribs) (→Refreshing the Combo Box Contents - continue working on this section) ← Previous diff |
Revision as of 18:55, 25 April 2008 (edit) (undo) Lorinda (Talk | contribs) (→External Links - add link) Next diff → |
||
Line 119: | Line 119: | ||
==External Links== | ==External Links== | ||
- | [http://www.oooforum.org/forum/viewtopic.phtml?t=37999&postdays=0&postorder=asc&start=0 OOo Forums thread on | + | [http://www.oooforum.org/forum/viewtopic.phtml?t=37999&postdays=0&postorder=asc&start=0 OOo Forums thread on macros needed for button]<br> |
+ | [http://user.services.openoffice.org/en/forum/viewtopic.php?f=13&t=3619&p=23822#p23822 OOCF Forum thread on macro needed to refresh form] | ||
{{botlangbarEN|[[Fr:Ajouter un Bouton pour Ouvrir un Autre Formulaire |Français]]}} | {{botlangbarEN|[[Fr:Ajouter un Bouton pour Ouvrir un Autre Formulaire |Français]]}} |
Revision as of 18:55, 25 April 2008
If you attempt to add a new field trip involving a location not already in the Locations table, you will encounter an error message. New locations must be added before the field trip data is entered. Because the Field Trips data entry form will be used most often, and because it is likely that users will forget to enter new locations first, we can make the user's experience easier by adding a button to the Field Trips Data Entry form that will open the Locations Data Entry form. This article explains how to do that.
Contents |
Create The Necessary Macros
Note: The macros below were found in This thread of the oooforums.org Base forum. The only changes involve changing the form names.
- In the main database document window, click on Forms in the left hand column.
- Control-click or right-click on the Field Trips Data Entry form and choose Edit from the contextual menu. The form will open.
- Under the Tools menu, choose Macros, then Organize Macros and finally NeoOffice Basic
- Click the Organizer button
- Click on the flippy triangle to the left of the document icon named Field Trips Data Entry
- Click on the folder icon named Standard that appears
- Click the New button; the New Module window appears.
- Give the new module a name. I chose "ButtonMacros" (without the quotes)
- Click OK
- A new window will open, titled Field Trips Data Entry.Standard - Basic. You will see the following lines:
- Sub Main
- End Sub
- Click in the blank space after the "End Sub" line
- Enter the following code:
- REM Generic macros needed to open any form 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 REM Macro to open specific form called 'Locations Data Entry'. One is needed for each form you wish to open from another form. sub OpenForm_Locations_Data_Entry( oev as variant ) sFormName = "Locations Data Entry" OpenForm( getFormsTC, getConnectionTC, sFormName ) end sub
- --> Note: code under REM Generic macros needed to open any form only needs to be entered once in the Module. If you add buttons to open other forms later, all you need to do is copy the code under REM Macro to open specific form and change the names in the first two lines to reflect the form you want opened. In the first line (that begins sub OpenForm_), you cannot use spaces in naming the macro; you must use underscores instead.
- Click on the Save button on the toolbar (it looks like an external "floppy" drive)
- Close the window by clicking on the red circle in the upper left hand corner.
--> Note: This tutorial has you save this macro in the document. It is possible to save it to a module in "My Macros." But if you copy or move the document to another computer (or another user account), the form will be unable to find the macro that way. Saving the macro in the document insures that it will always be available to the form.
Add the Button
- With the Field Trips Data Entry form still open, make sure that the form controls toolbar is visible. If it is not, go to the View menu and choose Toolbars and then Form Controls.
- Click on the Push Button icon on the Form Controls Toolbar.
- Move the mouse to the spot on the form where you want to add the button. I choose to place it near the "Location" label.
- Click and drag to create the button.
- Double-click the button to open the properties window.
- Under the General tab, make the following changes:
- Give the button a descriptive Name. I called it "Open Location Form"
- Give the button a Label this will appear on the button. I entered "New Location" (without the quotes)
- Set Tabstop to "No." This way the button will not be part of the tab order. (leave it at Yes if you want it to be part of the tab order)
- Near the bottom, enter some descriptive Help text. This will appear as a "tool tip" when you mouse over the button, unless you have such tips turned off.
- In the Events tab, find the line that says Mouse button pressed.... Click the ... button that is to the right of the field box in that same line. The Assign Actions window opens.
- Make sure that Mouse button pressed line is selected. If not, select it.
- Click the Macro...
- In the Macro Selector window, click on the flippy triangle next to the Field Trips Data Entry document icon.
- Click on the flippy triangle next to the Standard folder icon
- Click on the Button macros folder icon
- In the Macro Name column, select the macro named Open_Form_Locations_Data_Entry
- Click OK
- Close the Assign Action by clicking the OK
- Close the Properties:Button window
- Click on the Design Mode On/Off button (the blue drafting triangle)
- Click on the newly made button, and verify that the Locations Data Entry form opens.
Refreshing the Combo Box Contents
At this point, the button on the Field Trips Data Entry form will open the Locations Data Entry form, and the user can then enter the data for the new location. But if you return to the Field Trips Data Entry form, the new location will not show up in the Combo Box. There are at least two ways to approach this. For our purposes, we will add a macro to the Locations Data Entry form to force the refresh of the Combo Box on the Field Trips Data Entry form after a new record has been entered in the Locations Data Entry form.
- First we must make a note of the name of the form and the combo box control that must be refreshed.
- Open the Field Trips Data Entry form for editing (Right click or control click on its icon and choose Edit)
- Right-click or control click on the Locations drop-down control and select Form... from the contextual menu.
- Make a note of the name entered in the Name field of the General tab of the Form Properties window and then close the window.
- Right-click or control on the Locations drop-cown control again and this time select Control... from the contextual menu.
- Make a note of the name entered in the Name field of the General tab of the Properties:Combo Box window and then close the window.
- Note you can also check the names of the form and controls by activating the Form Navigator.
- Open the Locations Data Entry for editing. (Right click or control click on its icon and choose Edit)
- Under the Tools menu, choose Macros then Organize and finally NeoOffice Basic...
- Select the Library and Module where you want the macro to be stored. See Using Macros article for more information on how to do this. Click the Edit
- If you store the macro in the Form, the macro will be transfered if you move the database file to another computer, but everytime you open the form, you will receive a warning about the document containing macros. If you will be using the file on only one computer, you can avoid this warning message by saving the macro in one of the libraries in the My Macros area. It is reccommended that you not save macros to the Standard library, as that library is easily overwritten. The Using Macros article includes information on creating new libraries and modules.
- Click after the words End Sub and hit Return a couple of times
- Paste in the following code, replacing any instaces of FieldTripsInfo with the name of your form and any instances of Location<tt> with the name of your combo box. Make sure to retain the double quotes around the form and control names
- Sub Refresh_Field_Trips_Data_Entry_Form Dim tmp as Object tmp = thisComponent.Parent.FormDocuments if tmp.HasByName("FieldTripsInfo") then tmp = tmp.getByName("FieldTripsInfo") if not IsNull(tmp.Component) then tmp = tmp.Component.DrawPage.Forms.getByName("FieldTripsInfo") if tmp.HasByName("Location") then tmp.getByName("Location").refresh endif endif endif End Sub
- Click the save icon on the Toolbar
- Close the Macro window
- Right-click or control-click on any form control in the Locations Data Entry form and choose Form... from the contextual mene.
- Click on the Events tab
- Click on the ... button to the right of the After record change field
- In the Assign Action window, make sure the After record change is still selected and click on the Macro
- Use the disclosure triangles to drill down through the libraries and modules in the Library column until you reach the module where the new macro is stored.
- In the Macro name column, select the name of the new macro (Refresh_Field_Trips_Data_Entry_Form)
- Click OK
- Close the Form Properties
- Test the macro by adding a new field trip to the Field Trips Data Entry form and using the New Location button to enter a new location.
External Links
OOo Forums thread on macros needed for button
OOCF Forum thread on macro needed to refresh form