×

Subscribe to newsletter

subscribe to our weekly newsletter to get notified with latest story and article Subscribe now!




PLC BLOG | If Else Conditional Statement In Siemens Wincc Vbscript

If Else Conditional Statement In Siemens Wincc Vbscript

A conditional statement in vbscript is used to perform different task or action which base on a specified condition is true or false. an if else is conditional statement which execute task or action if condition is true otherwise else condition is executed. you can use multiple if else statement within vbscript. The condition of if else statement is an expression with Boolean value (TRUE or FALSE). Logical expression or comparative expressions can be stated as conditions. When the if else condition is executed, the stated expressions are evaluated. If the value of an expression is TRUE, the if task is executed otherwise else task is executed.

If Then conditional statements in wincc vbscript

if then conditional statement execute task if condition of if statement is true. following are the expression of If then conditional statement in wincc hmi vbscript
    If _condition_ Then
    'statements
    End If
If Then conditional statements in wincc vbscript
above is the example of if then conditional statement in wincc vbscript. in this example we have two input output field which associated with tag1 and tag2 and when button pressed events evaluates if condition. If the condition is true then statement executed. Condition for this example is to evaluate value of both input output fields if the value are equal than textfild1 text changed to both values are equal. following are code explanation
    This script compares two values retrieved from SmartTags and updates the text of a text field on the screen if the values are equal.
  • Dim value1, value2, result: Declares variables to store the values and the object reference.
  • Set result = ...: Sets the reference to the text field object on the screen named "screen1".
  • value1 = SmartTags("tag1"): Retrieves the value of SmartTag named "tag1".
  • value2 = SmartTags("tag2"): Retrieves the value of SmartTag named "tag2".
  • If value1 = value2 Then: Checks if the values of the two SmartTags are equal.
  • result.Text = "Values are equal": Updates the text field with the message "Values are equal" if the condition is true.

If Then else conditional statements in wincc vbscript

if then else statement in VBScript is used to perform conditional checks and execute specific blocks of code based on the result of the condition. if the condition is true then if statement is executed if the condition is false then else condition is executed following are the expression of if then else conditional statement in wincc vbscript
    If _condition Then
    ' Code to execute if the condition is true
    Else
    ' Code to execute if the condition is false
    End If
  • If condition Then: Evaluates the condition. If it is true, the code block immediately following this statement is executed.
  • Else: Executes this block if the condition is false.
  • End If: Marks the end of the conditional structure.

wincc vbscript example of if then else conditional statment

below is the example of if then else conditional statement in wincc vbscript. in this example compare two input output fields value when button is pressed if the values are equal then text field text changed to "values are equal otherwise else statement is executed and text field text changed to "both values are not equal". tag1 and tag2 are associated with both input output field.
wincc vbscript example of if then else conditional statment
following are the code explanation
  • Dim value1, value2, result: Declares variables to store the SmartTag values and the text field object.
  • Set result = ...: Sets the result variable to reference a text field object on the screen named "screen1".
  • value1 = SmartTags("tag1"): Retrieves the value of the SmartTag named tag1.
  • value2 = SmartTags("tag2"): Retrieves the value of the SmartTag named tag2.
  • If value1 = value2 Then: Checks if the values of tag1 and tag2 are equal.
  • result.Text = "Both Values Are Equal": Updates the text field with the message if the condition is true.
  • Else: Executes this block if the values are not equal.
  • End Sub: Marks the end of the subroutine.
how it works
  1. The values of two SmartTags (tag1 and tag2) are retrieved and stored in variables.
  2. It sets a reference to the text field object on a specific screen.
  3. A conditional check is performed to see if the two values are equal.
  4. If the values are equal, the text field displays "Both Values Are Equal".
  5. If the values are not equal, the text field displays "Both Values Are Not Equal".

wincc vbscript multiple if else statement

A multiple If-Else statement evaluates multiple conditions, and if any of the conditions are true, it executes the corresponding block of code. If none of the conditions are true, the Else block is executed. This type of statement is useful when you need to check several conditions based on a single variable or expression and perform different actions depending on the outcome of each condition.
It works by sequentially checking each condition. Once a true condition is found, the associated block of code is executed, and the remaining conditions are skipped. If no condition evaluates to true, the Else block is executed as a fallback. following are the expression of multiple if else statement in wincc vbscript
    If _condition1 Then
    ' Executes when condition1 is true
    ElseIf _condition2 Then
    ' Executes when condition1 is false, but condition2 is true
    ElseIf _condition3 Then
    'Executes when both condition1 and condition2 are false, but condition3 is true
    Else
    ' Executes when none of the above conditions are true
    End If

example of multiple if else statement in wincc vbscript

below is the multiple if else statement in wincc vbscript. in this script compare two value. comperison result depend on condition. value1 and value 2 is associated with tag 1 and tag 2. tag 1 and tag2 are tag for input output fields. condition is executed when button is pressed.
example of multiple if else statement in wincc vbscript
The script compares two values and uses conditional statements (If-Else) to execute different blocks of code based on their relationship. Below is a breakdown of the code:
  • Assigning values to value1 and value2: The script retrieves values from two tags, tag1 and tag2, using the SmartTags function. These values will be compared.
  • First Condition (If value1 = value2): If value1 is equal to value2, the message "Both values are equal" is displayed on the screen.
  • Second Condition (ElseIf value1 > value2): If the first condition is false, the script checks if value1 is greater than value2. If true, the message "Input 1 is greater than Input 2" is displayed.
  • Third Condition (ElseIf value1 < value2): If neither of the first two conditions are true, the script checks if value1 is less than value2. If true, the message "Input 1 is less than Input 2" is shown.
  • Fallback Condition (Else): If none of the above conditions are true (although unlikely in this case), the Else block will be executed, displaying "Unexpected case". This ensures that there is always some feedback displayed.

comment