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

eval() function in VB6 3

Status
Not open for further replies.

SimonSellick

Programmer
Nov 3, 2003
305
0
0
GB
Hi,

I need to add functionality to a VB6 console application to evaluate a string containing a numeric expression, using BIDMAS. For example, it should evaluate

"(3 + 1) * (2 + 4)" as 32.

Access VBA has an eval() function that does this. Does anyone know how to get at with VB6? I can assume that Access will be present on the target machine, so I'm hoping that there might be a DLL API that I can call. I would like to avoid shelling out to a Windows Script or similar because this is likely to be called a few hundred times per run.

Any assistance gratefully received.

Simon.
 
Surely (3 + 1) * (2 + 4) = 24, not 32

>likely to be called a few hundred times per run

So? You think a few hundred iterations will be slow?
 
The Eval function is stored in the MS Access Object Library so just set a reference to that library in your VB6 project.

I'm actually quite surprised this function isn't available in VB6. Never noticed it's not there before.

Ed Metcalfe.

Please do not feed the trolls.....
 
Thanks Ed! Have a star.

Strongm - if I could do sums myself I wouldn't need the computer! Quite right and thanks for the correction.
 
I'm lost - you don't want to use the Scripting library because of concerns about the number of iterations, but you are happy to use the much heavier Access library ... ah, hang on, you mention shelling; you don't have to do that. Here's the cheap scripting library solution:
Code:
[blue]Option Explicit
Private objScript As Object

Private Sub Command1_Click()
    MsgBox objScript.Eval("(3 + 1) * (2 + 4)")
End Sub

Private Sub Form_Load()
    Set objScript = CreateObject("MSScriptControl.ScriptControl")
    objScript.Language = "VBScript"
End Sub[/blue]
 
Yes, it was the shell I was trying to avoid, not the external call.

Does creating the script control create a new process (a WScript.exe or something)? If so, presumably I can instantiate it just once and dispose of it when the program terminates to avoid too much overhead. Also, was it available in Windows 98?
 
>presumably I can instantiate it just once

Yep. Hence the specific code example I provided, which does exactly that (once instance of the object for the lifetime of the form)

>was it available in Windows 98?

As long as you've got IE4 or later ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top