If...Then...Else with multiple statements after Then - vba

a very easy question: considering an If...Then...Else instruction in VBA, how can I separate multiple instructions after Then? In other words, should I write something like
If condition [ Then ]
[ statement1 ] & [statement2]
Else [Else statement] (i.e. using "&"),
or
If condition [ Then ]
[ statement1 ] And [statement2]
Else [Else statement] (i.e. using "And"),
or some other separator/command?

Multiple statements are to be separated by a new line:
If SkyIsBlue Then
StartEngines
Pollute
ElseIf SkyIsRed Then
StopAttack
Vent
ElseIf SkyIsYellow Then
If Sunset Then
Sleep
ElseIf Sunrise or IsMorning Then
Smoke
GetCoffee
Else
Error
End If
Else
Joke
Laugh
End If

This works with multiple statements:
if condition1 Then stmt1:stmt2 Else if condition2 Then stmt3:stmt4 Else stmt5:stmt6
Or you can split it over multiple lines:
if condition1 Then stmt1:stmt2
Else if condition2 Then stmt3:stmt4
Else stmt5:stmt6

Related

Rose::DB masking in query statements

I'm seeking some help on my Rose::DB issue as described below.
I have an account object, and this has an integer "accounttype" field. In this example I will use the following accounttype constants:
ACCOUNT_TYPE_SPECIAL_MASK = 0x10;
ACCOUNT_TYPE_SPECIAL_1 = 0x10;
ACCOUNT_TYPE_SPECIAL_2 = 0x11;
ACCOUNT_TYPE_NORMAL_MASK = 0x20;
ACCOUNT_TYPE_NORMAL_1 = 0x20;
ACCOUNT_TYPE_NORMAL_2 = 0x21;
At present, when I want accounts of a given type, I'd list them all and do something like this:
my $iter = Test::Account::Manager->get_accounts_iterator(
db => $db,
query =>
[
'accounttype' => [ ACCOUNT_TYPE_SPECIAL_1, ACCOUNT_TYPE_SPECIAL_2 ],
]
);
However, I'd like to be able to query for the accounts using the appropriate mask, rather than specify all possible types.
I'd like to be able to say:
my $iter = Test::Account::Manager->get_accounts_iterator(
db => $db,
query =>
[
'accounttype' => 'accounttype & ACCOUNT_TYPE_SPECIAL_MASK'
]
);
However, I haven't spotted any way to do this. Any help or recommendations most welcome.
Thanks!
Let's say your SQL server understands the following:
(accounttype & 16) <> 0
This would then suggest that you could use the following:
[ \'(accounttype & ?) <> 0' => ACCOUNT_TYPE_SPECIAL_MASK ]
Under the circumstances, you could inline the constant.
\sprintf('(accounttype & %d) <> 0', ACCOUNT_TYPE_SPECIAL_MASK)
I don't know if these two versions result in different SQL, and I don't know which is faster if so.

xQuery: test for two specific children with 'and'

Assume I have the following snippet
<persName>
<forename>Gaius</forename>
<surname>Iulius</surname>
<addName>Caesar</addName>
</persName>
I need a result like [surname], [forename] where the comma should only be present if necessary.
In XSLT, I'd simply use
<xsl:value-of select="surname" />
<xsl:if test="surname and forename"><xsl:text>, </xsl:text></xsl:if>
<xsl:value-of select="forename" />
Now I naively thought I could transfer this to XQuery – and failed.
I was slightly puzzled that
if ($node/forename) then "1" else "0"
if ($node/surname) then "1" else "0"
if ($node/surname and $node/forename) then "1" else "0"
will give 1, 1, 0, respectively.
I worked around this by counting the number of children in one of these cases but I'm still puzzled why this is the way it is.
Tanks for any input!
Edit: here's the code I've been using:
declare function habe:shortName($node) {
<span>{
if ($node/tei:name) then $node/tei:name
else
let $tr := if ($node/tei:surname and count($node/tei:forename)>0) then ", " else ""
return concat($node/tei:surname, $tr, $node/tei:forename)
}</span>
};
which, when given the above snippet, returned IuliusGaius.
I then tried the three tests above and got the stated result.
I'm doing this on eXist – maybe their implementation is faulty?
Edit: Thanks to #MichaelKay and #joewiz for hinting at misleading typos!
The following code returns the expected results, (1, 1, 1), using eXide on http://exist-db.org/exist/apps/eXide/index.html:
xquery version "3.0";
let $node :=
<persName>
<forename>Gaius</forename>
<surname>Iulius</surname>
<addName>Caesar</addName>
</persName>
return
(
if ($node/forename) then "1" else "0",
if ($node/surname) then "1" else "0",
if ($node/surname and $node/forename) then "1" else "0"
)
One solution to your original problem (adding a ', ' if both forename and surname exist) is to use string-join($strs as xs:string*, $joiner as xs:string) instead of concat($string as xs:string[...]):
let $name :=
<persName>
<forename>Gaius</forename>
<surname>Iulius</surname>
<addName>Caesar</addName>
</persName>
return string-join(($name/surname, $name/forename), ', ')
This returns Iulius, Gaius.
You can also check for presence of nodes directly in boolean expressions:
let $name :=
<persName>
<forename>Gaius</forename>
<surname>Iulius</surname>
<addName>Caesar</addName>
</persName>
return (
$name/surname and $name/forename,
$name/surname and $name/foo
)
This returns true false.

SQL Server CASE then statement error

I have a pretty simple Stored Procedure that I'm trying to add a case then statement in the Where Statement. I keep getting an error message:
Msg 156, Level 15, State 1, Procedure Proc_AssuranceBilling_rpt, Line 36
Incorrect syntax near the keyword 'Case'.
I don't know why. Can someone please look and tell me what I've done wrong?
WHERE
e.[DDEventDesc] IN (SELECT #rptType
Case WHEN 'Payroll - Audit' THEN ('Payroll - Audit')
WHEN 'Audits' Then ('Audit - Aup', 'Audit - EBP', 'Audit- Financial Institutions','Audit - Governmental','Audit - HUD','Audit - Not-for-Profit','Audit - Personal Property Tax','Audit - Single Audit','Audit - Small Business')
WHEN 'Review & Comps' Then ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc')
WHEN 'Assur Tax Returns' THEN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter')
END)
AND AR.[ARType] = 1
AND (CLT.[cmaster] = 1 OR CLT.[cinvIndivEng] = 0)
I think you need to rewrite your WHERE clause as:
WHERE ((#rptType = 'Payroll - Audit' AND e.[DDEventDesc] = 'Payroll - Audit') OR
(#rptType = 'Audits' AND e.[DDEventDesc] IN ('Audit - Aup',
'Audit - EBP',
'Audit- Financial Institutions',
'Audit - Governmental',
'Audit - HUD',
'Audit - Not-for-Profit',
'Audit - Personal Property Tax',
'Audit - Single Audit',
'Audit - Small Business') OR
(#rptType = 'Review & Comps' AND e.[DDEventDesc] IN ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc')) OR
(#rptType = 'Assur Tax Returns' AND e.[DDEventDesc] IN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter') )
AND AR.[ARType] = 1
AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)
CASE cannot be used to return multiple values. From MSDN on CASE:
Syntax:
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
THEN result_expression Is the expression returned when
input_expression equals when_expression evaluates to TRUE, or
Boolean_expression evaluates to TRUE. result expression is any valid
expression.
And what can any valid expression be? According to Expressions:
Is a combination of symbols and operators that the SQL Server Database
Engine evaluates to obtain a single data value.
the variable #rptType should be the case variable .. u placed it out side the case
try this..
WHERE e.[DDEventDesc] IN ( SELECT Case #rptType
WHEN 'Payroll - Audit' THEN ('''Payroll - Audit''')
WHEN 'Audits' Then ('''Audit - Aup, Audit - EBP'', ''Audit- Financial Institutions'',''Audit - Governmental'',''Audit - HUD'',''Audit - Not-for-Profit'',''Audit - Personal Property Tax'',''Audit - Single Audit,Audit - Small Business''')
WHEN 'Review & Comps' Then ('''Audit - Review'', ''Audit -Comp/Disc'',''Audit - Comp w/o Disc''')
WHEN 'Assur Tax Returns' THEN ('''5500'',''720-PCORI'',''8955-SSA'',''Campaign Report'',''Corporate (1120-POL)'',''LM-1'',''LM-2'',''LM-3,''LM-4,''LM-10'',''LM-30'',''Non-Profit (990)'',''Non-Profit (990 EZ)'',''Non-Profit (990-N)'',''Non-Profit (990-T)'',''Schedule C Letters'',''Section 104D Letter''')
END
)
AND AR.[ARType] = 1
AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)

OSclass osc_set_preferences not working on the second set

The below code works for the first $myurl1 only after setting for the first time, it doesnot set for the other statements, can anybody help on this.
$myurl="http://alberta.domain.com/";
$myurl2="http://toronto.domain.com/";
if ($myurl1) {
osc_set_preference('subdomain_type', 'region', 'osclass');
osc_reset_preferences();
} elseif ($myurl2) {
osc_set_preference('subdomain_type', 'city', 'osclass');
osc_reset_preferences();
} else {
osc_set_preference('subdomain_type', 'country', 'osclass');
osc_reset_preferences();
}
Since $myurl1 is not empty, if will only execute the first if statement. elseif or else will not be executed

Linq to SQL Case WHEN in VB.NET?

How do I do a Case WHEN in Linq to SQL (vb.net please).
In SQL it would be like this:
SELECT
CASE
WHEN condition THEN trueresult
[...n]
[ELSE elseresult]
END
How would I do this in Linq to SQL?
var data = from d in db.tb select new {
CaseResult = If (d.Col1 = “Case1”, "Case 1 Rised", If (d.Col1 = “Case2”, "Case 2 Rised", "Unknown Case"))
};
Check This Out
var data = from d in db.tb select new {
CaseResult = (
d.Col1 == “Case1” ? "Case 1 Rised" :
d.Col1 == “Case2” ? "Case 2 Rised" :
"Unknown Case")
};
Please Note that [ ? Symbol = then] , [ : Symbol = Or].
I haven't tried this but you may be able to do something like:
Dim query = From tbl In db.Table _
Select result =_
If(tbl.Col1 < tbl.Col2,"Less than",_
If(tbl.Col1 = tbl.Col2,"Equal to","Greater than"))
You would just need to keep nesting the If functions to handle all of your cases.
You can find more examples of various queries at http://msdn.microsoft.com/en-us/library/bb386913.aspx