Need help to display a field only if... (1 Viewer)

justiwhi

Registered User.
Local time
Today, 09:33
Joined
Aug 18, 2008
Messages
14
In a form in Access 2007, I have a field that has a drop down box with four choices. The next field on the form is one that I only want available if one of the four previous choices is selected, and not avaiable if the other 3 are selected. Is this possible?
 

Scooterbug

Registered User.
Local time
Today, 10:33
Joined
Mar 27, 2009
Messages
853
Yes. On the On Current for the Form, you need to specify what happens when the correct entry is chosen.

Code:
if me.ComboBox = 1 then
    me.ControlName.enabled = true
end if

Where ComboBox is the name of the combo box, 1 being the desired value for the combo box and ControlName is the name of the control you want to be available. You could also substitue .Visible instead of enabled if you dont want the control to even be seen.
 

justiwhi

Registered User.
Local time
Today, 09:33
Joined
Aug 18, 2008
Messages
14
Awesome, thanks for the code! One more question. Can I have more than one if statement? Example, currently the code enables ControlName when ComboBox = 1, can I enable ControlName if ComboBox = 1 and 2?
 

missinglinq

AWF VIP
Local time
Today, 10:33
Joined
Jun 20, 2003
Messages
6,420
I'm assuming you mean if it's equal to 1 or 2:
Code:
If (Me.ComboBox = 1 Or Me.ComboBox = 2) Then
    Me.ControlName.Enabled = True
Else
  Me.ControlName.Enabled = False
End If
If you actually are using the multi-select function available in 2007 comboboxes, it'll be somewhat more complicated, I think.

It should be noted that in addition to this code in the Form_Current event, it also needs to be in the AfterUpdate event of the combobox, as well.
 
Last edited:

justiwhi

Registered User.
Local time
Today, 09:33
Joined
Aug 18, 2008
Messages
14
Thanks to everyone for their help. One more question for you guys. Is there a way to have more than one operation occur if the if statement is true? Example, If Me.ComboBox = 1 Then
Me.ControlName1.enabled = true AND Me.controlName2.enabled=true
 

Scooterbug

Registered User.
Local time
Today, 10:33
Joined
Mar 27, 2009
Messages
853
Thanks to everyone for their help. One more question for you guys. Is there a way to have more than one operation occur if the if statement is true? Example, If Me.ComboBox = 1 Then
Me.ControlName1.enabled = true AND Me.controlName2.enabled=true


Yes, your code would look like this:

Code:
If (Me.ComboBox = 1 Or Me.ComboBox = 2) Then
Me.ControlName.Enabled = True
me.ControlName2.Enabled = true
Else
Me.ControlName.Enabled = False
End If
 

Kryst51

Singin' in the Hou. Rain
Local time
Today, 09:33
Joined
Jun 29, 2009
Messages
1,896
Of course in missinglinq's code you would

Code:
If (Me.ComboBox = 1 Or Me.ComboBox = 2) Then
    Me.ControlName1.Enabled = True
    [COLOR=red]Me.ControlName2.Enabled = True[/COLOR]
Else
    Me.ControlName1.Enabled = False
    [COLOR=red]Me.ControlName2.Enabled = False[/COLOR]
End If

Is one way of doing it.

Edit: Scooterbug was faster :)
 

missinglinq

AWF VIP
Local time
Today, 10:33
Joined
Jun 20, 2003
Messages
6,420
Faster, but Scooterbug forgot the

Me.ControlName2.Enabled = False

line in the Else Clause! :D
 

justiwhi

Registered User.
Local time
Today, 09:33
Joined
Aug 18, 2008
Messages
14
Again, thanks for everyones help. Just to let you know, here is the code I ended up with which works exactly how I want it to. I am trying to learn as I go so I try to ask the right questions that I still have to figure out some on my own. You guys are a great help!


Private Sub Service_Type_AfterUpdate()


If Me.Service_Type = "Certified" Then
Text19.Enabled = True
Return_Receipt.Enabled = True
ElseIf (Me.Service_Type = "Express Mail" Or Me.Service_Type = "Registered") Then
Return_Receipt.Enabled = True
Text19.Enabled = False
Else
Text19.Enabled = False
Return_Receipt.Enabled = False
End If

End Sub
 

missinglinq

AWF VIP
Local time
Today, 10:33
Joined
Jun 20, 2003
Messages
6,420
Glad you got it working!

Since you're still earning (and I am, after years of using Access!) a word of advice; you really need to replace Access generated control names, like Text19, with names that will make sense to you three months or six months or a year down the road! You will be back working on the app again, and at that time, Text19 won't mean a thing to you, at that time!.
 

justiwhi

Registered User.
Local time
Today, 09:33
Joined
Aug 18, 2008
Messages
14
Advice taken! Thanks again, I'm sure I will have more questions in the future. Have a great weekend.
 

Users who are viewing this thread

Top Bottom