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

anything like javascript eval()?

Status
Not open for further replies.

mackey333

Technical User
May 10, 2001
563
US
javascript has a method called 'eval()' which basically takes a string and executes it as code, for example:

strCode = "intCount = 5"
eval(strCode)

now intCount is set to 5..is there anything like this in vb? -Greg :-Q

flaga.gif
 
There is a VB function called Shell() that you can use to run an executable file (EXE BAT COM). You just specify the path as a string.

Example: Shell("C:\WINDOWS\NOTEPAD.EXE")

This function is not available in VBScript, though, so if you want to use that you'll have to build a shell (no pun intended) function arounded it that you can instantiate and execute on your target box. Keep in mind this opens up the a whole box of security vulnerabilities if you do it, though. There are other things to consider about the Shell() function too. Here's a link to some good info on it:


Once again, I think the text editor may break up this URL, so make sure you copy the whole thing onto your browser's address line.
 
I'm a doofus. I read your description but didn't look at your example before replying (definitely my bad). You are looking for something that runs inline code, not something that calls a separate program. I am not aware of anything in VB that does that (which does not by any means prove that there is not one out there).

Out of curiosity, what are you trying to use it for? I can see where it might be handy for creating dynamic code (by building a command string based on user input), but I can't really think of a good idea WHY you'd want to build dynamic code like that since if you are building from user input then you know what's being input and could write "fixed" code to handle it yourself.
 
thats not really what i'm looking to do, here is what i am trying to do, the first thing i thought of was eval() so thats what i asked about:

public function clearTxtBoxes(strTarget)

with strTarget

.txt1 = ""
.txt2 = ""

ect....

end with

end function


then....

clearTxtBoxes("frmMain")

and from a different form:

frmMain.clearTxtBoxes("frmOtherForm")

i get an object expected on the .txt1 = "" -Greg :-Q

flaga.gif
 
so i figure the prob is with the quotes, so if i had eval i could do something like:

strTarget = "with " & "strTarget"
eval(strTarget)

to get the with statement to work -Greg :-Q

flaga.gif
 
oops, make that:

strTarget = "with " & strTarget
eval(strTarget) -Greg :-Q

flaga.gif
 
It looks like what you are doing (correct me if I am wrong) is trying to create a generic function that, when it is passed a reference to a Form object, clears the text boxes called "txt1" and "txt2" on that form. Is that right?
 
yes, the reason i'm doing this is because there are 47 text boxes that display info on the main form, these boxes are disabled all the time. when the user clicks the edit button, a new form opens up, the text boxes are then cleared and filled in the same way (this function obviously doesn't work either), on this form the user can edit and update the data. the text boxes have the same names on both forms. -Greg :-Q

flaga.gif
 
Unfortunaely you can't directly access a VB form by its "name" this way. You can, however, do this by looping through the Forms collection checking each object for that attribute (see below)

'===================================================

Sub ClearTxtBoxes(strFormName As String)

' Declare variables
Dim objForm As Form

' Loop through the Forms collection
For Each objForm In Forms
If objForm.Name = strFormName Then
With objForm
.txt1 = ""
.txt2 = ""
End With
End If
Next

End Sub

'===================================================

Hopefully this helps. :)
 
Nagrom is right that you cant pass the name of a form this way, but you can pass a refrence to the form using the Me keyword. These changes should fix it:

Code:
public sub clearTxtBoxes(FormToClear As Object)

     with FormToClear   
          .txt1 = ""
          .txt2 = ""

           ect....
     
     end with

end sub

then....

clearTxtBoxes Me 

and from a different form:

frmMain.clearTxtBoxes Me

HTH Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
Nice one Ruairi, but I actually wrote some code VERY similar to this in my last programming job... I'd recommend some error trapping in there to ensure that the textbox exits - maybe by calling a seperate function to handle clearing the text control, and having that function check the control exists before it touches it. You can see if the control exists by looping through the Form's Controls collection.

It might slow the code a little bit, but insurance is worth it!

And, just because I'm curious, why didn't you declare the Form as a Form in your function?

Martin

 
See if this is helpful to you. if you just want to clear textboxes.
2 ways.

Ist Method.
-----------
private sub Refresh_Click()
Set gForm = me
ClearControls
end Sub

any module....
----------------------------------------
Option Explicit
Public gForm as Form

Public Sub ClearControls()
Dim Ctrl As Control
For Each Ctrl In gForm.Controls
If TypeOf Ctrl Is TextBox Then
Ctrl.Text = ""
End If
Next
End Sub

+++++++++++++++++++++++++++++++++++++++++++++++++++
2nd MEthod
-----------


Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long
Public Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function


Shakeel
SyedShakeel@hotmail.com
 
Hi, going back to the original question. There's a Script Control in MSDN. (msscript.ocx).It has the EVAL method originally asked for It is located in
You have to add the control to the form. Here's a code example

dim X
X= Me.ScriptControl1.Eval("2+2")
' X will have a value of 4
'Hope it helps...
 
Hi, going back to the original question. There's a Script Control in MSDN. (msscript.ocx).It has the EVAL method originally asked for It is located in
You have to add the control to the form. Here's a code example

dim X
X= Me.ScriptControl1.Eval("2+2")
' X will have a value of 4
'Hope it helps...
 
mmilan,
Declaring the variable As Form would work just as well as declaring it As Object, either way should function the same behind the scenes. They both just pass the form's window handle (hWnd). My using object rather than form was totally arbitrary.
Ruairi

Could your manufacturing facility benefit from real time process monitoring? Would you like your employees to be able to see up to the minute goal and actual production?
For innovative, low cost solutions check out my website.
 
Ruairi,

Cheers!

I wasn't trying to question your decision, just thought you might know something I didn't, and I'm always keen to learn!

Martin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top