if and then in a form

  • Thread starter Thread starter deb
  • Start date Start date
D

deb

i'm guessing its a macro - not my strong point

i want to grey out an input field based on a combo box selection

so if they select option 1 then box 2 is unavailable and vice versa
 
On Thu, 15 Apr 2010 19:26:01 -0700, deb

Why not make today the day you write your first code? All Access
programmers eventually have to.
It starts with selecting the right spot to write your code. In this
case the AfterUpdate event of your Combobox. It fires after the user
makes a selection. Open the Properties window, select the combobox,
select the Events tab, and click on the ... button next to
AfterUpdate, choose Code Builder, and write this code:
if Me.myCombobox.Value = 1 or Me.myCombobox.Value = 2 then
Me.myInputField.Enabled = False
else
Me.myInputField.Enabled = True
end if
(of course you replace myObjectNames with yours)

-Tom.
Microsoft Access MVP
 
ok here goes, i fiddled a bit and changed it

If Me.Combo50.Value = 4 Then
Me.MonthlyRent.Enabled = False
Else
Me.WeeklyRent.Enabled = True

End If

this works except for a couple of things

when anything other than 4 is selected then i need to disable WeeklyRent
instead

how do i create multiple criteria?

also, if you inadvertantly select the wrong code you have to close the form
and reopen it - is there a way around that?
 
thanks

that makes a whole lot of sense and works perfectly
--
deb


BruceM via AccessMonster.com said:
With the code you posted, if Combo50 is anything other than 4, the enabled
property of MonthlyRent and WeeklyRent will remain as they were. You need
the option of setting Enabled to True for MonthlyRent and False for
WeeklyRent. You could do:

Me.MonthlyRent.Enabled = (Me.Combo50 <> 4)
Me.WeeklyRent.Enabled = Not Me.MonthlyRent.Enabled

Or you can turn it around:
Me.WeeklyRent.Enabled = (Me.Combo50 = 4)
Me.MonthlyRent.Enabled = Not Me.WeeklyRent.Enabled

Or use a Boolean (Yes/No) variable:

Dim blnRent as Boolean

blnRent = (Me.Combo50 = 4)

Me.MonthlyRent.Enabled = Not blnRent
Me.WeeklyRent.Enabled = blnRent

The point is that there are a number of ways to approach it, but using Not
helps eliminate some lines of code. In the first example, this expression is
either True or False:
(Me.Combo50 <> 4)
If Combo 50 = 4, (Me.Combo50 <> 4) is False, so the first line becomes:
Me.MonthlyRent.Enabled = False
The next line sets WeeklyRent.Enabled to the opposite of MonthlyRent.Enabled.

The other code does the same thing, but with different syntax that may work
better in some situations. Using the variable (the third example) can be
especially helpful if there are a number of controls that need to be enabled
or disabled.

Whatever you choose, you will probably want the same code in the form's
Current event.

As a suggestion, you will be doing yourself a favor if you give combo boxes
and other controls names that have some meaning, and are different from the
field names. If the field is named MonthlyRent, the text box could be
txtMonthlyRent. The combo box could be something such as cboRentPeriod.

ok here goes, i fiddled a bit and changed it

If Me.Combo50.Value = 4 Then
Me.MonthlyRent.Enabled = False
Else
Me.WeeklyRent.Enabled = True

End If

this works except for a couple of things

when anything other than 4 is selected then i need to disable WeeklyRent
instead

how do i create multiple criteria?

also, if you inadvertantly select the wrong code you have to close the form
and reopen it - is there a way around that?
Why not make today the day you write your first code? All Access
programmers eventually have to.
[quoted text clipped - 19 lines]
so if they select option 1 then box 2 is unavailable and vice versa
.

--
Message posted via AccessMonster.com


.
 
Back
Top