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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Quotes from Variables 1

Status
Not open for further replies.

Hotstepper9369

Technical User
Nov 26, 2007
6
0
0
US
Hello All,

I am trying to run a script that build some script dynamically. My problem is that I am writing values to variables and when the If statement is built I am getting errors. An example would be as follows:

strIfStatement = This.Blank

If strIfStatement Then

MsgBox "Worked"

End If



I am getting a Type Mismatch Error, I am assuming because it brings back strIfStatement as "This.Blank" when what I really need is This.Blank.

Could somebody please advise as to how I can do the above without having my variable or whatever wrapped in quotes?

Thanks in Advance
H
 
If I understand what you're trying to do then you'll need to build your entire If statement and then execute it as a whole using Execute

x = 1
strIfStatement = "If x > 0 Then WScript.Echo ""It worked"""
Execute(strIfStatement)

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi DM4Ever,

This works well. Thanks for the input.

Next challenge...........

My sample was a humble one line Message Box. What if I wanted an action that was 10-15 lines? Example is as below

If strIfStatement Then

MsgBox "Worked"
This.Value = "12345"
This.Flagged = False
This.ReadOnly = True

End If



How would I represent this? I appreciate your feedback. I can do some things in VBScript very well, but same basics kill me LOL.

Thanks Again
H
 
It would be the very similar...you just keep building your string to match what you want and then execute it. Use WScript.Echo as you're building your string to see if it outputs the same way you would write it if you were hard coding the code...then execute once you have it down.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Is WScript.Echo a line break? Would my script look like this?

strIfStatement = "If strIfStatement Then WScript.Echo MsgBox "Worked" WScript.Echo This.Value = "12345" WScript.Echo This.Flagged = False WScript.Echo This.ReadOnly = True WScript.Echo End If"

Execute(strIfStatement)

Or even this......


strIfStatement = "If strIfStatement Then
WScript.Echo MsgBox "Worked"
WScript.Echo This.Value = "12345"
WScript.Echo This.Flagged = False
WScript.Echo This.ReadOnly = True
WScript.Echo End If"

Execute(strIfStatement)


Again, forgive my naivety here, but I want to be sure I have the syntax right.

Thanks Again
H

 
No...more like this

Code:
strIfStatement = "If strIfStatement Then" & VbCrLf & _
				 "WScript.Echo MsgBox ""Worked""" & VbCrLf & _
				 "WScript.Echo This.Value = ""12345""" & VbCrLf & _
				 "WScript.Echo This.Flagged = False" & VbCrLf & _
				 "WScript.Echo This.ReadOnly = True" & VbCrLf & _
				 "WScript.Echo" & VbCrLf & _
				 "End If"
WScript.Echo strIfStatement

but why are you trying to do this in the first place? There's probably an easier and better way to accomplishing what you want...mind telling us what you have in mind along with the current code you are using?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Sure,

I have a solution for a customer that calls on business rules for validations. Dates being within a certain range, dollar values being under a certain amount.........that sort of thing. Rather than coding every field Iwanted to store the rules in a table and then build my If statement and the action behind it dynamically based on the rules that exist for a particular field in a table. This is written in a solitary function and then the function is called.

If you want further clarification I can send the whole script, but I will also try what you just posted and see what happens.

Again, thanks for your help

H
 
Thanks Again DM4Ever. This worked very well, and I am now cooking with gas. It was much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top