Topic: Display combobox list depending on constraint

Hi,

I am a bit stuck with a script I want to implement to limit the list displayed in a combobox based on a checkbox in another field.

What I managed to do is actually to limit the content of the limit based on the status of the checkbox but, where I struggle is to display the current value and normal list if the checkbox is not checked.

Let's say I have a combobox pointing to 6 status. Normally this combobox displays the current status in database and if I drop it, I see all other status.

Now, I am automating a process where if I check a checkbox in a time picker, this status automatically displays status 4 and droplist is only status 4.

Problem is when I uncheck this box, I want to reset the combobox to where it should be with current status and all other status.

Current script looks like this:

procedure Admin_ReturnDate_OnChange (Sender: TObject);
begin
    if Admin.ReturnDate.Checked= True then
    begin
       Admin.StatusList.dbFilter:= 'id=4';
       Admin.StatusList.dbItemID:= 4;
       Admin.StatusList.dbUpdate; // This works perfectly
end
else
       Admin.StatusList.dbFilter:= 'id'; // This clears the combobox but list all possible status
       Admin.StatusList.dbUpdate;
end;

2 (edited by sparrow 2022-08-09 16:55:30)

Re: Display combobox list depending on constraint

Hi,


Unfortunately, it is not clear how to help you.
Attach an example.


such a filter/condition is wrong.
Admin.StatusList.dbFilter:= 'id';

Correctly if clear filter.
Admin.StatusList.dbFilter:= '';

Re: Display combobox list depending on constraint

I agree, it is not easy to understand, I am attaching an example of what I want to do with a pdf in the zip file.

Post's attachments

Attachment icon combo-constraint.zip 1.25 mb, 167 downloads since 2022-08-09 

Re: Display combobox list depending on constraint

procedure Admin_ReturnDate_OnChange (Sender: TObject);
begin
  if Admin.ReturnDate.checked then
    begin
       Admin.StatusList.dbFilter:= 'id=4';
       Admin.StatusList.dbItemID:= 4;
       Admin.StatusList.dbUpdate;
    end else
    begin
       Admin.StatusList.dbFilter:= '';
       Admin.StatusList.dbItemID:= -1;
       Admin.StatusList.dbUpdate;
    end;
end;

Re: Display combobox list depending on constraint

your example

Post's attachments

Attachment icon combo-const1.zip 334.86 kb, 151 downloads since 2022-08-09 

6 (edited by tcoton 2022-08-09 23:46:41)

Re: Display combobox list depending on constraint

Thanks it is partially fixed, there is still this annoying empty status if you check a return date and then uncheck it straight away. Funny how it is easy to instantly change the status when choosing a date but difficult to revert when unchecking it before saving.

The only solution I found was to enter a sql query after the "else" to get the current id but I wonder if it could be done via an object property since, it resets when closing the form.

Re: Display combobox list depending on constraint

Your example

Post's attachments

Attachment icon combo-const2.zip 334.8 kb, 152 downloads since 2022-08-10 

Re: Display combobox list depending on constraint

Hi TCoton, Sparrow,
I was having a quick look at your post and there are a couple of ways of achieving it, one of which Sparrow has detailed.
But I am confused by the .pdf in your attachment.  In it, you write

Question 3 is: Is there a way to disable the date picker only when the status is already 'To be restaged' with a date in record prior to today? The date picker should unlock with status id = 6.

However, aren't you already limiting the choice of statuses available when you initially check the 'returndate'?  And the limited choice of statuses DOES NOT include id 6.
So my question is, how does the 'returndate' ever get re-enabled if status id = 6 is not available - or am I misunderstanding your question?
Derek.

Re: Display combobox list depending on constraint

Sparrow, you are a chief!! That is so much more elegant than writing a SQL query and much more effective!!

I fixed question 3, I just forgot to put a "begin-end;" after the else... stupid me!
The reasoning behind the request is to force a chain of events before giving the opportunity of choosing a new return date, a computer being in status "To be restaged" must be restaged, then delivered. In my full app, I do have a delivery date field and all actions are logged for full tracking.

The whole purpose of the script is to avoid data entry errors as much as possible

Final script looks like this, I am going to fine tune it but this one works as expected!

var
  status: integer;

procedure Admin_OnClose (Sender: TObject; Action: string);
begin
  Admin.StatusList.dbFilter:= '';
  Admin.StatusList.dbUpdate;
end;

procedure Admin_OnShow (Sender: TObject; Action: string);
begin
  status := Admin.StatusList.dbItemID;

     if Admin.StatusList.sqlValue='4' then
       begin
       Admin.StatusList.dbFilter:= 'id<=4'; // locks status list to only 3 choices
       Admin.StatusList.dbUpdate;
       Admin.ReturnDate.Enabled:= False;  // disable date picker
       end
       else
       begin
       if Admin.StatusList.sqlValue='6' then
       Admin.ReturnDate.Enabled:= True; // enable date picker
       end;
end;

procedure Admin_ReturnDate_OnChange (Sender: TObject);
begin
  if Admin.ReturnDate.checked then
    begin
       Admin.StatusList.dbFilter:= 'id=4';
       Admin.StatusList.dbUpdate;
       Admin.StatusList.dbItemID:= 4;
    end else
    begin
       Admin.StatusList.dbFilter:= '';
       Admin.StatusList.dbUpdate;
       Admin.StatusList.dbItemID:= status;
    end;
end;


begin

end.
Post's attachments

Attachment icon combo-const3.zip 337.68 kb, 195 downloads since 2022-08-10