[Solved] Initialize form's ID field to MAX+1

Discuss the database features
Post Reply
mohanvarier
Posts: 4
Joined: Tue Sep 22, 2020 3:56 pm

[Solved] Initialize form's ID field to MAX+1

Post by mohanvarier »

Dear all
I am a beginner in open office data base. My requirement is...

While loading a form the default value of a number field in that form should be +1 of the last record of the corresponding table.
I was trying to get using a macro...the algorithm of my macro is as said below.

While loading form should display last record.
Then the number field value should take to a variable.
increment the variable.
form will jump to new record.
assign the number field to the value of the variable..

My macro codes are shown below.

Code: Select all

REM  *****  BASIC  *****

Sub Main

End Sub


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

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:LastRecord", "", 0, Array())
oForm = ThisComponent.CurrentController.Model.DrawPage.Forms
oControl = oForm.getByName("MainForm").getByName("fmtOPNumber")
iopnum = oControl.getCurrentValue()
iopnum = iopnum+1

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:NewRecord", "", 0, Array())
oControl.updateInteger(iopnum)

end sub
The macro is not giving the required result...
kindly help me
Last edited by mohanvarier on Mon Sep 28, 2020 7:26 am, edited 3 times in total.
OpenOffice 4.1.7 Windows 7 Ultimate
F3K Total
Volunteer
Posts: 1038
Joined: Fri Dec 16, 2011 8:20 pm

Re: default value of a number field in a form should be +1 t

Post by F3K Total »

Hello,
if you use an integer column as AutoValue, the database does this job for you for every new record.
AV.png
Witch record you see on loading can be adjusted by sorting the form correctly.
R
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 10 AOO, LOLinux Mint AOO, LO
mohanvarier
Posts: 4
Joined: Tue Sep 22, 2020 3:56 pm

Re: Initialize form's ID field to MAX+1

Post by mohanvarier »

sir,

Thank you for your response....
May be because of my language you was mislead-ed.....
1. My requirement is not in the ID field
2. This is most important.....I want an entry option also in that field, ie default value should be MAX+1 at the same time an entry option for any value also should be there, that is why i used the word "default".
The mentioned solution will not give entry option in that field..

Sorry for my poor language...which mis guided you.

waiting for you help
thanks
OpenOffice 4.1.7 Windows 7 Ultimate
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Initialize form's ID field to MAX+1

Post by Villeroy »

Which type of database? Embedded HSQL?
In case of a database server: with multi-user access?
Is that incremented integer referenced by some other field?
In most cases this kind of requirement indicates bad database design.
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
UnklDonald418
Volunteer
Posts: 1548
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Initialize form's ID field to MAX+1

Post by UnklDonald418 »

See if this example helps with what you are attempting.
Demo51_AutoIncrementNonKeyField.odb
(13.66 KiB) Downloaded 162 times
If you are going to be programming macros you need to install an object inspection tool.
[Tutorial] Introduction into object inspection with MRI
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Initialize form's ID field to MAX+1

Post by Villeroy »

I prefer the event driven approach, aka "Hollywood programming" ("Don't call us! We call you!").

1. Install my Python macro from viewtopic.php?f=21&t=88831. Just open the attached document and push the macro button to install the Python macro to the right place.
2. Now you can test my input form in the here attached database document.

In order to find out how it works:
1. open the form in design mode
2. get the forms navigator from toolbar "Form Design". The navigator shows the forms collection with 2 independent logical forms.

-- The form "NewOPNum" just queries the max+1 value from the OPNum and displays this value in the green control. You may delete that green control if you like. No macro code makes use of the displayed form control.

-- The MainForm has an additional hidden control "AutoRefresh". Hidden controls store configuration values for macros. This one stores the value ../NewOPNum.
../ refers to this form's parent item (the forms container) which has a refreshable form "NewOPNum".

-- The main form's event "after record action" calls the Python macro you have installed whenever this form updates, inserts or deletes any record. The Python macro refreshes all items that are listed in the hidden control "AutoRefresh" (multiple items would be semicolon separated).

-- The main form's event "before record action" calls a macro that is embedded in the database document (Standard.Module2.before_RecordAction). It reads the integer value from colun #1 of form "NewOPNum" and writes it to column #3 of the calling form when a new record is inserted.

One macro takes care of the correct max+1 value whenever something may have been changed in the MainForm (after record action). The other macro inserts the max+1 value whenever a new record is going to be inserted into the MainForm (before record action).

P.S. the embedded macro might fail when you start with an empty table. I did not test this special case.
P.P.S you get the calling source and additional elements of any event if you bind the event to macro MriLib.Module1.mri of your installed MRI object inspector. Whenever the event is triggered, a new MRI window pops up from which you can dive into the object hierarchy.
Attachments
Demo51_AutoIncrementNonKeyField_v2.odb
(16.47 KiB) Downloaded 164 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
Post Reply