IF Statement Not Working

D

DS

I have this on the OnOpen Event of a form but it doesn't seem to be
working. Any suggestions?

If Forms!Sales!Text140 = 1 Then
Me.Text87.Visible = True
Me.Text89.Visible = True
Me.Text91.Visible = True
Else
Me.Text142.Visible = True
Me.Text144.Visible = True
Me.Text145.Visible = True
End If

Thanks
DS
 
R

Rick B

"not working" does not tell us what is wrong. Details please.

One thing I notice is that you are comparing a text field, so I'd compare it
to a string. (Text140 = "1").
 
F

fredg

I have this on the OnOpen Event of a form but it doesn't seem to be
working. Any suggestions?

If Forms!Sales!Text140 = 1 Then
Me.Text87.Visible = True
Me.Text89.Visible = True
Me.Text91.Visible = True
Else
Me.Text142.Visible = True
Me.Text144.Visible = True
Me.Text145.Visible = True
End If

Thanks
DS


Doesn't seem to be working may have some meaning to you, but it really
doesn't tell us anything. Try and be a bit more specific next time.

The Open event is too early in the form opening process.
Place your code in the Form's Load event instead.
Is the form you have this code in the 'Sales' form?
You can use If Me!Text140 = 1 then
otherwise use your Forms!Sales!Text140 syntax.
Don't you want to toggle the 2 groups so that if one group is Visible
the other is not?

If Me!Text140 = 1 Then
Me.Text87.Visible = True
Me.Text89.Visible = True
Me.Text91.Visible = True
Me.Text142.Visible = False
Me.Text144.Visible = False
Me.Text145.Visible = False
Else
Me.Text87.Visible = False
Me.Text89.Visible = False
Me.Text91.Visible = False
Me.Text142.Visible = True
Me.Text144.Visible = True
Me.Text145.Visible = True
End If
 
R

Rick B

Alos, in the onopen event, the fields will not be rechecked as you move from
one record to the next. I'd put the code in my "current" event so it works
as you scroll through records.
 
D

DS

Rick said:
"not working" does not tell us what is wrong. Details please.

One thing I notice is that you are comparing a text field, so I'd compare it
to a string. (Text140 = "1").
Its not hiding or showing the specified fields. I tried the "1" that
didn't work. could it be a timing issue, where Text140 isn't occupied yet?
Thanks
DS
 
D

DS

fredg said:
Doesn't seem to be working may have some meaning to you, but it really
doesn't tell us anything. Try and be a bit more specific next time.

The Open event is too early in the form opening process.
Place your code in the Form's Load event instead.
Is the form you have this code in the 'Sales' form?
You can use If Me!Text140 = 1 then
otherwise use your Forms!Sales!Text140 syntax.
Don't you want to toggle the 2 groups so that if one group is Visible
the other is not?

If Me!Text140 = 1 Then
Me.Text87.Visible = True
Me.Text89.Visible = True
Me.Text91.Visible = True
Me.Text142.Visible = False
Me.Text144.Visible = False
Me.Text145.Visible = False
Else
Me.Text87.Visible = False
Me.Text89.Visible = False
Me.Text91.Visible = False
Me.Text142.Visible = True
Me.Text144.Visible = True
Me.Text145.Visible = True
End If
Yeah, I do want to toggle between the 2 groups. Text140 and the other
fieldsare on the same form. Maybe it is a timing issue. I tried the
OnLoad as well. That didn't show or hide anything either. Text142 to
text145 arealways showing. I have their default set to not visible as well.
Thanks
DS
 
D

DS

Rick said:
Alos, in the onopen event, the fields will not be rechecked as you move from
one record to the next. I'd put the code in my "current" event so it works
as you scroll through records.
I tried the onCurrent as well...I think it might be a timing issue.
Thanks
DS
 
R

Rick B

Yes. If text 140 is not filled in when you open the form, then the code
will do the "else" part.

If you are trying to do this in a brand new record, and you want it to
happen after the field is filled in, then you need to tell Access that. You
have told Access to look at the field ON OPEN and do the If statement.
Whatever is in that field when you open the form is what it will use.

I would think you would want the code in the CURRENT event so that as you
scroll through existing records, the field will appear and disapper. You
might also want the same code to run when TEXT140 is changed. Taht way if
the user changes it from "1" to something else, the fields will appear or
disappear.

A bigger issue might be to think about what happens if the field is changed.
Let's say I have a "1" in TEXT140, and I have values in TEXT87, TEXT89, and
TEXT91. If I change TEXT140 to a different entry, then those three fields
will be invisible, but they still have data in them. Don't you want to
clear that data as well? Based on the names, I'm thinking these are unbound
fields, so clearing the data may not matter.
 
D

DS

DS said:
I tried the onCurrent as well...I think it might be a timing issue.
Thanks
DS
Fixed it! It was a Timinig Issue. I placed the code on the previous
form and that fixed it. Thank you for you input!
Sincerely,
DS
 
D

DS

Rick said:
Yes. If text 140 is not filled in when you open the form, then the code
will do the "else" part.

If you are trying to do this in a brand new record, and you want it to
happen after the field is filled in, then you need to tell Access that. You
have told Access to look at the field ON OPEN and do the If statement.
Whatever is in that field when you open the form is what it will use.

I would think you would want the code in the CURRENT event so that as you
scroll through existing records, the field will appear and disapper. You
might also want the same code to run when TEXT140 is changed. Taht way if
the user changes it from "1" to something else, the fields will appear or
disappear.

A bigger issue might be to think about what happens if the field is changed.
Let's say I have a "1" in TEXT140, and I have values in TEXT87, TEXT89, and
TEXT91. If I change TEXT140 to a different entry, then those three fields
will be invisible, but they still have data in them. Don't you want to
clear that data as well? Based on the names, I'm thinking these are unbound
fields, so clearing the data may not matter.
Thanks Rick, Text140 doesn't change once I'm on form, so I just set it
from the previous form and that works fine...thank you for all of the
input and advice.
Sincerely,
DS
 

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