Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute the condition stored in variable? 1

Status
Not open for further replies.

PankajAgr

Technical User
Jul 30, 2003
52
IN
Hi Guys,

Is there any way to execute the condition stored in variable. Like- Variable 'strVar1' stored the following condition-

strVar1 = "iif(Text1.enabled = 'true','Ok','Invalid')"

I want to execute the this condition & resultant value should be display in anywhere.

I want the command to execute in that way-

Text2.Text = ExecuteCondition(strVar1)

Based on result either it will display "Ok" or "Invalid" in Text box.

Important point is that condition string can be anything so it is not possible to create a function.

Please suggest me some solutions or alternate.

Thanks in Advance

Pankaj


Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
Here is an example of how you can do this.

Create a Form with 2 text boxes and 2 command buttons with their default names ( Text1, Text2, Command1, Command2) then copy and paste this code in to test it. this is the only way to multiple condition testsing with one function.


Private Sub Command1_Click()
Text2.Text = ExCon(Text1, "Text", "Valid", "InValid", "Hello")
End Sub

Private Function ExCon(Obj As Object, ConChk As String, _
ValTrue As String, ValFalse As String, _
Condition1 As String) As String

If ConChk = "Enabled" Then
If Obj.Enabled = True Then
ExCon = ValTrue
Else
ExCon = ValFalse
End If
Exit Function
End If

If ConChk = "Text" Then
If Obj.Text = Condition1 Then
ExCon = ValTrue
Else
ExCon = ValFalse
End If
Exit Function
End If

End Function

Private Sub Command2_Click()
Text2.Text = ExCon(Text1, "Enabled", "Valid", "InValid", "")
End Sub


You can put as many conditions like Condition1 in there and as many ifstatements to test the ConChk (The entity of the object to test for)

hope this is helpfull.

RaVeN Coder
 
Hi RaVeNCoder,

You misunderstood my this point -
"Important point is that condition string can be anything so it is not possible to create a function."

Your code assuming it always check the enable or disable of the control but in my case condition can be anything like-

1. strVar= "iif(var1='ABC','True','False')" && Assumes var1 variable will be declare before executing this statement

2. strVar= "iif(var1=Text1.Text,'Match','Mismatch')"

I want to know how to excute the condition that is stored in variable.

Thanks

Senior Software Engineer,
Infotech Enterprises Limited
Hyderabad, Andhra Pradesh, India.
URL :
 
But My code doesn't allways check for text1.enabled
you can code anything in there.
Like say you have a text box called text5, now you wanna check to see if that text box contains the text "Shoes" then you do this.

Result = ExCon(Text5, "Text", "Valid", "Invalid", "Shoes")

Because of the way my code is written.
Check the second if statement.

you can put as many if statements in there you want, the ones I put in was just an example.

if you wanna check the height of a picture box (Picture1 for ex.)

Put this code in the function aswell:

If ConChk = "Height" then
If Obj.Height = NumCon1 then
ExCon = ValTrue
Else
ExCon = ValFalse
End if
Exit Function
End if

now Change the Function to say :

Private Function ExCon(Obj As Object, ConChk As String, _
ValTrue As String, ValFalse As String, _
Condition1 As String, _
NumCon1 as Long) As String

Then just use the same funtion to do 3 DIFFERENT Checks.

This is the only way it can be done. I know it is going to be a lot of coding. But what do you expect you are trying to create a language within language. So you will have to cater for all the scenario's ex. Width, Height, Text, Enabled, Zorder, ext.

Believe me it is the only way.

Or If you are Trying to Insert A command in a text box at run time like say you are running your proggie and you want to manually type that code in a textbox and then execute the string that is in the textbox then you have even more work to do.
What you need to do then is parse the string into the individual words then have to put in an enormas amount of If statements or HUGE case statement to cater for all the vars you are gonna use and ones the user might think of useing and the or and and statements they might use. That is gonna be a mission and a half because then you are creating your own little programming language within Visual Basic and believe me when i tel you that is hard, I'm still trying to write my own language in assembly and i've been doing that for the last 5 years and i'm still busy.
 
Given some (pretty minor) restrictiosn, this is quite possible.

Here are the restrictions:

1) Variables that you might use in the condition must be declared in a class module
2) Variables and controls must be fully referenced (e.g Form1.Text1.Text rather than just Text1.Text)

Oh, and since we leverage VBScript to do this, and VBScript doesn't have a native IIF function, we have to add this as a function to VBScript

So here's an example.
Firstly, add a referenc to the Microsoft Script Control.
Secondly, add a Class Module to your project, and drop in the following complex code:
Code:
Option Explicit

Public var1 As String
Now add a form (if you haven't already got one) with a command button and a couple of textboxes, and drop in the following code:
Code:
Option Explicit
Private wsh As ScriptControl
Private myvars As Class1

Private Sub Command1_Click()
    
    myvars.var1 = "ABC"
    Text1.Text = "Not ABC"
    
    Text2.Text = ExecuteCondition("iif(form1.text1.enabled=true,'Ok','Invalid')")
    MsgBox ExecuteCondition("iif(myvars.var1='ABC','True','False')")
    MsgBox ExecuteCondition("iif(myvars.var1=form1.text1.text,'Match','Mismatch')")
End Sub


Private Function ExecuteCondition(ByVal strStatement As String) As Variant
    strStatement = Replace(strStatement, "'", """")
    ExecuteCondition = wsh.Eval(strStatement)
    
End Function

Private Sub ExtendVBS()
    Set wsh = New ScriptControl
    wsh.Language = "vbscript"
    wsh.AddCode "Function IIf(expr, truepart, falsepart)" + vbCrLf + "   IIf = falsepart" + vbCrLf + "   If expr Then IIf = truepart" + vbCrLf + "End Function"
    wsh.AddObject "form1", Form1, True
    Set myvars = New Class1
    wsh.AddObject "myvars", myvars, True
End Sub

Private Sub Form_Load()
    ExtendVBS
End Sub
 
Hi Strongm,

I got your point & solve my problem also.

Through VB script I can execute the statements.

Thanks for this valuable suggestion.

* for you.

Regds,
Pankaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top