Populating a TDBTest box Delphi XE2 - delphi-xe2

So, what I am trying to accomplish is when the number in one field is >= to a certain number then the field I want to populate will do a comparison and populate, or not depending on the condition. If the result is true the field is populated fine, but if the result is false it still performs the calculation and populates the field with " -4. I have worked trying to fix the problem for over a week. Can't seem to get it done. My variable 'total' is declared an integer.
I understand that I am comparing text and integers and that I should not compare that way and that I should convert the text to integer then calculate, but every time I try to convert something to integer I get an error code telling me that it is not a valid integer. I don't have enough experience to figure this out.
I am creating a four week budget. I buy an item, calculate how many portions there are and spread the cost through the four weeks. If there are more portions than 4 what is left over after the four week that number is calculated and automatically entered in the "leftover" field. In this code everything works perfectly, except the last if statement.It calculates and enters the data correctly, if the portions are more than 4. If they are less than 4 it writes a"-4" in the field. I cannot see my error. That is where I need help.
My Code:
procedure TForm1.costperChange(Sender: TObject);
var
Total: integer;
total1: double;
begin
Total1 := tblCosts.FieldByName('Cost').Asfloat / tblCosts.FieldByName('servings').Asfloat ;
costper.Text:= floattostr(total1);
if serv.Text >= '1' then
wk1.Text:= costper.Text
else
wk1.Text:= '';
if serv.Text >= '2' then
wk2.Text:= costper.Text
else
wk2.Text:= '';
if serv.Text >= '3' then
wk3.Text:= costper.Text
else
wk3.Text:= '';
if serv.Text >= '4' then
wk4.Text:= costper.Text
else
wk4.Text:= '';
if serv.Text >= '5' then
total:= strtoint(serv.Text);
total:= total - 4;
left.Text:= inttostr(total);
end;

Related

IIF and DATEADD Issue in SSRS Report

I’m trying to add three new columns into a report I’ve already created. I’d like to sum up the item quantities by their age. So, the first column would be the total quantity for each item for the last 0-7 days, second would be for the last 7-14 days and the last would be +14 days.
This is what I have currently:
=IIF(Fields!Date.Value >= DATEADD(DateInterval.Day, -7, FormatDateTime(Today)), SUM(Fields!Qty.Value))
I’ve tried modifying this in several ways but all (including the expression above) are underlined in red indicating that there is an error.
I’ve tried:
= IIF(Fields!Date.Value >= DATEADD(DateInterval.Day, -7, Now()), SUM(Fields!Qty.Value))
= IIF(Format(Fields!Date.Value, “dd/mm/yyyy”) >= DATEADD(DateInterval.Day, -7, Format(Today(), “dd/mm/yyyy”)), SUM(Fields!Qty.Value))
I’m at a loss as I don’t really write expressions too often. Am I going about this the wrong way?
The issue with your IIF is that there isn't an ELSE condition after the SUM. The IIF will return the value if the condition is true and the if not.
IIF(< condition >, < true >, < false >)
Also you want the sum to be outside the IIF in this instance, otherwise it will check the first date and if it matches add all the quantities from all dates.
=SUM(IIF(Fields!Date.Value >= TODAY.AddDays(-7), Fields!Qty.Value, 0))
If the Qty field is a decimal, the 0 may need conversion with CDEC(0).

How do I repeat a random number

I've tried searching for help but I haven't found a solution yet, I'm trying to repeat math.random.
current code:
local ok = ""
for i = 0,10 do
local ok = ok..math.random(0,10)
end
print(ok)
no clue why it doesn't work, please help
Long answer
Even if the preferable answer is already given, just copying it will probably not lead to the solution you may expect or less future mistakes. So I decided to explain why your code fails and to fix it and also help better understand how DarkWiiPlayer's answer works (except for string.rep and string.gsub).
Issues
There are at least three issues in your code:
the math.random(m, n) function includes lower and the upper values
local declarations hide a same-name objects in outer scopes
math.random gives the same number sequence unless you set its seed with math.randomseed
See Detailed explanation section below for more.
Another point seems at least worth mentioning or suspicious to me, as I assume you might be puzzled by the result (it seems to me to reflect exactly the perspective of the C programmer, from which I also got to know Lua): the Lua for loop specifies start and end value, so both of these values are included.
Attempt to repair
Here I show how a version of your code that yields the same results as the answer you accepted: a sequence of 10 percent-encoded decimal digits.
-- this will change the seed value (but mind that its resolution is seconds)
math.randomseed(os.time())
-- initiate the (only) local variable we are working on later
local ok = ""
-- encode 10 random decimals (Lua's for-loop is one-based and inclusive)
for i = 1, 10 do
ok = ok ..
-- add fixed part
'%3' ..
-- concatenation operator implicitly converts number to string
math.random(0, 9) -- a random number in range [0..9]
end
print(ok)
Detailed explanation
This explanation makes heavily use of the assert function instead of adding print calls or comment what the output should be. In my opinion assert is the superior choice for illustrating expected behavior: The function guides us from one true statement - assert(true) - to the next, at the first miss - assert(false) - the program is exited.
Random ranges
The math library in Lua provides actually three random functions depending on the count of arguments you pass to it. Without arguments, the result is in the interval [0,1):
assert(math.random() >= 0)
assert(math.random() < 1)
the one-argument version returns a value between 1 and the argument:
assert(math.random(1) == 1)
assert(math.random(10) >= 1)
assert(math.random(10) <= 10)
the two-argument version explicitly specifies min and max values:
assert(math.random(2,2) == 2)
assert(math.random(0, 9) >= 0)
assert(math.random(0, 9) <= 9)
Hidden outer variable
In this example, we have two variables x of different type, the outer x is not accessible from the inner scope.
local x = ''
assert(type(x) == 'string')
do
local x = 0
assert(type(x) == 'number')
-- inner x changes type
x = x .. x
assert(x == '00')
end
assert(type(x) == 'string')
Predictable randomness
The first call to math.random() in a Lua program will return always the same number because the pseudorandom number generator (PRNG) starts at seed 1. So if you call math.randomseed(1), you'll reset the PRNG to its initial state.
r0 = math.random()
math.randomseed(1)
r1 = math.random()
assert(r0 == r1)
After calling math.randomseed(os.time()) calls to math.random() will return different sequences presuming that subsequent program starts differ at least by one second. See question Current time in milliseconds and its answers for more information about the resolutions of several Lua functions.
string.rep(".", 10):gsub(".", function() return "%3" .. math.random(0, 9) end)
That should give you what you want

What makes Crystal ignore record selection formula?

Crystal 2008. Have record selection formula ending with
and
( ( "Zero" in {?Credit_Debit} and {V_ARHB_BKT_AGING_DETAIL.AMOUNT} = 0)
or ( "Credit" in {?Credit_Debit} and {V_ARHB_BKT_AGING_DETAIL.AMOUNT} < 0)
or ( "Debit" in {?Credit_Debit} and {V_ARHB_BKT_AGING_DETAIL.AMOUNT} > 0) )
but no matter what combination of values is selected for Credit_Debit the result set is the same.
Also without success, I tried joining the parameter array into a single string and using lines like
or ( {#Cred_Deb_Choices} like "*Credit*" and {V_ARHB_BKT_AGING_DETAIL.AMOUNT} < 0)
Using the first method works in the same formula when the parameter values are integers, as:
and ({?Location ID} = 0 or {V_ARHB_BKT_AGING_DETAIL.LOC_ID} in {?Location ID})
I examined the generated SQL, and saw that the part at the beginning that had no effect was not shown.
I changed a part that tested for a hard-coded value to instead test for a parameter value, and looked at the SQL again. No change.
When you try to create a filter that doesn't fit with the datatype of the field then that doesn't get reflected in record selection formula.
For Integer field give integers in record selection for text give text.
E.g:
ID=0 and Name='XXX' works
ID='Zero' and Name='XXX' doesn't
This should solve your issue

Using Apex Validation on Text Fields?

I'm having an issue getting my validations to work correctly in Apex.
I have 3 page items that are causing me trouble, :P5_JACKPOT, :P5_TICKET_PRIZE, :P5_TOTAL_PRIZE. Jackpot can be any size, and ticket_prize + total_prize can be any size as long as they are LESS then jackpot. The validations I have in place for this are as follows:
if :P5_TICKET_PRIZE > :P5_JACKPOT then
return false;
else
return true;
end if;
Same validation for both items, with the necessary replacements, simple enough. The issue is, it doesn't seem to work for all numbers. For example, having a jackpot value of 200, and 50 for both other items cause the error to flag, when it shouldn't. However, having a jackpot value of 200, and other values of 100 + 100 don't cause the error flag, as it should. It seems that some numbers work, and others don't. Is there any reason why this is?
It sounds like the problem is one of data typing. :P5_TICKET_PRIZE and :P5_JACKPOT are both strings so when you compare them, you get character comparison semantics. Alphabetically, the string "50" comes after the string "200" since the character "5" comes after the character "2". If you want to compare the numeric value in :P5_TICKET_PRIZE to the numeric value in:P5_JACKPOT, you'd need to apply a to_number function to both sides of the expression
if to_number( :P5_TICKET_PRIZE ) > to_number( :P5_JACKPOT ) then
return false;
else
return true;
end if;

number of days in a period that fall within another period

I have 2 independent but contiguous date ranges. The first range is the start and end date for a project. Lets say start = 3/21/10 and end = 5/16/10. The second range is a month boundary (say 3/1/10 to 3/31/10, 4/1/10 to 4/30/10, etc.) I need to figure out how many days in each month fall into the first range.
The answer to my example above is March = 10, April = 30, May = 16.
I am trying to figure out an excel formula or VBA function that will give me this value.
Any thoughts on an algorithm for this? I feel it should be rather easy but I can't seem to figure it out.
I have a formula which will return TRUE/FALSE if ANY part of the month range is within the project start/end but not the number of days. That function is below.
return month_start <= project_end And month_end >= project_start
Think it figured it out.
=MAX( MIN(project_end, month_end) - MAX(project_start,month_start) + 1 , 0 )

Resources