Solved

value set

Posted on 2013-01-30
21
382 Views
Last Modified: 2013-02-25
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?
0
Comment
Question by:AbhiJeet
  • 10
  • 6
  • 4
  • +1
21 Comments
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38837178
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
 

Author Comment

by:AbhiJeet
ID: 38838351
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
 

Author Comment

by:AbhiJeet
ID: 38838394
is it possible to store user input for the combo into a disconected client dataset and retrieve later?
how to implement?
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38838642
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
 

Author Comment

by:AbhiJeet
ID: 38839326
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 38839340
"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
 

Author Comment

by:AbhiJeet
ID: 38839418
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 38839459
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
 

Author Comment

by:AbhiJeet
ID: 38839486
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
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 38839551
so basically "No" is the same as "blank" ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:AbhiJeet
ID: 38839771
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
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38839781
To not set combobox when some field is not set:
combobox1.itemindex := -1;

Open in new window

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 38839898
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
 

Author Comment

by:AbhiJeet
ID: 38840498
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
 

Author Comment

by:AbhiJeet
ID: 38840981
any solution please?
0
 
LVL 17

Expert Comment

by:geobul
ID: 38845402
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
 

Author Comment

by:AbhiJeet
ID: 38845950
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
 

Author Comment

by:AbhiJeet
ID: 38849065
no solution? can  close  without assigning  any points?
0
 
LVL 36

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 500 total points
ID: 38850162
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
 
LVL 25

Expert Comment

by:Sinisa Vuk
ID: 38850196
I read multiple times too :-)
Without working sample (without connection to real db) from AbhiJeet it is hard to finish this.
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 38850244
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now