Results 1 to 11 of 11

Thread: Solved - Toggle Button Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    10

    Resolved Solved - Toggle Button Code

    I'm running VBA within MS Excel97 and like to know the code for the
    following situation:

    Form with ToggleButton, the button is labelled "1"

    When I click the button, I'd like it to
    a) become invisible (preferred), or
    b) change it's label from "1" to "0"

    On clicking the button again, i'd like to reverse he previous actions.

    May sound to trivial for you to even bother about, but I have no previous
    programming experience, and your reply would help me greatly with a
    project I'm working on.

    Thanks & Rgds, Andreas
    Last edited by afehrmann; Jun 21st, 2005 at 06:27 AM. Reason: Solved

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Toggle Button Code

    it is very easy to make a button toggle somthing

    Private Sub CommandButton1_Click()
    CommandButton1.Visible = Not CommandButton1.Visible
    End Sub

    but of course this won't work, once it is not visible you can not click it again to bring it back, so it depends what else your code is doing, you my need to just set it back to visible later in the code

    CommandButton1.Visible = true

    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    10

    Re: Toggle Button Code

    Thanks for the quick reply. Works like a charm, but as you said, it works
    onlly once: since it's invisible I can't toggle back and forth.

    Is there a way to change the caption of the button from "1" to blank and
    back to "1" with the next click?

    Rgds, Andreas
    (Adelaide, Australia...in case you're wondering where this dummy novice is from)

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Toggle Button Code

    Private Sub CommandButton1_Click()
    Static onoff As Boolean
    onoff = Not onoff
    CommandButton1.Caption = -CInt(onoff)
    End Sub

    pete

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Toggle Button Code

    Code:
    Private Sub tglButton_Click()
        tblButton.Caption = iif(tglButton,"1","0")
    End Sub
    Or some thing like that. The true/flase part of the iif may need reversing...

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    10

    Thumbs down Re: Toggle Button Code

    Thks Ecniv...

    that routine only works once, not repeatedly...Rgds, Andreas

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Toggle Button Code

    Uh? only once?
    Should fire each time the toggle button is pressed.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Toggle Button Code

    this fixes ecniv example

    CommandButton1.Caption = IIf(CommandButton1.Caption = 0, "1", "0")

    pete

  9. #9
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Toggle Button Code

    Hmmm - but the tglButton should be true or false depending on whether it is pressed or not - shouldn't need to fix it... Oh btw you'd need the speechmarks around the 0 in the iif - 'cause it is text and not a number

    Edit:
    Ok I have tested on a toggle button and works fine (original example).
    There must be something else you've done if it is not working.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Toggle Button Code

    works anyway, and the toggle button state is always the same when the button is pushed

    pete

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    10

    Resolved Re: Toggle Button Code

    Problem Solved.

    Thks for your help...Rgds, Andreas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width