Solved

Setting a Default Value in a Function for Delphi

Posted on 2011-03-05
12
396 Views
Last Modified: 2012-05-11
I have this Parameter which is part of function:

ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));

Open in new window


I need to set a default value of 0 (zero) when this field is empty. Otherwise it outputs whatever the value is in the field.

How best to do this please?
0
Comment
Question by:zberg007
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 14

Expert Comment

by:systan
ID: 35044727
var v_oil:integer;
...
...
v_oil := StrToInt(StripIT(fieldByName('Oil Added').AsString));
if v_oil <= 0 then
ParamByName('@FluidAdded').Value := 0
else
ParamByName('@FluidAdded').Value := v_oil;
...
...
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044780
You can test the value of the field first as follows:
if fieldByName('Oil Added').AsString = '' then
  ParamByName('@FluidAdded').Value := 0
else
  ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));

Open in new window


or
if fieldByName('Oil Added').IsNull then
  ParamByName('@FluidAdded').Value := 0
else
  ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35044792
A bit of a hack, but if Oil Added is always positive or blank, prepend a leading zero to make it always a number.

ParamByName('@FluidAdded').Value := StrToInt(StripIT('0'+fieldByName('Oil Added').AsString));

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044807
You need to check the value of the field "Oil Added" whether it is convertible to Integer, to avoid error's messages:
if fieldByName('Oil Added').IsNull then
  ParamByName('@FluidAdded').Value := 0
else
  begin
    try
      ParamByName('@FluidAdded').Value := StrToInt(StripIT(fieldByName('Oil Added').AsString));
    except
      showmessage(fieldByName('Oil Added').AsString +' is a non-integer value!');
    end;
  end;

Open in new window

0
 

Author Comment

by:zberg007
ID: 35044810
Hey systan, when I run your solution, I get this error while running the application:

raised exception error class EConvertError with message '"is not a valid integer value'.

Any ideas if I have something wrong?

Thanks!
0
 

Author Comment

by:zberg007
ID: 35044848
jimyX. thanks I'm trying this and I get a quick message of: Undeclared identifier: 'showmessage'

Thanks!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35044857
This version should work even if there are spaces in the original Oil Added field.

ParamByName('@FluidAdded').Value := StrToInt('0'+StripIT(fieldByName('Oil Added').AsString));

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044861
If you need it add "Dialogs" to the uses clause.
0
 

Author Comment

by:zberg007
ID: 35044903
sjklein42, You're solution works as is, easy and short with no errors!

Otherwise, I kept getting '"is not a valid integer value'

Thanks again!
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35044916
Thanks.  Sometimes you just have to look at it from a different angle.  ;)
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35044963
First congrats for the points sjklein42.

What angle was that? if you have spaces or non-convertible-to-integer value you will get an error.

This will get you an error:
showmessage(inttostr(strtoint('0'+'1 ')));

I am posting this for the records and to clarify for the future viewers. But anyway that angle seems to suit the author of this question.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35045003
jimyX - You're absolutely right.  I hope you saw the smiley face.  One doesn't need to be much of a purist to be rankled.  But it just seemed so simple I had to throw it in.  Sorry - I didn't mean to snipe you.

It is a pragmatic solution (ie: hack) that works just fine in this case, since the number is always positive.  Spaces and nonsense, it may also handle, if the StripIT function does what I'm guessing and strips out non-numeric characters.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 119
Comparing dates in Delphi. Greater than/ Less than 3 113
Delphi selector screen 2 69
How to debug For loops? 3 45
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A short film showing how OnPage and Connectwise integration works.

930 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

8 Experts available now in Live!

Get 1:1 Help Now