value set

need an expert solution for a typical problem as below;

In my form, i have a combo and an edit box. Combo has values as "YES" and "NO". If yes is selected then input into editbox is mandatory, and the text entered in the editbox shall be saved to database. Combo box values shall not be stored into database. Hence when the form reopens after saving the data, it shall query database for the values as entered into  editbox, the  query shall take other input parameters which are out of scope for this question.

If the record is found the editbox shall be populated with the value and combo box text shall  be set to YES.

But combo box can take NO and also it can be left blank , hence how to set the text for combo as per user input when the form reloads after saving whether it is no or blank?
AbhiJeetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
Check if "some" value is set and set index of combobox. Best on AfterScroll Event.

procedure TForm1.ADOQuery1AfterScroll(DataSet: TDataSet);
begin
  if DataSet.FieldByName('somefield').IsNull then
    ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('Yes')
  else
    ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('No');
end;

Open in new window

0
AbhiJeetAuthor Commented:
I need to set YES, NO or blank as per user input. Setting yes is not a problem as i mentioned that depends whether the edit box has value or not.

How to set No or blank as per user input.
0
AbhiJeetAuthor Commented:
is it possible to store user input for the combo into a disconected client dataset and retrieve later?
how to implement?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Sinisa VukSoftware architectCommented:
You did not get it. When you click save/post - check value of combo, if is yes then save value of edit to db, if is no then clear value in db.

procedure ....SaveToDB();
begin
  table1.Edit; //or insert 
  if ComboBox1.Text = 'YES' then
      table1.FieldByName('UserField1').AsString := Edit1.Text
  else
      table1.FieldByName('UserField1').Clear;
  table1.Post;
end;

Open in new window


... od form load open table/query ... If you set OnAfterScroll event, check on it if field is set, if yes then set combo value to yes.

procedure ....table1AfterScroll(DataSet: TDataSet);
begin
  if not DataSet.FieldByName('UserField1').IsNull then
  begin
    ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('YES');
    Edit1.Text := DataSet.FieldByName('UserField1').AsString;
  end
  else
  begin
    ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('NO');
    Edit1.Text := '';
  end;
end;

Open in new window

0
AbhiJeetAuthor Commented:
my question ws not understood clearly i hope.

Let me clarify once again:

The combo has values YES, NO, user can select either yes or no or leave as blank.
When it is selected as YES, edit is mandatory, in database i shall save the value.
I shall save the form and close. Then i shall open the form, then i shall get the value as stored in database and populate the edit.
Then if edit is not empty string, i am setting YES in combo, upto this point there is no issue.

But when NO is selected in combo, or it is left as blank and form is saved and reopened, how i can set the combo value as per user input?
0
Geert GOracle dbaCommented:
"Yes", "No" and blank are 3 conditions

you have only given 2 conditions
if you have a record in the database it's yes.
otherwise it's either "No" or blank.

take the blank out is the obvious way to go.

simply put:
procedure SetCombo(RecordExists: Boolean);
const Vals: array[Boolean] of string = ('No', 'Yes');
begin
  combobox.itemindex := Combobox.items.indexOf(Vals[RecordExists]);
end;
0
AbhiJeetAuthor Commented:
ID: 38839340 - This shall set combo as yes if record exists and no otherwise. But combo can left as blank also in which case also above code shall set the combo as NO.
0
Geert GOracle dbaCommented:
what are the conditions and actions for "No" and "blank" ?

how do you set the combo blank after it has been filled with "Yes" or "No" ?
0
AbhiJeetAuthor Commented:
when no is selected or no option is selected and combo is left as blank, the editbox shall be disabled and shall not be stored into db.
0
Geert GOracle dbaCommented:
so basically "No" is the same as "blank" ?
0
AbhiJeetAuthor Commented:
yes, and difficulty is to set the value in combo as no when no has been selected and blank when it has been left as blank by user.
0
Sinisa VukSoftware architectCommented:
To not set combobox when some field is not set:
combobox1.itemindex := -1;

Open in new window

0
Geert GOracle dbaCommented:
add "No" and "Yes" to the items of the combo
set the style to csDropDownList
set the default upon start to "no"
itemindex = 0

then it can have only 2 values: either yer or no.
there is no point to allow for blank
as there is no difference between "no" and "blank"
0
AbhiJeetAuthor Commented:
user has 3 choices, to select yes or no or leave blank. And after saving and reopening user needs the same value in the combo as entered, yes, no  or blank.
0
AbhiJeetAuthor Commented:
any solution please?
0
geobulCommented:
Hi,

An option could be to store blank choices in the database as empty strings an No choices as null. This way you could cover the three cases:
- the value is null, so the combobox should be No;
- the value is an empty string, so it should be Blank;
- otherwise it should be Yes.

Geo
0
AbhiJeetAuthor Commented:
ID: 38845402: there are more than a dozen of this kind of combos in the form, hence cant store into db.

Any other  workaround please?
0
AbhiJeetAuthor Commented:
no solution? can  close  without assigning  any points?
0
Geert GOracle dbaCommented:
I have read your question and answers multiple times
there is a piece of crucial information missing

initial state of a combo: blank

if the user selects "Yes" an action saves data in the database, thus creating a record
when loading the value "yes" is set based upon existance of a record in the database

if the user selects "No" , data is not stored in the database
if the user leaves "blank", data is not stored in the database
both "blank" and "No" have the same output > no data stored in the database

you could call this a fork: 2 items lead to the same action
the action to take based on the user input is clear

the way back is unclear
you can't deduce what needs to be done based on the results

the only way is to store the status of each combobox
0
Sinisa VukSoftware architectCommented:
I read multiple times too :-)
Without working sample (without connection to real db) from AbhiJeet it is hard to finish this.
0
Geert GOracle dbaCommented:
your prerequisite:
>>Combo box values shall not be stored into database.

our solution:
Not possible with that prerequisite.

The solution:
Save the state (or value) of the comboboxes in the database
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.