Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Toggle button not working - Excel 2003 1

Status
Not open for further replies.

Ken

IS-IT--Management
Jul 13, 2005
68
CA

Objective of using Two Toggle Button:
On clicking Toggle Button1 - Activates Toggle Button1 with color blue and makes toggle button2 color black
On clicking Toggle Button2 - Activates Toggle Button2 with color blue and makes toggle button1 color black

Problem:
Earlier was working, now clicking either toggle Button1 or Button2 nothing happens.
Toggle button was working properly earlier now it is not. On clicking only once on any one of the toggle button, it remains in loop.
Not certain why the code is doing it, as same Code works on different Windows Xp SP3 computer. Tried using Option button same thing is happening.
It seems system issue (Windows Xp SP3), question what could be?


Tried below steps for solving the problem:
- I have uninstall, reinstall VBA and MS Excel 2003 still the same.
- Tested the procedure on different Xp SP3 computer with Excel 2003 - it works.
- Did code debugging (F8)
1) On clicking Toggle Button1 it completes the steps from line 1 to 7, after executing the line 7 - the cursor moves to another procedure tglbtn_2_click (not calling any code)
when it is on procedure tglbn_2_click - it moves to next line 11 than returns to line 10 and starts the process again from line 11 moves to 12, 13..16.
After moving to line 16 it goes to previous procedure (tglbtn_1_click) to line1 and moves to line2 again to line1
and process from line 1 to 7.

Code:
Code:
1  Private Sub tglBtn_1_Click()
2    tglBtn_1.Value = True
3    tglBtn_1.ForeColor = vbBlue
4    pub_TglBtn12 = "1"
5
6    tglbtn_2.ForeColor = vbBlack
7    tglbtn_2.Value = False
8  End Sub
9 
10 Private Sub tglBtn_2_Click()
11    tglbtn_2.Value = True
12    tglbtn_2.ForeColor = vbBlue
13    pub_TglBtn12 = "2"
14   
15    tglBtn_1.ForeColor = vbBlack
16    tglBtn_1.Value = False
17 End Sub
 
hi
what happens if you remove lines 7 & 16?
or what happens if you insert the line application.enableevents=false before lines 7 & 16 then set back to =true after 7 & 16?

i don't understand why this works on 1 pc and not the other though!

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Your code fires 'Click' event for the other control:
VBA help said:
Click Event
Occurs in one of two cases:
[ul][li]The user clicks a control with the mouse.[/li]
[li]The user definitively selects a value for a control with more than one possible value.[/li][/ul]
The second case applies to the CheckBox, ComboBox, ListBox, MultiPage, TabStrip, and ToggleButton.
You can test it by adding [tt]Debug.Print[/tt] line to your code:
Code:
Private Sub tglBtn_1_Click()
    tglBtn_1.Value = True
    tglBtn_1.ForeColor = vbBlue
    pub_TglBtn12 = "1"

    tglBtn_2.ForeColor = vbBlack
    tglBtn_2.Value = False
    Debug.Print "Fired by tb1"
End Sub

Private Sub tglBtn_2_Click()
    tglBtn_2.Value = True
    tglBtn_2.ForeColor = vbBlue
    pub_TglBtn12 = "2"

    tglBtn_1.ForeColor = vbBlack
    tglBtn_1.Value = False
    Debug.Print "Fired by tb2"
End Sub

combo
 
hi
in addition to what combo has said i woke up this morning thinking what i said earlier was complete wotsits!

the issue is with one event firing the other. disabling events doesn't affect in this case (no idea why?).

the bigger issue i wasn't thinking about yesterday is that toggelbuttons aren't really designed to be use in this way. some may say they should never be used in any way! more info on controls can be found here

you should really use the option buttons for a mutually exclusive control. to do this you will need to put them in a 'frame' which needs to be created first if you are using a userform. if you are on a worksheet there is a description of how to group the controls on the above link if you use the site's nnavigation buttons to go back a few pages.

if you really want buttons you could possibly use command buttons with similar code to the code tou have for your toggle button but using one button to disable itself and enable the other button.

life? i really need one if this is the first thing i think about when i wake up, that and what the hell today's 'earworm' or 'ohrwurm' is!

good luck

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Hi Loomah / Combo,

1) Already tried using option button - same thing happens event pushes event to next button on line 7 or line 16.

2) used the code given by "Combo" below are the responses

On Clicking once Toggle Button1, Debug prints twice as
Fired by tb1
Fired by tb1

On Clicking once Toggle Button2
1 Fired by tb2
2 Fired by tb1
3 Fired by tb1
4 Fired by tb2
5 Fired by tb1
6 Fired by tb1
7 Fired by tb2
8.... 199 times it does it.

3) Tried using application.enableevents=false before lines 7 & 16
after line to TRUE - still no effect.

It was working for years and even now works on different computer. Thought it may be VBA corrupted and reinstalled it still didn't work.

Just remembered that only thing changes made was for testing had installed Visual Studio 2010 Trial Version - though not using it. Had uninstall Visual Studio 2010 and reinstalled it.) still the same, it seems to be system problem - question arises which file is corrupted and doing this.

Thank you for your support.

Appreciate it.

Thanks,

TechIT
 
Hi Guys,

Apology, correction to my previous posting 31 Jan 12 17:44 on point number 1 used "check box" not "option button"

Tested using option button, with link given by loomah for controls, tried it, works using option button. - Thanks loomah

Only concern, if it was working for a year, how, all of a sudden toggle button started doing wrong steps.

Created new file and tested to see if it works, still it happens on line 7 and line 16.

Hope we get answer for the toggle button.

Thanks,

TechIT
 
The Application.EnableEvents refers to host application events only, does not set MSForms events.
To avoid event loops a custom flag can be used instead.
MS recommends to use rather Change event instead of Click, it fires once with the initial click.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top