Help with code

A

Ayo

I wrote the following code hopeing to Enable a textbox "Me.[Extended Price]"
when its value is greater than 0 and disable it when its value is equal to 0.
Am I using the right Event here?

Private Sub Extended_Price_Change()
If Me.[Extended Price].Value > 0 Then
Me.[Extended Price].Enabled = True
ElseIf Me.[Extended Price].Value = 0 Then
Me.[Extended Price].Enabled = False
End If
End Sub
 
A

Allen Browne

That's not going to work.

Firstly, the Change event fires for each keystroke. At that time, the Value
is not yet updated, so the code won't do what you expect. You could use the
Text property instead of Value, or you could use the AfterUpdate event: the
Value is updated then.

The second issue is that when these events fire, the Extended_Price box has
the focus. Since it has focus, the attempt to set its Enabled property to
False will fail. The attempt to set Enabled to True will do nothing, since
it cannot have focus unless it is enabled.

The third issue is that this is probably the wrong way to design the
database. [Extended Price] is typically the Quantity times UnitPrice. If so,
it should, you should not have a field for this in your table: it should be
a calculated query field. And if it is a calculated field, it cannot be
changed, so these events cannot fire.

Here's some reading on how to set up the table:
Calculated fields
at:
http://allenbrowne.com/casu-14.html

Then if you want an example of how you can *appear* to let the user type
into the calculated control and store the value into the real fields, see:
Enter text in calculated controls
at:
http://allenbrowne.com/TechniqueEnterCalcText.html
There's a sample database in that link to download and see how it works.
 

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