LO querry not saving records from form

Depends on what you compare with.

Open tbl_Filter of my https://ask.libreoffice.org/uploads/short-url/pcJkYxh7TT7NLsUmEtCnWQA28be.odb

Record FID=1 filters by text like “2025-01” stored in C1.
In the subform properties, tbl_Filter.C1 is linked to paramYearMonth of query “qry_WXdataLeeds_Filter”
This subform query splits the “2025-01” in two numbers and compares the resulting numbers with the years and months of the ReadDate:

 WHERE
    YEAR( "ReadDate" ) = CAST( LEFT( :paramYearMonth, 4 ) AS SMALLINT ) 
    AND MONTH( "ReadDate" ) = CAST( SUBSTRING ( :paramYearMonth, 6, 2 ) AS SMALLINT )

CAST converts the leftmost 4 digits and the last 2 digits into small integers and compares them with Year(ReadDate) and Month(ReadDate) which are also to integers.
Of course you may also concatenate Year(ReadDate) with 2-digit Month(ReadDate) and compare the resulting strings with each other. That would be:

WHERE 
    Year("ReadDate") || Right('0' || Month("ReadDate"), 2) = :paramYearMonth

We need a parameter query because we can not compare the VARCHAR filter criterion with anything stored in the database table directly. We need to do some kind of conversion in this or that direction, either split a string into 2 numbers or concatenate 2 numbers into one string.

In the next variant, record FID=2 stores 2 integer numbers in INT1 and INT2.
The subform query compares the two numbers with the years and months of ReadDate:

WHERE YEAR( "ReadDate" ) = :paramYear 
AND ( :paramMonth = 0 OR :paramMonth = MONTH( "ReadDate" ) )

The listbox selects distinct year numbers from “ReadDate”. The numeric form control allows for numbers 0 to 12 and. In case of zero, the query returns the entire year’s data.
The first comparison before the AND returns True if Year(ReadDate) equals the paramYear (taken from tbl_filter.D1, entered by means of the listbox).
The second comparison behind the AND returns True either if paramMonth (taken from tbl_Filter.D2 entered by means of a numeric control) is zero or if paramMonth equals Month(ReadDate).
Both comparisons are connected by AND, so a record is included in the filter result if both sides of the AND are True. (year is equal) AND (month is zero or equal)

In the third variant “Leeds Weather2” I store 2 date values in D1 and D2 and compare them directly with “ReadDate”

WHERE ( :paramD1 IS NULL OR "ReadDate" >= :paramD1 ) 
  AND ( :paramD2 IS NULL OR "ReadDate" <= :paramD2 )

Both date controls (and their underlying tbl_filter.D1 and D2) are allowed to be empty. If both are empty, the comparisons on both sides of the AND return True. True AND True is True for all records, therefore the query returns all data unfiltered.
If only paramD1 (taken from tbl_Filter.D1) is empty, the first comparison returns True anyway while the second comparison returns True in case of ReadDate being smaller than or equal to paramD2 connected to tbl_Filter.D2.
If only paramD2 (taken from tbl_Filter.D2) is empty, the first comparison returns True in case of ReadDate being greater than or equal to paramD1 while the second comparison returns True anyway.
If none of the two parameters is empty, both parameters are compared with ReadDate and only those records are matched where D1 >= paramD1 AND D2 <= paramD2.
In this variant we need a parameter query because the two comparisons distinguish between given and missing values and because we compare by operators >= and <=.

Without any parameter query, we can only compare if 2 values are equal to each other:
tbl_Filter.D1 = tblWXdataLeeds.ReadDate
which returns no records if tbl_Filter.D1 is empty or if tbl_Filter.D1 does not occur in tblWXdataLeeds.ReadDate. You have to filter by a directly comparable value that occurs in the filtered table.

Ok, I( have used the same Field Type a FID.

Could you please answer my other 2 questions regarding Leeds Weather 2 form which I have made a copy of. They about 3.2.2 in 1 of your replies and 3 of your 2nd reply .
Thanks

LeedsWXData_3.odb (1013.2 KB)
Form “Leeds Weather2_New_Record” with screenshot of subform properties, OK and Cancel buttons. OK is triggered by the Enter key (property “Default button”=Yes). It jumps to the next new record saving the just edited one.
You may reset “Add data only” to “No” in order to get all data at the preset filter date. In its current state, the subform loads (without existing records) to the new record having the default date inherited from the filter table.
I had to remove some unrelated forms in order to get the file small enough for uploading to this site.

Thanks again for the help, but I don’t have the same Link fields in the form properties /Data as you show me in the latest odb file.
So I can’t or don’t know how to link D1 to the filtered ReadDate.

menu:Form>“Form Navigator”
The root of the forms hierarchy is the forms collection on top.
The filtering form is part of the forms.
In your original form, uploaded on 22 January, the filtered form was also part of the root collection. Make that form a subform by dragging its icon onto the icon of the filtering form. Being a subform, it shows the master/slave links.
The ellipsis buttons open a helper dialog which allows you to map columns side by side. However this dialog does not list any query parameters. You have to enter any parameter names manually, without the leading colon which marks them as parameters in the context of the query.
The drop down button allows to edit the list of fields, one field name (or parameter name) below the other when using many links. The sequence of filtering field needs to match the corresponding sequence of filtered fields.

In Form Navigator, I have:
Forms
frmFilter
List Box 1
Label Field 1
Main Form
SubForm

So what do I drag from where to where. Do you mean Forms to frmFilter, because it doesn’t work

Thanks

7u3fkU9S96
Initially both items, “frmFilter” and “MainForm”, are directly below “Forms”.
Select “MainForm” and drag it onto “frmFilter”.
Now the “MainForm” is subform of “frmFilter”.
You may change the misleading name “MainForm”, but the name does not really matter.

Thanks, but when I click on MainForm to drag it up to frmFilter, it doen’t stay there , and goes back to where it was.

Oops, the MainForm is now under frmFilter. It seemed not to happen straight away??
Ok so now what is the next stage I have to do

I’ve done this for years with all versions of LibreOffice. :man_shrugging:
Right-click “frmFilter” in the form navigator and choose “New”.
Bind it to the right table.
Drag all the form controls from “MainForm” into the new subform.
Delete “MainForm”

Ok but after I dragged all the form controls to the new form, does that include all the form control from or under the SubForm etc.

Yes, including any subforms or “MainForm”. Drag the subform and all its controls will follow.
In the navigator you can also perform a from … to selections. Click the firlst element, hold the Shift key and click the last element. Then drag the selection. Ctrl+Click selects many single elements.

I have moved all the form controls including the subform from MainForm to the new Form.
After I set the new form to use a table and add data etc, when I open the new form and choose the month and year from the combo box, it does not load that data. Instead it loads the data from the 1st entry in the table, and I have to use the navigation buttons to get to this month and date.
Also,I have to now enter a parameter before any data will load, but not the data I have entered the year and month for. As before it just goes to the first entry in the table.

I find selecting a year and month from the combo box and pressing OK does not do anything apart from going back to the 1st entry in the table.
Is there anything that can be done to make it work.

Thanks

The month is a string, 4 digits a dash and 2 digits stored in a VARCHAR column in the distinct row of your filter table.
The string needs to be compared with another string which is not stored in the subform’s table. The comparable string needs to be calculated from ReadDate.
Make this the source of your subform:

SELECT "tblWXdataLeeds".*, Year("ReadDate") || '-' || Right('0' || Month("ReadDate"), 2) AS "Month"
FROM "tblWXdataLeeds"

This simply selects everything from “tblWXdataLeeds” plus the calculated month string under column name “Month”.
Source type = SQL
Source = above statement
or
Source type = Query
Source = name of a query made from above statement.

Master = YearMonth (name of fiter table column)
Slave = Month (name of the calculated string)

Now the subform is filtered by the month string, BUT there is no preset default value in any column because that month string is not stored anywhere in the table.
If you want to work with a preset date value for new record, you better create an additional form for new records and connect a date column as master with the ReadDate as slave. The master date will appear as default value of column ReadDate in every new record.

When you say:
Make this the source of your subform:
Where exactly, is that in form properties or the combo box properties ,Or is in control properties of the combo box.

Thanks

Variant #1:
Content type: SQL (sorry, I wrote “Source” instead of “Content”)
Paste my query into the “Content” box.

Variant #2:
Open or create a new query in SQL view, paste my query, save the query.
Content type: Query
Content:: query name

Link master: YearMonth (column name in the filtering table)
Link slave: Month (name of the calculated Month string in my query)

I just tried Variant#1, but I am getting an error, well 4 actually, about connectivity.

I then tried Variant#2 and now no error but it is just the same as it was 2h ago, when I have to enter a parameter etc etc.
The combo box does not show anything now.

Pardon me for saying so, but it is not as ‘smooth’ as I hoped it would be but I do appreciate your help and time.

Out of interest , is this how it is supposed to work, as I’ve explained.

thanks

Regarding to extra qry filters in Leedsweathe2, I tried them just and none of the actually work or return any data. There was an error with all of them regarding connectivity

LeedsWXData_4.odb (887.0 KB)

Thanks, but again I don’t have the same version of LO as you do because the filter fields in your form properties are not in mine.

Out of interest, what version of LO or any other office program are you running on linux

Thanks

All my “LeedsWeather” forms work well with OpenOffice which is a 12-year-old predecessor of LibreOffice. They work with LibreOffice 24.2 and with 25.2 (beta). Therefore I assume, they work with any version of LibreOffice. I use similar forms since 15 years without any maintainance or fixing.

1 Like