If problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DAHMB
    New Member
    • Nov 2007
    • 147

    If problem

    I have a toggle button with a control source called active that is a yes/no field.

    On my form I put a rectangle colored red with a label with the words Inactive on it.
    My toggle button says Active on the button. I have placed the button on top of the rectangle. I want to make the button be invisible when it is toggled off and visible when toggled on. So far I have the following but it is not working:

    [CODE=vb]Private Sub tglActive_After Update()
    Dim strControl As String
    strControl = tglActive
    If strControl = Yes Then
    DoCmd.SetProper ty strControl, acPropertyVisib le = True
    Else
    If strControl = No Then
    DoCmd.SetProper ty strControl, acPropertyVisib le = False
    End If
    End If
    End Sub[/CODE]

    Any ideas?
    Thanks Dan
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi Dan. A toggle returns a boolean value (False/True, represented as 0 and -1) which can be tested directly. Yes/No fields are booleans internally.
    I do not know the name of the label on your form that you want to show/hide, as it is not listed in your code. There are some issues in your code where you are treating controls as strings (which won't work). What is listed below should work once you substitute the actual name of the label or control you want to show or hide.
    [CODE=vb] Private Sub tglActive_After Update()
    Me.[name of the label or control].visible = not(Me.tglActiv e)
    End Sub
    [/CODE]
    However, you don't need to overlay another label on top - you can simply change the caption on the toggle to "Inactive" as follows:
    [CODE=vb] Private Sub tglActive_After Update()
    If Me.tglActive then
    Me.tglActive.Ca ption = "Active"
    else
    Me.tglActive.ca ption = "Inactive"
    endif
    End Sub
    [/CODE]
    Hope this helps.
    -Stewart

    Comment

    • DAHMB
      New Member
      • Nov 2007
      • 147

      #3
      Originally posted by Stewart Ross Inverness
      Hi Dan. A toggle returns a boolean value (False/True, represented as 0 and -1) which can be tested directly. Yes/No fields are booleans internally.
      I do not know the name of the label on your form that you want to show/hide, as it is not listed in your code. There are some issues in your code where you are treating controls as strings (which won't work). What is listed below should work once you substitute the actual name of the label or control you want to show or hide.
      [CODE=vb] Private Sub tglActive_After Update()
      Me.[name of the label or control].visible = not(Me.tglActiv e)
      End Sub
      [/CODE]
      However, you don't need to overlay another label on top - you can simply change the caption on the toggle to "Inactive" as follows:
      [CODE=vb] Private Sub tglActive_After Update()
      If Me.tglActive then
      Me.tglActive.Ca ption = "Active"
      else
      Me.tglActive.ca ption = "Inactive"
      endif
      End Sub
      [/CODE]
      Hope this helps.
      -Stewart

      Thank you for the information, I am learning alot in here. I took your advice and this is what I did and it works great, if something is bad please let me know.
      in my forms on current:
      [code=vb]
      Private Sub Form_Current()
      If Me.tglActive Then
      Me.tglActive.Ca ption = "Active"
      Me.tglActive.Fo reColor = vbGreen
      Else
      Me.tglActive.Ca ption = "Inactive"
      Me.tglActive.Fo reColor = vbRed
      End If
      End Sub
      [/code]

      In the on click event for the toggle button:
      [code=vb]
      Private Sub tglActive_Click ()
      Form_Current
      End Sub
      [/code]

      Thanks
      Dan

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        Glad to be able to help, Dan, and thanks for letting me know how you got on. Your code looks fine - well done!
        -Stewart

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          Looks good Dan :)
          I would make one suggestion :
          It's not good policy to call event procedures directly. A better way to get the same effect is as follows :
          Code:
          Private Sub Form_Current()
            Call Sub_A()
          End Sub
          
          Private Sub tglActive_Click()
            Call Sub_A()
          End Sub
          
          Private Sub Sub_A()
            'Your code here that was in Form_Current()
          End Sub

          Comment

          Working...