Library code snippets
Evaluate string formulas in VB
If you've come to Visual Basic from Microsoft Access, you probably
miss the handy Eval() method. This method evaluates a supplied
string as though it were code. As a result, you could easily
evaluate mathematical expressions passed as a string, like so:iResult = Eval("(2 * 3) + 5)")
which fills iResult with the value 11.
To achieve this same functionality in Visual Basic, you've no doubt
resorted to complicated string parsing functions. Or perhaps you
added Access' DLL to your project, which may have seemed like an
awful lot of DLL for such a simple method.
Now, though, you'll be happy to know that you can use the
Eval() method without a lot of overhead. Microsoft provides this
functionality in its Script Control. This very lightweight ActiveX
component is available so you can add end-user scripting ability
to your applications. However, as a result, it also comes with
an Eval method. Adding this component to your project provides
very little overhead and gives you the ability to evaluate
mathematical strings.
To download this OCX, visit http://msdn.microsoft.com/scripting/
Select the Script Control link, then follow the Download
instructions. The following code shows the simple VB we added
to a command button's click event. The SC1 script control
evaluates a mathematical formula in the txtFormula textbox.Private Sub Command1_Click()
MsgBox txtFormula & " = " & SC1.Eval(txtFormula)
End Sub
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Did you know that you can also evaluate and even CREATE a complete function using this?
check this code:
Place a command button and a text box in a form. Add the Microsoft Script control (MSSC in my sample) Here's the code:
Option Explicit
Private Sub Command1_Click()
On Error GoTo myErrorHandler
MsgBox MSSC.Run("Check", Me.Text1)
myErrorHandler:
End Sub
Private Sub Form_Load()
Dim strPath As String
Dim lonFreeFile
Dim GetLine As Variant
Dim strLine As String
Dim m, SC
Dim strPrograma As String
strPath = "c:\script\isbn.txt"
lonFreeFile = FreeFile
With Me.MSSC
.Language = "VBScript"
.AllowUI = True
End With
Open strPath For Input As lonFreeFile
Do While Not EOF(lonFreeFile)
Line Input #lonFreeFile, GetLine
strLine = strLine & Trim(UCase(GetLine)) & vbCrLf
Loop
Close lonFreeFile
MSSC.AddCode strLine
End Sub
Here's the file conaining the function that's going to be evaluated (the file ISBN.txt mentioned in the source code)
FUNCTION CHECK(strVal)
L1 = Mid(STRVAL, 1, 1) * 10
L2 = Mid(STRVAL, 2, 1) * 9
L3 = Mid(STRVAL, 3, 1) * 8
L4 = Mid(STRVAL, 4, 1) * 7
L5 = Mid(STRVAL, 5, 1) * 6
L6 = Mid(STRVAL, 6, 1) * 5
L7 = Mid(STRVAL, 7, 1) * 4
L8 = Mid(STRVAL, 8, 1) * 3
L9 = Mid(STRVAL, 9, 1) * 2
If UCase(Mid(STRVAL, 10, 1)) = "X" Then
L10 = 10
Else
L10 = Mid(STRVAL, 10, 1)
End If
TOTVAL = L1 + L2 + L3 + L4 + L5 + L6 + L7 + L8 + L9 + L10
CHECK = ((TOTVAL / 11) = (Int(TOTVAL / 11)))
End Function
The function obviously no exists in the main code byt IT WORKS.
This thread is for discussions of Evaluate string formulas in VB.