Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Instant update of calculated Text Box in Form

I have a form which contains a Text Box which calculates a value from a user input text box on the same form.

The trouble is it only does this once the user has stopped typing and clicks somewhere else. I assume I have to put some sort of expression into the correct event property of the user input text box but dont know an expression to force uppdate of another text box. Does anyone know?
1 Solution
Other controls will update when the focus changes from control to control.

It is possible to do an update through code, by placing it on the OnKeyDown event.
However, if you're using calculated fields, this will cause errors that will have to be managed too.
Another event that can do the trick is the OnChange. Use anyway for your calculation the Nvl function to avoid calculation problems.
KrobarAuthor Commented:
This seems alot more difficult than I thought it would be (I upped the points too). I tried entering simple piece of code but it does not execute when I expect.

For example if you have two text boxes called Text1 & Text2 and I wanted to update Text2 (which has no control source) using the following "Text2.Value = Text1.Value * 5" every time a key is pressed in Text1, how is this done?
I can't find a way to do it whilst you still have focus on text1.

Having the focus on the control that is part of a calculated field stops the events from firing - so you can't use the Timer event on the form either.
The problem here is that the text box has two or three properties describing the content.
  .Value is changed once the entry in the box has been validated (and converted sometimes)
  .Text is only available while the control has the focus

To update Text2 with Text1 on the fly, use:

Private Sub Text1_AfterUpdate()
    Me.Text2.Value = Text1.Value * 2
End Sub

Private Sub Text1_Change()
    Me.Text2.Value = Val(Text1.Text) * 2
End Sub

If Text1 is formatted as short time, you will see this:

1   -->   2
12   -->   24
12:30   -->   24
(leave)   -->   1.0416666   (= 25/24 or 1 day and 1 hour)

You see that the information is quite different. Text is a String property, Value will depend on the formatting and/or on the underelying field (string, number, date/time, etc.)

Good Luck

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now