Posted on 2000-04-24
Last Modified: 2010-05-02
I need to know how can I convert string to an arithmetic expression

For example I have this variable:


and i want to have the answer shown in a msgbox
Question by:DImirC
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +5
LVL 12

Expert Comment

ID: 2745077
This is much harder than it looks.

If your expression is always of th form A+B-C then you can simplify things. But if you want to evaluate any random equation you're looking at a lot of work. You'ld have to generate a parser that will scan the line of text and identify numeric values and operators. You'll then need to perform the indicated operations.

In the old days of BASIC you could place that formula on a numerical input statement and the BASIC itself would parse it out. However it could be corrupted by entering invalid characters, etc.

The relative complexity comes when you start taking all the permutions into account. Are the only functions you need + & -? Or do you need * and / as well? How about trig functions, Sin(), Cos(), Tan()? Powers? Are your numbers only going to be integers? or do you need to handle reals? How about fractions? Are you going to allow $ signs and ,'s? This can get really messy in a hurry.

Search the PAQ's, I've seen code for this posted before.


Expert Comment

ID: 2745165
try the VAL statement

Author Comment

ID: 2745218
if I use this:




I need the result of the arithmetic expression
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Expert Comment

ID: 2745230
Uh, no, the VAL() function will just give the number until a non-number character is found. So

Val("10 + 2 - 5") will just return 10 and not 7, the correct answer.

Will this just be for Integer numbers, or will you be using Floating Point numbers as well? Will you be using Parenthesis "()" in the formula as well?

This is kind of a complex thing if you want to include support for floating point numbers and parenthesis.

Otherwise you could do a loop that reads the number until it reaches a plus "+", minus "-", multiply "*", or divide "/". Then parse it into an array and sort the array so that the multiplide part is done first, then the division, then the plus, and then the minus.

Say, this isn't for a school assignment, is it? ;)

Expert Comment

ID: 2745234
Try parsing the string:

dim a$
dim b$
dim c$
a$ = mid$(abc, 1, 2)
b$ = mid$(abc, 3, 1)
c$ = mid$(abc, 6, 1)

ans = val(a$ + b$ - c$)

This is the only way.
There is no other way.

Author Comment

ID: 2745248
tsa2 , the expression can have more numbers, so I dont know how many numbers are there going to be...
LVL 28

Accepted Solution

AzraSound earned 50 total points
ID: 2745250

example there that parses the expression and calculates
LVL 32

Expert Comment

by:Brendt Hess
ID: 2745375
If you can use the MS Scripting control in your VB programs, you can use the EVAL and EXECUTE functions from VBScript.

for a discussion of the scripting control.
LVL 28

Expert Comment

ID: 2745433
If you need only "+/-" operations (order is not important), it's not a hard code. Using brackets, multiplication and dividing is more difficult.

Expert Comment

ID: 2746273
if ur app is connected to a database you can use select statement
to evaluate this string , using recordset you can access the answer
to the expression

ur code may look like this

ABC = "10+2-5"

sqlstmt = "select" & ABC & "from DummyTable"

set mdb = OPendatabase.....
set mset = mdb.OpenRecordset(sqlstmt,.....)
if not mset.eof then
   answer  = mset.Fields(0)
end if

DummyTable must exist in your database , this table could only have one field and one row .....

hope this helps
LVL 28

Expert Comment

ID: 2746800
I thing, you have to change your comment to answer - class from your link do many math operations, it's new and I just found two links at this class at another VB sites - it means that code is good and popular.
LVL 12

Expert Comment

ID: 2747041
Just use the "Accept comment as answer" option above his response...


Author Comment

ID: 2749032
thanx AzraSound

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question