He there, I'm new to VB script and I am trying to make a select case to change an emailadress to a combobox choice. I know i'm thinking to easy but i need help with the direction. the combobox works but i cannot get the value chosen in the combobox to trigger the select case.
Sub Item_Open()
Set FormPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = FormPage.Controls("Subject")
Control.PossibleValues = "SPOED;STATUS;MANCO;KLACHT;TRANSPORT;TRACKING;INKOMEND;REPARATIE;RETOUREN;LADEN;MILKRUN"
Set MyPage = Item.GetInspector.ModifiedFormPages("Message")
Set Mail = MyPage.Item("Subject").Value
Select Case Mail
Case SPOED
Item.To = "hihi#blabla.com"
Case STATUS
Item.To = "haha#blabla.com"
Case else
Item.To = ""
End Select
End Sub
Assuming MyPage.Item("Subject").Value returns a string value like "STATUS". Then you must pick it up in a string variable:
strMail = MyPage.Item("Subject").Value ' look ma, no Set!
The Select Case X statement evaluates the X expression and compares it to the values Y, ... in the Case Y parts and executes the first block for which (the values of) X and Y are equal. To apply this to strMail containing strings like "SPOED", the Case Y expressions should be string literals:
Select Case strMail
Case "SPOED"
...
Case "STATUS"
...
If you would have used Option Explicit, VBScript would have told, that SPOED (etc) is (understood as) an unitialized variable that can't be equal to a (decent) strMail.
Related
I'm new in shell programming.
I've to verify if certain sql query returns any tuple/row.
If something`s returned then one action is made, else another action is made. Something like this:
getResults(){
getAnyResults=`sqlplus -s $MMUSER/$MMPASS <<!
set verify off
set heading off
set feedback off
set pages 0
set pause off
set linesize 2500
select x, y, z
from A;
exit;
!`
}
Edit 02/09/2021 - Begin
CREATE TABLE A (
x INTEGER,
y INTEGER,
z INTEGER
);
SELECT * FROM A;
The above code should not return any tuple since i've not inserted anything in table A
However if i do this:
CREATE TABLE A (
x INTEGER,
y INTEGER,
z INTEGER
);
INSERT INTO A
VALUES(1,2,3);
COMMIT;
SELECT * FROM A;
one tuple must be returned(1,2,3)
What i want is to verify if any tuple is returned and if so action1 is made, if any tuple is not returned, action2 is made
Edit 02/09/2021 - End
I'm storing the query's result on getAnyResults variable.
If variable getAnyResults returns any tuple/row one action is made, else another action is made
What's the best way to do it?
If getAnyResults must contain at least a tuple, you can just check this way:
if echo "$getAnyResults" | grep -Eq "\(.+\)"; then
echo ACTION1
else
echo ACTION2
fi
Of course the regexp can be more strict if you need this.
According to the comments, you need to match something like x=1, y=2, z=3 or x=5, y=8, z=4. In this case a suitable test is:
echo "$getAnyResults" | grep -Eq "x=[0-9]+, y=[0-9]+, z=[0-9]+"
I have defined some Global Constants:
'Reason Codes'
Global Const MQRC_NONE = 0
Global Const MQRC_APPL_FIRST = 900
Global Const MQRC_APPL_LAST = 999
Now I want to get the constant name from its value in VB6.
Is it possible, I know it can be done in .Net and Java. Not sure about vb6.
Use a select statement. I do not like this solution but at least you can get things done with it.
Select Case constantValue
Case MQRC_NONE
result = "MQRC_NONE"
Case MQRC_APPL_FIRST
result = "MQRC_APPL_FIRST"
Case MQRC_APPL_LAST
result = "MQRC_APPL_LAST"
Case Else
result = "N/A"
End Select
If you have control over the constant values make them range 0,1,2. Then you can just index another array of equivalent constant strings based on this constant index.
I have an SSRS report where there is a parameter that asks the user to include records where revenue is greater than zero, or records with revenue values that are just zero.
Since the query is not a stored procedure and it is not an option to put it into a procedure, I need to use some case logic for the embedded query. I need to do this in the where clause in the end.
I am trying to do something like this:
SELECT * FROM TABLE
WHERE MY_DATE BETWEEN D_START AND D_END
AND
CASE
WHEN :REVENUE = 1 THEN REV != 0
WHEN :REVENUE = 2 THEN REV = 0
END
However, when I run this query I get the following error:
ORA-00905: missing keyword
Is what I am doing not possible? Or is there an error that someone can see and help me with?
Please help. Thanks!
UPDATE: Just to clarify, the user is passing a value of 1 or 2. And the query should filter the data according to what value is passed to it. If 1 is passed in the parameter, then filter out all revenue not equal to zero. Else if two is passed, then filter so that only records where revenue is zero is returned.
You can write it better with a bit of boolean logic:
SELECT * FROM TABLE
WHERE MY_DATE BETWEEN D_START AND D_END
AND (
(:REVENUE = 1 AND REV != 0)
OR
(:REVENUE = 2 AND REV = 0 )
)
CASE is meant to extract different values based on conditions, so you can use it to check conditions, but you need to use it as a value to check against a condition
It's not necessary to use a CASE expression to get this particular result.
But it is possible to make use of one.
The problem in the original query is that Oracle is more strict than other databases (like MySQL) in that Oracle doesn't implicitly convert a boolean expression to a value, or convert a value into boolean.
I suspect that Oracle is choking in a couple of places. The error message is only showing us one of those.
The CASE expression returns a value, and Oracle is balking that he won't evaluate the value as a boolean.
To get that value evaluated as a boolean, we could do a comparison of the value to some other value.
If we fix that, I think Oracle is still going to choke on the expression following THEN. Oracle is expecting to return a value, and it's finding a comparison, which evaluates to a boolean.
Okay, so we know the CASE expression needs to return a value, and we need to use that in a boolean expression. If we move that conditional test into the WHEN part, and specify a value to be returned in the THEN, we can compare the return from the CASE expression to another value.
(As an aside... I strongly recommend that you qualify the column references in the SQL statement. That makes the intent more clear. Looking at the statement, it looks like MY_DATE, D_START and D_END are all column references. That's perfectly valid, it just seems a bit odd to me.)
As an example, we could do something like this with the CASE expression:
SELECT t.*
FROM TABLE t
WHERE t.MY_DATE BETWEEN t.D_START AND t.D_END
AND CASE
WHEN ( :REVENUE = 1 AND t.REV != 0 ) THEN 1
WHEN ( :REVENUE = 2 AND t.REV = 0 ) THEN 1
ELSE NULL
END = 1
The parens inside the CASE aren't necessary; I just included them to highlight the part that Oracle is evaluating in a boolean context.
So, does that work? If the value passed in for :REVENUE is 2, the condition in the first WHEN won't evaluate to TRUE (the result of first comparison is guaranteed to be FALSE). The condition in the second WHEN may evaluate to TRUE (first comparison will yield TRUE, the result from second comparison will depend on the value in the REV column.)
That CASE expression is either going to return a value of 1 or NULL. (We could just as easily use a 0 or a -1, or 999 in place of NULL if we wanted.)
Once the CASE expression is evaluated, the value returned will be compared to a literal value, as if we wrote e.g. val = 1. That comparison is evaluated as boolean. If it evaluates to TRUE, the row will be returned...
To get Oracle to behave similarly to other databases (like MySQL), we would need to make the conversion from boolean to value and value to boolean explicit. We would still need the return from the CASE compared to 1, like we did above. In place of REV != 0 we could use another CASE expression. I'm not recommending this, just shown here for illustration, converting a boolean to a value.
WHERE CASE
WHEN ( :REVENUE = 1 )
THEN CASE WHEN ( t.REV != 0 ) THEN 1 ELSE NULL END
WHEN ( :REVENUE = 2 )
THEN CASE WHEN ( t.REV = 0 ) THEN 1 ELSE NULL END
ELSE
NULL
END = 1
Note that the return from the outermost CASE expression is being compared to a value, so we get a boolean (where Oracle expects a boolean.)
All of the ELSE NULL in the statements above can be omitted for an equivalent result, since that's the default when ELSE is omitted.)
Again, it's not necessary to use a CASE expression. You can get equivalent results without it. For example:
SELECT t.*
FROM TABLE t
WHERE t.MY_DATE BETWEEN t.D_START AND t.D_END
AND ( ( :REVENUE = 1 AND t.REV != 0 )
OR ( :REVENUE = 2 AND t.REV = 0 )
)
In these queries that all return an equivalent result, the CASE expression doesn't buy us anything. But in some circumstances, it can have some advantages over a regular OR, because the CASE expression stops evaluation when a condition in a WHEN clause evaluates to TRUE.
The problem is that Oracle SQL does not have the boolean data type, so you cannot have columns of type boolean, pass boolean parameters to a query, have boolean expressions etc. So they have the somewhat unnatural concept of "condition" which is something that goes into logical conditions (like in the WHERE clause). Unfortunately, when they introduced the case EXPRESSION, which can be used wherever any other expression can be used (but this excludes boolean), they DID NOT introduce a "case CONDITION" - which could be used where other conditions can be used. This omission is odd, since the code for a case condition would probably use 95% of the code for the case expression. All the more weird since PL/SQL does have the boolean type, and the case expression there works seamlessly for Booleans.
I'm new to VBScript and I am running into some trouble. The script is making an API call and pulling account information, placing the data into a CSV file. I'm pulling the data into an array, looping through each account and, if certain properties qualify, assigning them to a variable to be written to the CSV. The problem I am having is if one account qualifies for a property, it sets the variable and if the next account doesn't qualify, the variable is still retaining the value, giving false results in the CSV.
Set SFTPServer = WScript.CreateObject("SFTPCOMInterface.CIServer")
accounts = SFTPServer.AdminAccounts
For each admin in accounts
adminName = admin.Login
Dim count : count = admin.GetPermissionsCount()
For i = 0 To CInt(count )- 1
Set permission = admin.GetPermission(i)
' AdminPermissionsPolicy:
' ServerManagement = 0,
' SiteManagement = 1,
' STManagement = 2,
' UserCreation = 3,
' ChangePassword = 4,
' COMManagement = 5,
' ReportManagement = 6,
Select case permission.Permission
case 0:
serverAdmin = "Server Admin"
case 1:
site = permission.SiteName
case 2:
stMan = "2"
case 3:
userCreate = "3"
case 4:
chPassword = "4"
case 5:
comMan = "5"
case 6:
report = "6"
End Select
Next
WriteStuff.WriteLine""+adminName+"|"+site+"|"+stMan+"|"+userCreate+"|"+chPassword+"|"+comMan+"|"+report+"")
Next
Unfortunately variables in VBScript are either at global, or function scope.
So you'll need to reset each of the variables on each iteration of the for loop.
One way would be to write Dim dummy at the top of your script, and just before the Select Case, write serverAdmin = dummy, site = dummy etc.
It's good practice to Dim explicitly all your variables, and to use Option Explicit at the top of the module to enforce that.
Here's an alternate way to do what you need without having to empty each variable on each iteration. You've already documented what each value means in your code. Instead of using comments for that purpose, define them as constants at the top of your scope block:
' AdminPermissionsPolicy:
Const ServerManagement = 0
Const SiteManagement = 1
Const STManagement = 2
Const UserCreation = 3
Const ChangePassword = 4
Const COMManagement = 5
Const ReportManagement = 6
Then you can declare an array to hold the values:
Dim a(6)
And then in your loop you can empty the array on each iteration using the Erase function. You can use the constant names instead of 0/1/2/etc and, when it comes time to write the values, you can use Join() to combine your array values into a string instead of having to concatenate 7 variables.
For each admin in accounts
adminName = admin.Login
Erase a ' Empty the Permissions array for each new account
Dim count : count = admin.GetPermissionsCount()
For i = 0 To CInt(count )- 1
Set permission = admin.GetPermission(i)
Select case permission.Permission
case ServerManagement: ' Now you can use the constant instead of "0"
a(ServerManagement) = "Server Admin"
case SiteManagement:
a(SiteManagement) = permission.SiteName
...
End Select
Next
WriteStuff.WriteLine Join(a, "|") ' Use Join() to combine array values
Next
Let's start with the output. You want to print a list of items (some of them possibly Empty) separated by "|". That should be done like this:
WriteStuff.WriteLine Join(aOut, "|")
Advantages:
You don't need to know that VBScript's concatenation operator is &, not +, because you can't even use the wrong one with Join.
You don't need to repeat the separator.
No useless pre/ap-pending of the empty string "".
Works with any number of items.
aOut needs to be initalized in the loop. That is easy with ReDim - without Preserve.
Advantages:
You don't need to know that Empty is the literal for empty/uninitialzed in VBScript.
You don't need to repeat an assignment for each variable.
Works with any number of items.
Demo code:
Option Explicit
Const cnUB = 3
Dim nTest
For nTest = 0 To cnUB
ReDim aOut(cnUB)
Select Case nTest
Case 0
aOut(0) = "A"
Case 1
aOut(1) = "B"
Case 2
aOut(2) = "C"
Case 3
aOut(3) = "D"
End Select
WScript.Echo Join(aOut, "|")
Next
output:
cscript 31565794.vbs
A|||
|B||
||C|
|||D
Disadvantage:
Putting the data into the array anonymously (just known by number/index) may be more errorprone than using distinct variable( name)s. If you need the elements for further computations it may be a good idea to define constants
wtf
Const ciReport = 1
...
aOut(ciReport) = "B"
I need help with the following:
Dim reportesTA(2)
reportesTA(0) = "Report1"
reportesTA(1) = "Report2"
I want to execute certain actions for each item in the array list. So I was thinking of working with a Select/Case. I tried this but didn't work:
Select Case reportesTA
Case 0
//do stuff
Case 1
//do stuff
End Select
Is there any way to get the cases switch? something like a switch/case? Anyone has a better way to work with each item of the array? Thanks very much.
Dim reportesTA(2)
reportesTA(0) = "Report1"
reportesTA(1) = "Report2"
For I = LBound(reportesTA) to UBound(reportesTA)
Select Case reportesTA(I)
Case "Report1"
MsgBox "Report1"
Case "Report2"
MsgBox "Report2"
End Select
Next
Explanation:
In "FOR" cycle we passing through of all elements in array, starting from first one till last one.
function "LBound" return lowest ID of available element in array.
function "UBound" return highest ID of available element in array.
In "SELECT CASE" we taking value (yes, it has string type) of element from array and then making decision what we should do -- in this sample we popup message box that displaying report name.
You need to de the Select case with an element of the array, not the array itself.
I would do it like this, more clear as to what you are doing and more DRY.
reportesTA = Array("Report1", "Report2")
Sub do_stuff(text)
WScript.echo text
End Sub
For each element in reportesTA
Select Case element
Case "Report1"
do_stuff "Report1"
Case "Report2"
do_stuff "Report2"
End Select
Next
In this example you would rather do the following
For each element in reportesTA
do_stuff element
Next