Default value depending on previous field

G

Guest

I have a Costing form with a several lines that include fields for Crates and
their Costs.
If a user enters a Crate on line 1 (Crate1) the related cost field
(Crate1Cost) should then be populated with the amount of 60.00.
However, on any lines that are NOT used, the cost should remain at the
default of 0.00.
In other words, the cost fields should only come alive if the preceding
Crate field on that line has been populated.
In an earlier post somebody suggested the following code, but unfortunately
it has no effect:
Private Sub Crate1_AfterUpdate()
If Not IsNull(Crate1) Then
Crate1Cost = 60
Else
Crate1Cost = 0
End If

End Sub

I'll be grateful for your comments
Many thanks
CW
 
K

Ken Snell \(MVP\)

That code appears to be a reasonable approach for this problem. Tell us what
"has no effect" means -- no value is written into the Crate1Cost control?
wrong value is written into the control?
 
K

kingston via AccessMonster.com

I don't think that the code was meant to be used literally. Go into design
mode of the form. Click on the Crate textbox control and open its properties
window. Select [Event Procedure] in the After Update event and click on the .
.. button at the end. Now, enter the code provided but change Crate1 and
Crate1Cost to the actual names of the form controls. It might help to use
"Me." to automatically display the control names.

Another way to do this is to set the cost control's Control Source property
to something like this:
=IIF(IsNull([Crate]),0,60)
 
G

Guest

The only thing wrong with the code you posted is that the object references
are not qualified. It should work. It is a very common way to do what you
want to do. Have you tried tracing the code in debug to see if the event is
firing?
 
G

Guest

Several responses to my post already! Everybody is up and at it today!!

Ken, the "non-effect" is that when you enter the Crate field and input
something (it's usually a simple decription of the item to be crated, but
this has no relevance, is not hooked up to anything else), and then you leave
the field, the value on the Cost field remains at 0.00, even if you tab
through it and carry on into another line.
By virtue of the fact that the Crate field has been populated, the Cost
field should have come up with 60.00 (to save people have to type it in
manually).
Looking fwd to any further ideas...
thanks
CW
 
K

Ken Snell \(MVP\)

Let's make a small change in the code (assuming that the controls are named
Crate1 and Crate1Cost):

Private Sub Crate1_AfterUpdate()
If Not IsNull(Me.Crate1) Then
Me.Crate1Cost = 60
Else
Me.Crate1Cost = 0
End If

Now, let's also be sure that the form knows you have this code attached to
the On After Update property of the Crate1 control. Open the form in design
view, click on the Crate1 control, open the Properties window, click on
Event tab, and look at the On After Update property. Do you see [Event
Procedure] in that box? If not, the form does not know that you've added the
above code procedure. In that case, click in the box, select [Event
Procedure] from the dropdown, and then click on the three-dot button at far
right of box. That will connect the code to the property.
 

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