Topic: Autoselect default combobox value on New Record

Hi guys,

a new challenge: I am looking for a way to bring automatically a chosen value in a combobox on "New Record" action in a simple way.

e.g:

I want add.combobox1 to select id=3 after click on form1.Add button

Any idea?

Re: Autoselect default combobox value on New Record

I managed to select default value but not display it by default:

 // Set default Status to In Stock on new Entry
 procedure Form1_Button1_OnClick (Sender: string; id: integer);
begin
    Data_edition.Combo_VodafNrSttus.dbSQLExecute('select distinct st.status, st.id from status st where st.id=3');
    Data_edition.ComboSttus.dbSQLExecute('select distinct st1.status, st1.id from status st1 where st1.id=3');
end;

Re: Autoselect default combobox value on New Record

ComboBox have property:
DefaultIndex - Number of entries (not id) in the list selected by default. If 0, the default entry is not selected.

Dmitry.

4 (edited by tcoton 2015-12-03 17:44:53)

Re: Autoselect default combobox value on New Record

Cool, it does the trick. I set DefaultIndex=1 and it works. Thanks Dmitry.

However, I use the same form for both edit and add, is it possible to script this property to match one action?

Re: Autoselect default combobox value on New Record

DefaultIndex works when you create new record.

Dmitry.

6 (edited by tcoton 2015-12-03 20:13:47)

Re: Autoselect default combobox value on New Record

yes, it works but it selects also a default value when editing as I am using same form for both... and I do not want to select a default value when showing the form for edition and I want to keep the number of form as minimum as possible. I may have to re-think the design but well, too many forms is confusing and does not look nice.

Re: Autoselect default combobox value on New Record

DefaultIndex has no effect when a record is edited.

Dmitry.

Re: Autoselect default combobox value on New Record

The way I am handling it, there was a visual issue. I tricked it by using a null value for first record in status table tongue

Everything is Ok now, thanks a lot.

Re: Autoselect default combobox value on New Record

Working code for the fans:

// procedure to display available numbers only when deployed or current one by default
// SIM
procedure PopulateSIM_ComboBox(ComboBox: TdbComboBox; ShowNotUsed: boolean);
begin
    if ShowNotUsed then
    ComboBox.dbSQLExecute('SELECT SIMNo,'+
        'id FROM SIM GROUP BY SIMNo '+
        'HAVING (SELECT COUNT(*) FROM Users WHERE Users.id_SIM=SIM.id) = 0')
    else
    ComboBox.dbSQLExecute('SELECT SIMNo, id FROM SIM');
end;

// Auto-update id_status on change if original is "In Stock"
procedure Details_ComboSimNr_OnChange (Sender: string);
begin
     SIMsstus:= SQLExecute('select id_status from SIM where SIM.id=' +sim);
     if SIMsstus = '3'
     then SQLExecute('update SIM set id_status=2 where SIM.id=' +sim);
end;


 // Set default Status to In Stock on new Entry
 procedure Form1_Button1_OnClick (Sender: string; id: integer);
begin
    Data_edition.Combo_VodafNrSttus.dbSQLExecute('select distinct st.status, st.id from status st where st.id=3');
    Data_edition.ComboSttus.dbSQLExecute('select distinct st1.status, st1.id from status st1 where st1.id=3');
end;

// Clear comboboxes when leaving Data Edition
procedure Data_Edition_OnClose (Sender: string; Action: string);
begin
    PopulateSIM_ComboBox(Data_Edition.Combo_VodafNrSttus, False);
    PopulateSIM_ComboBox(Data_Edition.ComboSttus, False);
end;

10 (edited by tcoton 2015-12-07 13:24:30)

Re: Autoselect default combobox value on New Record

Actually the code above nearly works. I can edit the first time with proper status but the second time I open the form to edit, I cannot choose a status, I get a phone number list instead... How do you clear properly a combobox on form closure?

http://myvisualdatabase.com/forum/misc.php?action=pun_attachment&item=1524

Post's attachments

Attachment icon combobox_issue.png 69.88 kb, 299 downloads since 2015-12-07 

Re: Autoselect default combobox value on New Record

Here is how I display the record which works first time but is messed-up on second visit

http://myvisualdatabase.com/forum/misc.php?action=pun_attachment&item=1525

Post's attachments

Attachment icon combobox_issue_2.png 87.31 kb, 278 downloads since 2015-12-07 

Re: Autoselect default combobox value on New Record

I could find where the problem is, it is about the procedure I use to populate the combobox when using the very same form to add a record which I did not fully understand. I changed it with another query on the Else part and it is working as expected but I would like to learn how to properly use such a procedure to not select the wrong information in the first part of it.


procedure PopulateSIM_ComboBox(ComboBox: TdbComboBox; ShowNotUsed: boolean);