none
Code not working IF THEN ELSE.... RRS feed

  • Question

  • So I am trying to make something weather it is a combo box or label or text box be come visible if the value of another text box is Employee. So I wrote this code and it does not work nothing happens. What did I do wrong?

    Private Sub txtreportedbyhidden_Change()

        If Me.txtreportedbyhidden.Value = "Employee" Then

            Me.lblEmployeeName.Visible = True
            Me.cmbEmployeeName.Visible = True
            Me.lblOtherName.Visible = False
            Me.lblTelephoneNumber.Visible = False
            Me.txtOtherName.Visible = False
            Me.txtContactInfo.Visible = False

        Else

            Me.lblEmployeeName.Visible = False
            Me.cmbEmployeeName.Visible = False
            Me.lblOtherName.Visible = True
            Me.lblTelephoneNumber.Visible = True
            Me.txtContactInfo.Visible = True
            Me.txtOtherName.Visible = True

        End If

    End Sub

                                        
    Monday, December 21, 2015 1:59 PM

Answers

  • Step 1: Shorten your code.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Value = "Employee")
    	    
    	lblEmployeeName.Visible = IsEmployee
    	cmbEmployeeName.Visible = IsEmployee
    	lblOtherName.Visible = Not IsEmployee
    	lblTelephoneNumber.Visible = Not IsEmployee
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub

    Step 2: Assign all lables to their controls, then you can reduce it further.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Value = "Employee")	    
    	
    	cmbEmployeeName.Visible = IsEmployee	
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub

    Step 3: In an active TextBox control, the Value is only changed when editing is done. This only happens when you exit the TextBox or save or cancel the change otherwise, e.g. in code. While you're in the On Change event, you need to use the Text property to read the currently changing value.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Text = "Employee")	    
    	
    	cmbEmployeeName.Visible = IsEmployee	
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub


    Monday, December 21, 2015 2:48 PM

All replies

  • I'd use the After Update event instead of the On Change event:

    Private Sub txtreportedbyhidden_AfterUpdate()
        ...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, December 21, 2015 2:10 PM
  • I did that and still nothing.
    Monday, December 21, 2015 2:37 PM
  • Step 1: Shorten your code.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Value = "Employee")
    	    
    	lblEmployeeName.Visible = IsEmployee
    	cmbEmployeeName.Visible = IsEmployee
    	lblOtherName.Visible = Not IsEmployee
    	lblTelephoneNumber.Visible = Not IsEmployee
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub

    Step 2: Assign all lables to their controls, then you can reduce it further.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Value = "Employee")	    
    	
    	cmbEmployeeName.Visible = IsEmployee	
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub

    Step 3: In an active TextBox control, the Value is only changed when editing is done. This only happens when you exit the TextBox or save or cancel the change otherwise, e.g. in code. While you're in the On Change event, you need to use the Text property to read the currently changing value.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Text = "Employee")	    
    	
    	cmbEmployeeName.Visible = IsEmployee	
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub


    Monday, December 21, 2015 2:48 PM
  • Step 1: Shorten your code.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Value = "Employee")
    	    
    	lblEmployeeName.Visible = IsEmployee
    	cmbEmployeeName.Visible = IsEmployee
    	lblOtherName.Visible = Not IsEmployee
    	lblTelephoneNumber.Visible = Not IsEmployee
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub

    Step 2: Assign all lables to their controls, then you can reduce it further.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Value = "Employee")	    
    	
    	cmbEmployeeName.Visible = IsEmployee	
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub

    Step 3: In an active TextBox control, the Value is only changed when editing is done. This only happens when you exit the TextBox or save or cancel the change otherwise, e.g. in code. While you're in the On Change event, you need to use the Text property to read the currently changing value.

    Private Sub txtreportedbyhidden_Change()
    
    	Dim IsEmployee As Boolean
    
    	IsEmployee = (txtreportedbyhidden.Text = "Employee")	    
    	
    	cmbEmployeeName.Visible = IsEmployee	
    	txtContactInfo.Visible = Not IsEmployee
    	txtOtherName.Visible = Not IsEmployee
    
    End Sub


    Thank you!!! I got it to work
    Monday, December 21, 2015 3:01 PM