Calculating and displaying infomration on the form.

B

Brian W

I have a form where I have a number of totals I would like to calculate (see
below for details). The calculations are done in VBA (because they pull
information from various places) and placing the return value into a textbox
on the form. I am trying to get the calculations to update whenever a field
is changed on the form (there are a number of textboxes). I can get the
onChange event to fire correctly and process correctly. I am pulling the
values from some of the textboxes on the form to pass to the calculation
routine. To do that I have to use textbox.value property (to use .text, the
textbox has to have the focus). the problem is that I'm not getting correct
results this way, the value doesn't update as it is entered, you have to
begin entering in the next box to update the previous one. Is there
something I can do so that this will calculate correctly AS the person types
so that the calculation is correct and updating in real-time (as they type).

Thanks in advance for any help.

Brian w.

Calculations
txtCostA.value = CalculateA(txtbox1.value, txtbox2.value)
txtCostB.value = CalculateB(txtbox3.value, txtbox4.value)

function CalculateA(viNumber1, viNumber2) as single
CalculateA = viNumber1 * viNumber2 * otherValue 'otherValue pulled from
another unrelated table via sql
end function
 
R

Rob Oldfield

You need to be using the text property where you're referring to the control
that is being changed, so it would be something like....

CalculateA(me.txt1.text,me.txt2) in the change event of txt1 and
CalculateA(me.txt1,me.txt2.text) in the change event of txt2
 
B

Brian W

Problem is I get an error message from the code if I refer to a texbox using
..text and the textbox doesn't have focus.

BW
 
R

Rob Oldfield

Correct. But if you're running the code in the change event of, say,
txt1... then that control must have the focus. The change event can't
happen in any other way. Which is why you can use txt1.text in the change
event of txt1, and txt2.text in the change event of txt2.
 
R

Rob Oldfield

Again correct. In that case txt1 can be changing so you need to use the
Text property. txt2 cannot be (and also doesn't have the focus) so you need
to use the straight value property.

To repeat...

CalculateA(me.txt1.text,me.txt2) in the change event of txt1
CalculateA(me.txt1,me.txt2.text) in the change event of txt2

What's the problem with that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top