How to enable or disable fields based on value of a combo box

G

Guest

I have a form to record mail with or without cash/cheque enclosed. I have a
combo box to choose the mail type and I want to enable the payment related
fields only if the selected mail type includes a payment. I have disabled
these fields 'on open' in the form properties (which works Ok) and added an
afterupdate event to the type field to enable based on selection, which
doesn't work at all. Can anyone help ? I'm new to Access so be gently with
me. Code for both is:

Private Sub Form_Open(Cancel As Integer)

Me.Currency.Enabled = False
Me.Other.Enabled = False
Me.Amount.Enabled = False

Me.Label12.Visible = False
Me.Label13.Visible = False
Me.Label24.Visible = False

End Sub

Private Sub ID_AfterUpdate()
Select Case Me.[Type].Value
Case Is <> "Letter Only"
Me.Currency.Enabled = True
Me.Other.Enabled = True
Me.Amount.Enabled = True

Me.Label12.Visible = True
Me.Label13.Visible = True
Me.Label24.Visible = True


End Select
End Sub
 
G

George Nicholson

' Add this procedure to the code module of your Form
Private Sub DisablePerType()
Dim bolFlag as Boolean

If IsNull(Me.[Type]) then
bolFlag = False
Else
bolFlag = (Me.[Type] <> "Letter Only")
End if

Me.Currency.Enabled = bolFlag
Me.Other.Enabled = bolFlag
Me.Amount.Enabled = bolFlag

Me.Label12.Visible = bolFlag
Me.Label13.Visible = bolFlag
Me.Label24.Visible = bolFlag
End Sub

In the AfterUpdate event of the appropriate control:
Call DisablePerType

In the Form_Current event:
Call DisablePerType
(This should do the work you wanted from Form_Open plus a lot more)

1) Your AfterUpdate code enabled the fields, but they stay enabled because
you never disable them again (until the form is re-opened). You needed a
"Case Else" added to what you posted that would disable your controls again
if (Type <> "Letter Only") was False.
2) Assuming your form isn't in DataEntry mode (only adding records), you
also need to enable/disable as you move through existing records, not just
in AfterUpdate. That's what Form_Current is for. It fires every time the
record changes, *including when the form first opens*, so you don't need
anything in Form_Open.
3) Since you need pretty much identical code in both Form_Current and
ID_AfterUpdate, it makes sense to put that code in a separate routine and
call it from those two events, but this is far from mandatory. This also has
the advantage that if you want to modify it in the future, you only have to
do it in one place.
4) The additional Null handling is just in case. It never hurts :)

5)
added an afterupdate event to the type field
What you posted was an AfterUpdate event to a control called "ID". Any
chance that is why it wasn't working? :)

HTH,
--
George Nicholson

Remove 'Junk' from return address.


Angelaf said:
I have a form to record mail with or without cash/cheque enclosed. I have
a
combo box to choose the mail type and I want to enable the payment related
fields only if the selected mail type includes a payment. I have disabled
these fields 'on open' in the form properties (which works Ok) and added
an
afterupdate event to the type field to enable based on selection, which
doesn't work at all. Can anyone help ? I'm new to Access so be gently
with
me. Code for both is:

Private Sub Form_Open(Cancel As Integer)

Me.Currency.Enabled = False
Me.Other.Enabled = False
Me.Amount.Enabled = False

Me.Label12.Visible = False
Me.Label13.Visible = False
Me.Label24.Visible = False

End Sub

Private Sub ID_AfterUpdate()
Select Case Me.[Type].Value
Case Is <> "Letter Only"
Me.Currency.Enabled = True
Me.Other.Enabled = True
Me.Amount.Enabled = True

Me.Label12.Visible = True
Me.Label13.Visible = True
Me.Label24.Visible = True


End Select
End Sub
 

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