How can I enter =2+2 in an access field?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to enter data in a field of a form the same way you enter data
in a cell in Excel. If I enter =2+2 Enter the field should display the
result 4.
 
It will if you put that as the field's source. What is it you are trying to
accomplish? Why would the end user need to enter a mathmatical formula and
store the result? Typically, a normalized database would store items in
separate fields. In other words, you'd have two different fields in which
the user would enter the numbers, then you'd display the calculated result
in a third field.

Rick B
 
The end user needs to enter a mathamatical formula because of the way she
receives the data from the production floor. Sometimes it is just one
number, sometimes 2 or 10 there is no set number. It really need to be one
field in my form and table. We are trying to avoid having to go to the
calculator all the time while we are trying to enter data in Access.
 
Sounds like you need to write a function to check how many numbers and
decide which math formulas to use. the output of the function would be the
final value.

Make a public function in a module and you will be able to call it from hour
form or query

Tom
 
Access's Eval() function may provide the necessary result, but will require the
use of VBA code (or careful implementation of a macro) to assign the returned
value to the field.
 
Simply put the following code in the after update of the field:


if isnull(me.MyControlName) = false then

me.MyContorlName = eval(me.MyControlName.Value)

end if

Now, if you enter:

((237 + 2) * (2)) ^ 2

You get:
228484

You can well enter just about any expression.....
 
Instead of using formulas, try using a loop to read all numbers, one by one,
and adding them as you input them. Try this:

Private Sub Command2_Click()
Dim x As String
x = " "
While Len(x) <> 0
x = InputBox("Value ?", "Test", "")

If (x <> "") Then
If IsNumeric(x) Then
Text0.Value = CDbl(Text0.Value) + CDbl(x)
Else
MsgBox "Invaliud number", vbOKOnly + vbExclamation
End If
End If
Wend
End Sub

Change:
- text0 for the name of your TextBox
- Create a button and change the Command2 for the name of your new button

It will be in loop while you don't click cancel or enter an empty value.

Let me know if it worked

Take Care
Mauricio Silva
 
I have a continuous Subform in a main form that I'm trying to get this to
work in. I don't know what I'm doing wrong. My code looks like:

Private Sub Length__in__AfterUpdate()
If IsNull(Me.Length__in_) = False Then

Me.Length__in_ = Eval(Me.Length__in_.Value)

End If

End Sub


And I keep getting an error:

The value you entered isn't valid for this field.
For example, you may have entered text in a numeric field or a number that
is larger than FieldSize setting permits.

My General Setting for the Data Type: Number are:
Field Size = Single
Decimal Places = 4
Default Value = 0
Required = No
Indexed = No

Are there some different setting I can use that will make this work?
 
Back
Top