Toggling the Enable control with code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Here is the code (placed in the On Current event in Form and placed in the On
Activate event in the control 'categories'.

Private Sub Form_Current()

If Me.category = "Throws" Then
Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
Me.FCShell.Enabled = False
Me.FCThrow.Enabled = True
ElseIf Me.category = "Pillows" Then
Me.FCFeather.Enabled = True
Me.FCPoly.Enabled = True
Me.FCShell.Enabled = True
Me.FCThrow.Enabled = False
End If

Me.Refresh

End Sub

It will work initially but if I go back to the size control (which will
select the category for me automatically) and change it then the enable
feature does not do what it is told to do (the way i see it).

What am I missing ???
 
Steve,

On activate seems unnecessary since you've already got it in the form's
Current event. I'd add the code to category_AfterUpdate so that it fires if
you change your selection.

HTH,
Brian
 
Steve said:
Here is the code (placed in the On Current event in Form and placed in the On
Activate event in the control 'categories'.

Private Sub Form_Current()

If Me.category = "Throws" Then
Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
Me.FCShell.Enabled = False
Me.FCThrow.Enabled = True
ElseIf Me.category = "Pillows" Then
Me.FCFeather.Enabled = True
Me.FCPoly.Enabled = True
Me.FCShell.Enabled = True
Me.FCThrow.Enabled = False
End If

Me.Refresh

End Sub

It will work initially but if I go back to the size control (which will
select the category for me automatically) and change it then the enable
feature does not do what it is told to do (the way i see it).


You need to call that procedure from the size control's
AfterUpdate event.

I don't understand what you are using the Activate event,
but I can't imagine that it will do anything useful, so try
dumping that stuff.

I also don't see a reason for the Refresh in your code.
Refresh just trys to update existing records that might have
been modified by another user on an different computer.
 
However.....

I just noticed that your code only allows for 2 category values. What
happens if 'category' is null? You may want something like:

Private Sub Form_Current()

SELECT CASE Me.category

CASE "Throws"

Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
etc.

CASE "Pillows"

Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
etc.

CASE ELSE

'do something

End SELECT

End Sub


Brian Bastl said:
Steve,

On activate seems unnecessary since you've already got it in the form's
Current event. I'd add the code to category_AfterUpdate so that it fires if
you change your selection.

HTH,
Brian


Steve D said:
Here is the code (placed in the On Current event in Form and placed in
the
 
not working still.
if i go back and change the size again to one and then the other category it
will not set the enable properties accordingly.

hmmm ...
--
Thanks,
Steve D


Brian Bastl said:
However.....

I just noticed that your code only allows for 2 category values. What
happens if 'category' is null? You may want something like:

Private Sub Form_Current()

SELECT CASE Me.category

CASE "Throws"

Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
etc.

CASE "Pillows"

Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
etc.

CASE ELSE

'do something

End SELECT

End Sub
 
not working still.
if i go back and change the size again to one and then the other category it
will not set the enable properties accordingly.

hmmm ...
 
here's what i am trying now

Private Sub category_AfterUpdate()

If Me.category = "Throws" Then
Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
Me.FCShell.Enabled = False
Me.FCThrow.Enabled = True
ElseIf Me.category = "Pillows" Then
Me.FCFeather.Enabled = True
Me.FCPoly.Enabled = True
Me.FCShell.Enabled = True
Me.FCThrow.Enabled = False
ElseIf IsNull(Me.category) Then
Me.FCFeather.Enabled = True
Me.FCPoly.Enabled = True
Me.FCShell.Enabled = True
Me.FCThrow.Enabled = True
End If

End Sub

This results in nothing happening. Why is this so elusive?
 
Steve said:
here's what i am trying now

Private Sub category_AfterUpdate()

If Me.category = "Throws" Then
Me.FCFeather.Enabled = False
Me.FCPoly.Enabled = False
Me.FCShell.Enabled = False
Me.FCThrow.Enabled = True
ElseIf Me.category = "Pillows" Then
Me.FCFeather.Enabled = True
Me.FCPoly.Enabled = True
Me.FCShell.Enabled = True
Me.FCThrow.Enabled = False
ElseIf IsNull(Me.category) Then
Me.FCFeather.Enabled = True
Me.FCPoly.Enabled = True
Me.FCShell.Enabled = True
Me.FCThrow.Enabled = True
End If

End Sub

This results in nothing happening. Why is this so elusive?


Elusive? Probably because you're missing some little issue.
For example, is Category a combo box with an invisible bound
column?

Let's try a little debugging along the lines of what Brian
suggested. Add this to your procedure:

Else
MsgBox "Category=" & Me.Category

Then change the value and see if that provides any
additional clues.
 
a couple of things ...

the control category is pulled in via a query using the related tblSize.
The category type is determined based off of the sizeID (which is in a combo
box).

as for the debugging piece you mentioned, i am not sure what value to
change. sorry, but i don't understand.
 
Steve said:
a couple of things ...

the control category is pulled in via a query using the related tblSize.
The category type is determined based off of the sizeID (which is in a combo
box).

as for the debugging piece you mentioned, i am not sure what value to
change. sorry, but i don't understand.


Add the Else statments. Then open the form in form view and
do something to the size control to invoke the code.

The general idea of debugging is to add some code or use
some mechanism that provides more information. Then enter
some test data and see what happens. Hopefully, you get a
clue about what's going on. Repeat this process until you
pin down the problem.

Wait a minute! Are you changing the category value using
VBA code in some other event procedure? Is that what you
mean by "category type is determined based off of the
sizeID". If this is the case, the AfterUpdate event will
not be invoked and you would need to put the above code in
the same procedure that set the category.
 
I think we are closing in on this problem. The category value is changing
when the sizeID is changed (no VBA is used though). It's just a combo box
with many different sizes in it. Some size represent pillows, some throws,
others bedding items. The size selected though will dictate what shows up in
the category control. So if I understand you correctly, I should put the
code behind the sizeID control (AfterUpdate event) and it should work. I
will try this later. Gotta run now. Please reply back if you agree with
this line of thinking.

Thanks Marsh.
 
Steve said:
I think we are closing in on this problem. The category value is changing
when the sizeID is changed (no VBA is used though). It's just a combo box
with many different sizes in it. Some size represent pillows, some throws,
others bedding items. The size selected though will dictate what shows up in
the category control. So if I understand you correctly, I should put the
code behind the sizeID control (AfterUpdate event) and it should work. I
will try this later. Gotta run now. Please reply back if you agree with
this line of thinking.


Well, it's still not clear to me how Category is set. If a
user selects a category from the combo box, then the code
belongs in the category combo box's AfterUpdate event. If
there is code in the size control's AfterUpfate event that
set the value of category, then the code belongd in the size
controls AfterUpdate event.

I don't understand how else you could be getting a value in
the category combo box if it's not selected by a user or set
by some code. Maybe I require further clarification???
 
sorry for not making myself clear and thanks for hanging in there with me.
let's try again.

1) no combo box for category control
2) size control is a combo box (located just above category control on the
form). category is filled in automatically when the size is selected.
3) the form is based on a query
4) i have two seperate tables - tblSize and tblCategories. tblCategories is
related to tblSize

hth,
Steve
 
hey Marshall,

i did some experimenting and got it to work.

i put the code on the 'OnChange' event of the sizeID control and it works
just fine.
 
Steve said:
hey Marshall,

i did some experimenting and got it to work.

i put the code on the 'OnChange' event of the sizeID control and it works
just fine.


Well, it's nice to know that it can be made to work, BUT the
Change event is not the right place. It should be the size
control's AfterUpdate event.

You keep saying that the category control (text box?) is
"filled in automatically", but I have no idea what that
means. I presume that it means the user is not entering the
value, so it must mean that you have some code that is
setting its value. OR maybe it means that category is a
text box with a control source expression. In the latter
situation, all bets are off because you have no way to
determine when Access evaluates the expression and can not
use code that depends on the value of the expression. If
this is indeed what you have, then get rid of the expression
and set the category text box in the size combo box's
AfterUpdate event (right before the code we've been working
on all this time).
 
Steve said:
I sent you a copy of the db in a .zip format. Let me know if you learn
something from it.


Normally, I will not open files that are emailed to me. But
my curiosity got the best of me on this one.

First, I have to say that I do not understand why you are
resisting the use of the size combo box's AfterUpdate event
instead of the Change event. Using A2002, I moved your code
to the AfterUpdate event and it worked the way I expected it
to work. Remember that the Change event fires on every
keystroke as a user types a value in the list, so the event
might be triggered far more often than you think. The
AfterUpdate event doesn't fire until the final size value
has been determined.

The thing I found that's interesting and I don't remember
ever stumbling over before, is the way the category value is
automatically filled in (as you said). I don't know how
Access can keep track of all the details required to do this
kind of thing, but it seems that Access is very intelligent
about determining when it has enough information to pull a
value for a bound control from your record source query's
myriad of many-one and one-many joins.

I think the reason that I haven't seen this effect before is
that it is a violation of the rules of normalization to
store the category in the designs table. Since the size id
uniquely determines the category, there is no reason to save
the category. For what you're doing, it's probably not
absolutely necessary to remove the category field, but don't
make a habit of doing this ;-)
 
Back
Top