UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Calculated Field Result Does Not Appear In Field, Access 2010    
 
   
Gold
post Mar 13 2018, 03:56 PM
Post#1



Posts: 19
Joined: 13-May 03



I'm a beginner. I don't do much in Access. However, I have calculated a field before, but it will not work now. I have seen other posts about Access 2010 saying the same thing. I thought I would verify.

I work in research. I need a staff member in the field to enter 4 values in different fields (oneb, onec, twoa, threeb). The Access database will then calculate the total time (foura):

This is my code:


Private Sub foura_Click()

If onec = 1 Then oneb = oneb - 1

foura = (oneb * twoa) + threeb

End Sub

I do not get an error in the Vb window. However, my total value does not appear. It did once, but will not again.
I should also let you know that foura is a field in my table. Is that right? Actually, I tried a new field in the form and the same issue.

Thank you.
Go to the top of the page
 
RJD
post Mar 13 2018, 04:01 PM
Post#2


UtterAccess VIP
Posts: 8,357
Joined: 25-October 10
From: Gulf South USA


Hi: Are you trying to get the result to show in a form? If so, is the result control on the form bound or unbound to a field in the source table/query? (The calculation result should not be bound, it should be calculated when desired.)

HTH
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 
doctor9
post Mar 13 2018, 04:12 PM
Post#3


UtterAccess Editor
Posts: 18,324
Joined: 29-March 05
From: Wisconsin


Gold,

First, if you REALLY think you need to use VBA, then this should be a Function, not a Subroutine:

CODE
Public Function foura(oneb As Variant, onec As Variant, twoa As Variant, threeb As Variant) As Variant

    If onec = 1 Then oneb = oneb - 1

    foura = (oneb * twoa) + threeb

End Function

NOTE: There's a button above the text editor that you can use to wrap the selected text in Code tags, which preserves your indents and consecutive spaces.

Then, the call to the function should be an expression in the query that drives the form, not in the form control's Click event. Then, bind a form textbox to the expression. Since this is a function you'll likely need in more places than just this form, the function code should be placed in a new code module, not the one attached to the form.

Hope this helps,

Dennis

--------------------
(;,;) Li'l Cthulu says: Please talk about what you're trying to do, as well as how you're doing it.
Changing your real table name to "Table1" and your real form name to "Form1" in your posts makes it more difficult to understand what's going on, not easier.
Guidelines for Posting Questions
Go to the top of the page
 
RJD
post Mar 14 2018, 10:28 AM
Post#4


UtterAccess VIP
Posts: 8,357
Joined: 25-October 10
From: Gulf South USA


Hi: Pardon me for jumping back in, but this could be done directly on your form in an unbound control...

=IIf([onec]=1,[oneb]-1,[oneb]) * [twoa]) + [threeb]

(untested)

Although you say you store the result in the table, this is not likely a good idea. You should rarely store in the table a calculation result that uses only the fields in the record. And this does not seem to be one of these occasions. Just calculate the value when you need it. This avoids errors when an underlying value changes but you don't do the calculation again.

Just a thought ...

HTH
Joe

--------------------
"Each problem that I solved became a rule, which served afterwards to solve other problems."
"You just keep pushing. You just keep pushing. I made every mistake that could be made. But I just kept pushing."

Rene Descartes 1596-1650 (Mathematician and Philosopher)
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    20th June 2018 - 09:40 PM