Hiding A Control when another control = a certain value

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

I am creating a form and want to hide controls depending on what the
user has entered. I have three controls that are currently not visible
in the design, these are Units Per Tray, Units Per Skillet Pack and
Units Per Outer.

If they select "Bread manu" in the Line ID (which is a list box) I
want them to only be able to only see/enter data into Units Per Tray
and if they enter Cold Salad I want them to only see/enter data into
Units Per Skillet Pack and Units Per Outer.

I don't get any error messages, it just doesn't work.
My code looks like this:

Private Sub Line_ID_AfterUpdate()
If Me.Line_ID = "Bread manu" Then
Me.Units_Per_Tray.Visible = True

ElseIf Me.Line_ID = "Cold salad" Then
Me.Units_Per_Tray.Visible = False
Me.Units_Per_Skillet_Pack.Visible = True
Me.Units_Per_Outer.Visible = True

Else
Me.Units_Per_Tray.Visible = False

End If
End Sub

Any suggestions welcome
Thanks in advance

Mark
 
Is Units Per Tray always False? If so, I suspect that the
list boxes underlying bound column is a numeric key value,
rather than the verbal description. So, just change the
values in your If statements, e.g.:

' Assumes key value of "Bread manu" is 1
If Me.Line_ID = 1 Then
Me.Units_Per_Tray.Visible = True

HTH
Kevin Sprinkel
 
That was it - it works now thanks
But now I would like to add the if statement for a possible 2 entries.
So if If Me.Line_ID = 1 or 10 Then. But it isn't working.

Code looks like this:

Private Sub Line_ID_AfterUpdate()
If Me.Line_ID = 9 Or 1 Then
Me.Units_Per_Tray.Visible = True
Me.Units_Per_Skillet_Pack.Visible = False
Me.Units_Per_Outer.Visible = False

ElseIf Me.Line_ID = 21 Or 20 Then
Me.Units_Per_Tray.Visible = False
Me.Units_Per_Skillet_Pack.Visible = True
Me.Units_Per_Outer.Visible = True

Else
Me.Units_Per_Tray.Visible = False
Me.Units_Per_Skillet_Pack.Visible = False
Me.Units_Per_Outer.Visible = False

End If
End Sub


Thanks in advance

Mark
 
Syntax is IF (boolean expression) THEN (commands) END IF.
In a compound expression such as yours, each side of the
operator must evaluate to a boolean expression, so you
really need:

If Me.Line_ID = 1 OR Me.Line_ID = 10 Then ...

Although not strictly needed, some programmers also add
parentheses to indicate that it's a single boolean value
returned:

If (Me.Line_ID = 1 OR Me.Line_ID = 10) Then ...

Also, if you have more than 2 cases, Select Case is
cleaner and more readable than nested If statements:

Select Case Me.Line_ID
Case 1,10
....
Case 2,4
...
Case Else
End Select

HTH
Kevin Sprinkel
-----Original Message-----
That was it - it works now thanks
But now I would like to add the if statement for a possible 2 entries.
So if If Me.Line_ID = 1 or 10 Then. But it isn't working.

Code looks like this:

Private Sub Line_ID_AfterUpdate()
If Me.Line_ID = 9 Or 1 Then
Me.Units_Per_Tray.Visible = True
Me.Units_Per_Skillet_Pack.Visible = False
Me.Units_Per_Outer.Visible = False

ElseIf Me.Line_ID = 21 Or 20 Then
Me.Units_Per_Tray.Visible = False
Me.Units_Per_Skillet_Pack.Visible = True
Me.Units_Per_Outer.Visible = True

Else
Me.Units_Per_Tray.Visible = False
Me.Units_Per_Skillet_Pack.Visible = False
Me.Units_Per_Outer.Visible = False

End If
End Sub


Thanks in advance

Mark


"Kevin Sprinkel" <[email protected]>
wrote in message [email protected]>...
 
Back
Top