To open to a new record, try in the Open event:
Me.Recordset.AddNew
From what I understand, there is a hidden text box named tgal. You called
it a hidden field, but tables have fields; forms and reports have controls
that may or may not be bound to fields. Somehow a number is ending up in
that text box. I've lost track of how the number is ending up in tgal, but
rather than putting the number there, could you just put it into FuelAmount?
The thing about VBA is that code runs because it is called. If you update a
text box, you can have code in the text box After Update event to perform a
calculation or whatever. If you do not update the text box (for instance,
when you scroll through the records) the code will not run. If you need to
view the calculation result as you navigate through the records you will
need to put code into the form's Current event, which runs when you go to
another record, including a new record. However, sometimes code will
produce an error at a new record because there are no values with which the
code can work, so it tries to divide by 0 or whatever. So you may need to
run the code in several places. To do that, you can create a function to
perform the math, and call the function rather than copying the code. If
the code is not to run at a new record, the form's Current event could be:
If Not Me.NewRecord Then
YourFunction
End If
In short, one trick with code is figuring out when to run it. In your case
you want to run code to perform a calculation, then you want to store the
calculation result rather than the input that determines the result. Not
the choice I would have made. As I said, you are entering the various meter
readings, gallon readings, etc. anyhow, so why not store them. But if you
decide to go that route, you have to find a time to run the code. Since you
are controlling the data entry you could have something like this in the
MeterReading2 text box After Update event:
Me.FuelAmount = Me.txtMeterReading2 - Me.txtMeterReading1
In the After Update event for AfternoonGallons:
Me.FuelAmount = Me.txtMorningGallons + Me.txtAfternoonGallons
As long as the data entry proceeds in a particular order (MeterReading2
*after* MeterReading1), and does not occur in two places (Readings and
Gallons, for instance), you should be OK. Again, not the choice I would
have made, partly because I like to think others will one day to the data
entry. There are ways to assure the data is entered in only certain text
boxes. For instance, a Calculation Method option group could cause one pair
of text boxes to show if Gallon Method is selected, or another pair if Meter
Method is selected.
The form's After Udate event will not work for this sort of thing. After
the record is saved you would be going back and changing the record again,
which means when you navigate to another record the After Update event will
run again, and you would be in a loop.
With unbound text boxes you probably want to use the form's Current event to
clear them so they are blank when you go to a new record.
CJ-Hewitt said:
BruceM said:
Comments inline.
Do I understand that you are storing the calculation result rather than
the
various readings (meter, gallons used)? You have to hope there are no
data
entry errors, but I suppose you could trap for errors that are outside of
an
expected range.
This is the goal, and the data entry is by me. I use the top part of this
form (all these are unbound fields designed specifically to calculate
gallons) for calculation only. I then take the gallon number it produces
(because I cannot figure out how to transfer that automatically) and enter
it
in the bound field [FUELAMOUNT].
Do you mean it is an unbound form, and that the submit button opens a
recordset and updates it, or something like that?
it is a bound form, and I apologize for not knowing what to call the type.
When I start the form up, it would show 1 of 775, and I have to hit the
"new
record" button to get to a blank part of the form.
BTW if you have a method of opening this up to a blank section, that would
be grand!
You can use the basic code I suggested, except that something such as:
Nz(Me.Entry1,0) etc.
would be a reference to an unbound text box, and that at the end Fuel1 is
the only bound field. It may make it easier to follow if you use the txt
prefix to signify a text box:
Nz(Me.txtEntry1,0) etc.
However, don't worry about the complexity of the report. The computer
processes millions of instructions per second, and can handle this sort
of
calculation instantly from your vantage point. Again, you may do well to
store the readings. If there is an anomaly some day it may be difficult
to
see where it came from if you only have the calculation result. You're
going to type the readings anyhow, so why not save them?
Oh its not the computer I am saving, it would be my own sanity.
You've lost me here. Do you mean you could have meter readings and
gallons
for the same piece of equipment in the same record?
Meter readings would be gallons, just means I have to do the math rather
than data entry.
and no, it would be either one or the other for a piece of equipment per
record.
Just another focus on the [tgal] hidden field I've created.
This houses the number I've transferred, and this field is what I want
[FUELAMOUNT] to pick up.
I've got all the legwork pointing to [tgal], just need to get that last
hop
over to the bound field [FUELAMOUNT]
How do I make [FUELAMOUNT] = [tgal] during an update?