[Solved] Data dropdown list fails to save entry linked to foreign key

Discuss the database features
Post Reply
Ale_110
Posts: 3
Joined: Wed Dec 20, 2023 1:19 pm

[Solved] Data dropdown list fails to save entry linked to foreign key

Post by Ale_110 »

Hi there,
I have been experiencing troubles with a silly problem but I cannot get it fixed.
I would simply like to use a data dropdown function to select a value that is linked to another table. That is,
I have EVENT table that has contact_id label that is link to CONTACT table. I have get contact_id (the primary key lable of the CONTACT Table) to show me the value linked to the primary key which is first_and_last_name. Now whenever I select the value that is shown as a first_and_last_name and try to save the record the value gets deleted in the form and resets to empty. If i changed the table directly with the CONTACT_ID numbers linked to the other table it gets saved. If I also change the control configuration to view the CONTACT_ID to show me the actual IDs of the referenced table and select any number in the form, the entry does not get registered and the data validation box resets to empty once again. Below are the configuration of the data field:
Capture2.PNG
Capture2.PNG (30.47 KiB) Viewed 4421 times
Capture.PNG
Capture.PNG (35.17 KiB) Viewed 4421 times
the sql in list content is: SELECT "first_and_last_name", "first_and_last_name" FROM CONTACT
Last edited by Ale_110 on Sun Jan 14, 2024 7:41 pm, edited 1 time in total.
OpenOffice 4.1.14 ,windows 10
FJCC
Moderator
Posts: 9457
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Data dropdown list fails to save entry linked to foreign key

Post by FJCC »

The query for your list box should be

Code: Select all

SELECT "first_and_last_name", "CONTACT_ID" FROM CONTACT
with the Bound Field on the Data tab of the control dialog set to 1. That will display the first_and_last_name field but store the CONTACT_ID.
This is explained with a little more detail in the Getting Started Guides's chapter on Base . Look for the section Adding a List Box that is well down the page.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31323
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Data dropdown list fails to save entry linked to foreign key

Post by Villeroy »

You may use this demo as a reference for one-to-many and many-to-many relations. In this demo, each person has one animal.
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
Nick N,
Banned
Posts: 140
Joined: Wed Aug 09, 2023 2:10 pm

Re: Data dropdown list fails to save entry linked to foreign key

Post by Nick N, »

Hello Alex,

Could you please take a look at following form?

Please make sure PK and FK cascade option is ON. If you choose the combox, your cell will display only the ID value.
Better to select listbox because you may display the VARCHAR.

Please find enclosed a test ODB file for yr reference.

Regards

Nick
Attachments
clip.jpg
clip.jpg (34.27 KiB) Viewed 4294 times
Testify.odb
(12.21 KiB) Downloaded 507 times
Libre Office 6.0.7 on Ubuntu 18.04
Ale_110
Posts: 3
Joined: Wed Dec 20, 2023 1:19 pm

Re: Data dropdown list fails to save entry linked to foreign key

Post by Ale_110 »

Hi there,
Thank you very much for the guides, tutorials and test files, I will take a deeper look whenever I have time. I think that the solution that worked for me was to first of all set the variable as text [varchar] rather than integer (which as far as I remember was the problem) and change the following SQL code : SELECT "first_and_last_name", "first_and_last_name" FROM CONTACT ORDER BY "first_and_last_name". I believer that main problem was the variable type which, coupled with the SQL code, prevented the entry to be saved. Anyway, so far so good, I will dig deeper in order to tune it better but it is all good now! Thanks for the support.
OpenOffice 4.1.14 ,windows 10
Ale_110
Posts: 3
Joined: Wed Dec 20, 2023 1:19 pm

Re: [Solved] Data dropdown list fails to save entry linked to foreign key

Post by Ale_110 »

Thank you Villeroy and Nick for the database shared, I checked them and quite understand the difference in putting ID compared to directly inserting the name as I did. This explains the code inserted in both databases, in relation2listbox.odb: "SELECT "Name", "ID" FROM "Animals" ORDER BY "Name" ASC"
and in testify.odb this: "SELECT "client_name", "ID_client" FROM "Clients"".

Cheers,
Ale
OpenOffice 4.1.14 ,windows 10
Post Reply