[Solved] Macro to update form field

Discuss the database features
Post Reply
joaofmateus
Posts: 33
Joined: Fri Mar 20, 2009 12:15 pm
Location: Lisbon - Portugal

[Solved] Macro to update form field

Post by joaofmateus »

Good evening everyone
I urgently need to build a macro that fetches the maximum number from one table and adds one more to it, and with the number obtained, it updates a certain field of the forms.
The table is the "tMOVIMENTOS"
The forms are the "fMOVIMENTOS"
The forms field is "ID_MOV" (the field must be blank for it to be updated)

Could someone help me?
Last edited by RoryOF on Wed Dec 27, 2017 10:00 pm, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
OOo 4.1.5 on MS Windows 10
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

Re: Macro to update form field

Post by Nocton »

Some more information would be helpful and you may not need a macro.
When you say:
- "that fetches the maximum number from one table" is the maximum number a particular number in a field, the maximum number in a field list or is it the (maximum) number of records in the table?
- "it updates a certain field of the forms." and "The forms field is "ID_MOV" (the field must be blank for it to be updated)" then what is the name of the table containing ID_MOV that the form is connected to?

Also, why must ID_MOV be blank for it to be updated? Do you mean that you only want it updated once?
OpenOffice 4.1.12 on Windows 10
joaofmateus
Posts: 33
Joined: Fri Mar 20, 2009 12:15 pm
Location: Lisbon - Portugal

Re: Macro to update form field

Post by joaofmateus »

Hello Nocto
After Christmas parties, I'm back.I'll try to explain what's going on:
1. For losing data I decided to work with HDBSQL as back end and Base as front end.
2. When importing the tables the "ID_MOV" table of the table "tMOVIMENTOS", which was self-incremented, is no longer, it will soon have to be introduced at the time of the launch, which is done through the form "fMOVIMENTOS"
3. Only. I want this to be done in new records, since the IDs of the existing records should never be changed, so I was hoping that, whenever the "fMOVIMENTOS" form (through which the "tMOVIMENTOS" table is maintained) was changed and the field "ID_MOV" was blank was updated with the last "ID_MOV" of the table "tMOVIMENTOS" + 1. I wanted this to be done through macro.
Did I make myself understood this time?
Last edited by joaofmateus on Tue Dec 26, 2017 11:54 pm, edited 1 time in total.
OOo 4.1.5 on MS Windows 10
F3K Total
Volunteer
Posts: 1039
Joined: Fri Dec 16, 2011 8:20 pm

Re: Macro to update form field

Post by F3K Total »

Hi,
joaofmateus wrote:When importing the tables the "ID_MOV" table of the table "tMOVIMENTOS", which was self-incremented, is no longer ...
That's not good, but can be fixed by executing the following SQL-commands via Tools/SQL ...:

Code: Select all

ALTER TABLE "tMOVIMENTOS" ALTER COLUMN "ID_MOV"  SET NOT NULL;
ALTER TABLE "tMOVIMENTOS" ADD PRIMARY KEY ("ID_MOV");
ALTER TABLE "tMOVIMENTOS" ALTER COLUMN "ID_MOV" INTEGER GENERATED BY DEFAULT AS IDENTITY
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
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

Re: Macro to update form field

Post by Nocton »

Thank you, joaofmateus, for the extra information.
But you still have not said what you mean by "that fetches the maximum number from one table".
OpenOffice 4.1.12 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro to update form field

Post by Villeroy »

Another one who wants to control the creation of id numbers instead of leaving it to the database engine.
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: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Macro to update form field

Post by Villeroy »

If you only want to display which ID is propably the next ID for a new record, then you may implement something like this: download/file.php?id=24806 where a subform queries the record count of some table. Just calculate Max(ID)+1 for your purpose.
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
joaofmateus
Posts: 33
Joined: Fri Mar 20, 2009 12:15 pm
Location: Lisbon - Portugal

Re: Macro to update form field

Post by joaofmateus »

Thank you all for your help.

Following your suggestions, I have already retreated the table field to automatic and worked.
However, because a few years ago I had problems with "ACESS" with a field of this type, I preferred it to continue to be generated at the time of loading the record.

Together an example of what I intended with a RMG macro I did not get it to work.

If anyone can and will try to make it work, thank you in advance.

Here is the example link:
https://drive.google.com/open?id=1bHRBm ... F4GV7vyUzM
OOo 4.1.5 on MS Windows 10
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

Re: Macro to update form field

Post by Nocton »

I now see that you are trying to 'manually' update ID_MOV as the Primary Key for your table. Like Villeroy, I would strongly recommend that you let the database engine do the work. You say you had problems with ACCESS, however I think you are much more likely to get problems doing it this way than when using AutoValue. Over many years and many applications I have never had a problem with AutoValue in Base (or with ACCESS Either).
OpenOffice 4.1.12 on Windows 10
joaofmateus
Posts: 33
Joined: Fri Mar 20, 2009 12:15 pm
Location: Lisbon - Portugal

Re: Macro to update form field

Post by joaofmateus »

Once again grateful to all. All solved and working.
If anyone is interested in seeing how, you can follow this link

https://forum.openoffice.org/es/forum/d ... hp?id=7667
OOo 4.1.5 on MS Windows 10
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

Re: [Solved] Macro to update form field

Post by Nocton »

Pleased that you have achieved what you wanted to do. However, like Villeroy, I still think that the straightforward AutoValue would be best. Your solution allows the creation of empty records with a new ID_MOV but no other data - just keep clicking on the forward/next record button - so you need to fix that problem.
OpenOffice 4.1.12 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Macro to update form field

Post by Villeroy »

Indeed, far too complicated with no benefit.
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