PDA

View Full Version : Is it possible to add a new text field box to a form to perform a calculation?



wedd
01-21-2011, 12:18 AM
Is it possible to add a new textbox to an existing form to perform a calculation? If so, what steps will I have to consider when doing this? I have an existing textbox on my form but when I try to include its name into a calculation, it somehow isn't recognisable as the name# error and a messagebox appears that the textbox isn't recognised. Its strange because the textbox name is in my database table. Any reasons why this could be happening? Are there any possible solutions to resolve this?



Thanks for your suggestions:friends:

will1128
01-21-2011, 11:24 AM
Do you want to include the name of your textbox in the calculation or the value of the textbox in your calculation? Is the name from a table?

wedd
01-21-2011, 02:59 PM
will1128, I want to include the name of a textbox into a calculation. And yes, the name is from a table. Can it work if the name of the textbox wasn't from a table?

jrajul
02-18-2011, 02:08 PM
I am assuming this is an unbound textbox that you want to add.

Access will not let you name a control as a number. If you try to, it will add "Ctl" infront of it.

Say you have a textbox that you named "14" but it is now named "Ctl14" and you want to put a textbox to display 14 - times 4.



Create the new textbox and place it where you want to be
Name it. For this example, I will name it "YourNewBox"
In the form's "current" event type

Private Sub Form_Current()

YourNewBox = CDbl(Right(Me.Ctl14.Name, 2)) * 4

End Sub



Here we added a textbox, renamed it and used it to multiply the name times 4.

The Right() function specifies the part of the name that I want to work with. In the example, I only want the last two characters.

The Cdbl() function turns something into the "double" data type. This makes sure that VBA treats digits as numbers and not as words.

If the textbox is in another form, then you may have to use a notation like this :

forms!formName!.Controls("textboxname").name

AND both forms must be open, at least in design view.