If statement, wrong result?

D

Dave Elliott

both textboxes are on the main form; Pay and Bal
Code is being run on the current event of main form
right now the label show up when Pay is zero and Bal is zero
Pay should not be zero, but Bal is ok to be zero

Pay Textbox=[FPaymentSub].[Form]![Payment]' currency format
Bal Textbox=[FPaymentSub].[Form]![Balance]' currency format


If (IsNull(Pay) = False) And (Bal) = 0 Then''Label455.Visible = True
Else
Label455.Visible = False
End If
 
D

Dirk Goldgar

Dave Elliott said:
both textboxes are on the main form; Pay and Bal
Code is being run on the current event of main form
right now the label show up when Pay is zero and Bal is zero
Pay should not be zero, but Bal is ok to be zero

Pay Textbox=[FPaymentSub].[Form]![Payment]' currency format
Bal Textbox=[FPaymentSub].[Form]![Balance]' currency format


If (IsNull(Pay) = False) And (Bal) = 0 Then''Label455.Visible = True
Else
Label455.Visible = False
End If


1. There's nothing in that code that says anything about whether Pay is
zero or not.

2. You appear to have commented out the line that would make Label455
visible, so once it gets hidden the first time, it's going to stay that
way.

3. I'm not sure what you mean by
Pay Textbox=[FPaymentSub].[Form]![Payment]' currency format
Bal Textbox=[FPaymentSub].[Form]![Balance]' currency format

Are you saying that these are calculated controls, with controlsources
of

=[FPaymentSub].[Form]![Payment]

and

=[FPaymentSub].[Form]![Balance]

? It's conceivable that the values might not have been calculated yet
when the main form's Current event fires. I don't know if that's true,
however, because your If statement doesn't do what you said it ought to.
 
D

Dave Elliott

Yes, these are calculated controls with control sources of, etc...

If (IsNull(Pay) = False) And (Bal) = 0 Then
Label455.Visible = True
Else
Label455.Visible = False
End If
1. There's nothing in that code that says anything about whether Pay is
zero or not.

2. You appear to have commented out the line that would make Label455
visible, so once it gets hidden the first time, it's going to stay that
way.

3. I'm not sure what you mean by
Pay Textbox=[FPaymentSub].[Form]![Payment]' currency format
Bal Textbox=[FPaymentSub].[Form]![Balance]' currency format

Are you saying that these are calculated controls, with controlsources
of

=[FPaymentSub].[Form]![Payment]

and

=[FPaymentSub].[Form]![Balance]

? It's conceivable that the values might not have been calculated yet
when the main form's Current event fires. I don't know if that's true,
however, because your If statement doesn't do what you said it ought to.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

Dave Elliott said:
Yes, these are calculated controls with control sources of, etc...

If (IsNull(Pay) = False) And (Bal) = 0 Then
Label455.Visible = True
Else
Label455.Visible = False
End If

If this code is not doing what you want -- and I gather it isn't --
please say what it is that you want to have happen. I can't make it out
from your original post. Under what exact circumstances do you want the
label to be visible?
 
D

Dave Elliott

The Label455 should be visible if the criteria is met!
criteria is Pay has a value int it and Bal equals zero
 
D

Dirk Goldgar

Dave Elliott said:
The Label455 should be visible if the criteria is met!
criteria is Pay has a value int it and Bal equals zero

That's what you originally said, and that's what the code does (although
it would make a bit more sense to write "And (Bal = 0)" instead of "And
(Bal) = 0"). But then later you said "Pay should not be zero, but Bal
is ok to be zero". That confused me.

Now, I've tested this with a simple form, unbound controls, and a label,
and it does exactly what you just said: if Pay is not Null, and Bal is
equal to 0, then the label is visible; otherwise the label is not
visible. Therefore, if it's not working for you, it's probably due to
the fact that Pay and Bal are calculated controls that pick up their
values from calculated controls on a subform. There can be timing
issues in such circumstances, in which the values of the main form's
calculated controls haven't yet been calcuated at the time the code
runs. Maybe that's what's happening here.

One way to test that would be to temporarily add a command button to the
form, and give it this line of code to execute it when clicked:

Call Form_Current

If clicking that button causes the label's visibility to be set the way
you want, then the problem with the code in the Current event is almost
certainly due to the timing issue I mentioned.

If that is the problem, it can be a difficult problem to solve. Maybe
it would be suffcient to insert the line

Me.Recalc

in the form's Current event, before the code that checks the values and
sets the label's visibility. Does that work?
 
D

Dave Elliott

Yes the command button and the code by itself both work, bu the label does
not stay visible afterwards, i.e. leaving the form and re-opening.
Me.Recalc freaks it out...
will try a timer event with code next.
still dont understand why label is not staying visible as a saved record.
 
D

Dirk Goldgar

Dave Elliott said:
Yes the command button and the code by itself both work, bu the label
does not stay visible afterwards, i.e. leaving the form and
re-opening.
Me.Recalc freaks it out...

What does that mean?
will try a timer event with code next.
still dont understand why label is not staying visible as a saved
record.

I'm having a hard time figuring out what you mean, and if this is a
timing problem it may be difficult to solve. If you'd like to send me a
cut-down copy of your database, containing only the elements necessary
to demonstrate the problem, compacted and then zipped to less than 1MB
in size (preferably much smaller) -- I'll have a look at it, time
permitting. You can send it to the address derived by removing NO SPAM
from the reply address of this message (or you can get my e-mail address
from my website).
 

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