I have a form with 50 toggle buttons on it. One for each state.
What I’m trying to do is create a where clause for a SQL statement for the states the user selects with the buttons.
I created one that works with a multi-select listbox but I want to change the design and use buttons.
They are named Toggle0 to Toggle49.
Can someone give me some ideas?
I played with the Tag property but haven’t figured a way yet.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Toggle Buttons (A2K)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Toggle Buttons (A2K)
- This topic has 19 replies, 8 voices, and was last updated 22 years, 3 months ago.
AuthorTopicWSready4data
AskWoody LoungerJanuary 2, 2003 at 5:09 pm #381286Viewing 0 reply threadsAuthorReplies-
WSShane Sargent
AskWoody LoungerJanuary 2, 2003 at 6:29 pm #641829Given the design you’re moving to, I think you want to loop through each control in the form’s Controls collection, see if it’s a toggle button (acToggleButton). If it is, see if it has been depressed, i.e. its value is True. For each depressed button, you’ll build up the WHERE clause for your query. Simple, right?
I assume you’re familiar with building up a SQL statement in code given your current use of the multi-select list box. Check these two posts for discussions on how to loop through controls: 1 and 2.
Normally I’m not one to critique another’s design choice, but you may want to reconsider the 50 toggle buttons. I simply can’t conceive of a layout that would make a form with 50 toggle buttons visually appealing. Good luck!
-
WSready4data
AskWoody LoungerJanuary 2, 2003 at 7:52 pm #641839Thanks Shane.
What I did was set the tag property to 1 if the button is depressed. Then loop through all the controls that have a tag of 1 and built the where clause.
The buttons aren’t very big 1/4 in. So 50 takes up little room. I did it this way so you could visually see all the selections that were made. In the list box
you have to scroll up/down too much to see all the selections made. -
WSpatt
AskWoody Lounger -
WSready4data
AskWoody Lounger -
WSpatt
AskWoody Lounger -
WSready4data
AskWoody LoungerJanuary 3, 2003 at 10:43 am #642038John,
I’m using the tag property for something else so the caption text is used to build the where clause for the SQL string.
What the user needed is to be able to create from 1 to 5 regions using different combinations of states. After selecting a report will print out showing stats for the combinations that they selected.
If they need to modify a region they could move states from 1 region to another by for lack of a better term un-toggle two buttons and reassign them to a different region.
The report would then show the changed combination.
Here is some code for anyone interestedHere is the code behind each togglebutton:
Private Sub Toggle1_Click()
If Me.Toggle1.VALUE = True Then
Me.Toggle1.Tag = 1
Else
Me.Toggle1.Tag = 0
Me.Toggle1.ForeColor = RGB(0, 0, 0)
End If
End SubHere is the code for the create region buttons:
Private Sub cmdREGION1_Click()
Dim i As Integer
Dim strWHERE As String
For i = 0 To 49
If Me(“Toggle” & i).Tag = “1” Then
strWHERE = strWHERE & “(tblSTATE_SPECS.STATE) LIKE ” & “‘” & Me(“Toggle” & i).Caption & “‘” & ” or ”
Me(“Toggle” & i).Tag = 11 ‘the extra 1 in the tag is for region 1
Me(“Toggle” & i).ForeColor = RGB(255, 0, 0)
End If
Next i
strWHERE = “WHERE ((” & Left(strWHERE, Len(strWHERE) – 4) & “))”
Me.cmdREGION1.ForeColor = RGB(255, 0, 0) ‘turns the text of the button red
Debug.Print strWHERE
End SubHere is the code for the modify regions button:
Private Sub cmdMODIFY1_Click()
Dim i As Integer
Dim strWHERE As StringFor i = 0 To 49
If Me(“Toggle” & i).Tag = “1” Or Me(“Toggle” & i).Tag = “11” Then
strWHERE = strWHERE & “(tblSTATE_SPECS.STATE) LIKE ” & “‘” & Me(“Toggle” & i).Caption & “‘” & ” or ”
Me(“Toggle” & i).Tag = 11
Me(“Toggle” & i).ForeColor = RGB(255, 0, 0)
End If
Next i
strWHERE = “WHERE ((” & Left(strWHERE, Len(strWHERE) – 4) & “))”
Debug.Print strWHERE
End SubAnd finally the code to reset all buttons:
Private Sub cmdRESET_Click()
For i = 0 To 49
Me(“Toggle” & i).DefaultValue = False
Me(“Toggle” & i).Tag = 0
Me(“Toggle” & i).ForeColor = RGB(0, 0, 0)
Next i
Me.cmdREGION1.ForeColor = RGB(0, 0, 0)
Me.cmdREGION2.ForeColor = RGB(0, 0, 0)
Me.cmdREGION3.ForeColor = RGB(0, 0, 0)
Me.cmdREGION4.ForeColor = RGB(0, 0, 0)
Me.cmdREGION5.ForeColor = RGB(0, 0, 0)End Sub
-
WSAndyAinscow
AskWoody Lounger -
WSD Willett
AskWoody LoungerJanuary 3, 2003 at 1:15 pm #642066I’ve watched this thread with interest.
I’m currently playing about with toggles and liked the idea of changing colour when selecting.
I wrote the following to try and accomplish the same.Private Sub Togglered()
Dim Togglered As Integer
For Togglered = 1 To 17
If Me.Controls(“TglUnit” & Format(Togglered, “00”)) = True Then
Me(“tglUnit” & Format(Togglered, “00”)).ForeColor = RGB(255, 0, 0)
Else
Me(“tglUnit” & Format(Togglered, “00”)).ForeColor = RGB(0, 0, 255)
End If
Next Togglered
End SubOn each tglUnit Click I have put “ToggleRed”
It works somewhat but not quite right.
My problem is, when selecting a toggle and moving to the next, I may have two or three still red.Is their something wrong with my code ?
-
WSHansV
AskWoody LoungerJanuary 3, 2003 at 1:39 pm #642074Dave,
You will need to set the color in each routine that changes the toggle buttons:
– the routine that is called when a toggle button is clicked (ToggleUnit)
– the routine that sets the caption and state of the toggle buttons (FillUnits) (this is called by the scrolling routine ScrollUnits)
– the routine that deselects all buttons (Untoggle)BTW, you can use the constants vbRed, vbGreen and vbBlue instead of RGB(255,0,0), RGB(0,255,0) and RGB(0,0,255), respectively.
-
WSAndyAinscow
AskWoody LoungerJanuary 3, 2003 at 1:45 pm #642078At a quick read the code looks OK apart from the sub name is the same as a variable – not a wise idea even if it works.
Do you want to de-select the others when one is selected? If you do then you need to change the state of any previously toggled button before updating the colour.
If you want to support keyboard entry then you should use the AfterUpdate event. The click is only fired from the mouse.minor points: use Format$ – you want a string after all not a variant.
define two constants for your colours eg. cRed and cBlue
const cRed = RGB(255,0,0)
const cBlue = RGB(0,0,255)
then set the ForeColor to these – fractionally less work for the system, has to call the RGB function less often -
WSD Willett
AskWoody Lounger -
WSAndyAinscow
AskWoody Lounger -
WScharlotte
AskWoody LoungerJanuary 3, 2003 at 1:37 pm #642071I’m sorry, but I fail to understand why you’re setting the tag to a value based on the state of the button … and for that matter, why you’re using the tag value to change the forecolor. The color change could be done more simply like this:
Private Sub Toggle1_Click() If Me.Toggle1 = True Then Me.Toggle1.ForeColor = RGB(0, 0, 0) Else Me.Toggle1.ForeColor = RGB(255,0,0) End If End Sub
Alternatively, you could use the built in constants vbRed and vbBlack, since you aren’t using custom colors.
You’re setting the tag to a value and then adding a region number to it, but I don’t see why. And if you have only 49 toggle buttons, you could simply loop through the form’s controls, test to see if it’s a toggle button and handle it appropriately instead of creating control name strings and using that to reference the control.
-
WSready4data
AskWoody LoungerJanuary 3, 2003 at 2:43 pm #642108Charlotte,
Here is my logic flawed as it may be
I’m setting the Tag to a 1 when a button is pressed so when the code loops through the buttons it will only see the buttons that are pressed and build the state string from those. Then it sets the ForeColor of the buttons pressed to region color and the Tags to 11 12 13 14 or 15 depending on what region you are creating.
Say the user selected the whole top row for region1. The Tags for all of those are now 11 (1 for being pressed and 1 for region1) and the ForeColor is Red.
Now say the user selects the whole bottom row and Clicks Create Region 2. ForeColor turn green and the Tags for the bottom row are 12. Again 1 for being pressed and 2 for region 2.
Ok they print the report and find that that alignment of regions is not exactly what they want because of the data in the report. They want to swap 2 of the states between regions. GA to region 2 and WY to region 1
They now un toggle the two buttons they want to change(GA & WY) ForeColor changes to Black and tags to a 0.
Next they select GA. The Tag is set to 1 and then click on the Modify Region 2 button. The state string is now built from the existing buttons with Tags 12 and the newly pressed (GA) button who’s Tag is 1. GA Forecolor is now changed to green and the Tag changed to 12.
When they select WY and click on Modify Region 1 The state string is now built from the existing buttons with Tags 11 and the newly pressed (WY) button who’s Tag is 1 ForeColor is set to red and The tag is now 11.
This lets them create and modify up to 5 Regions, visually see what is selected and a report of stats in the selected regions.
Let me know if you see a better way.
Thanks for the input. -
WSAndyAinscow
AskWoody Lounger -
WSD Willett
AskWoody LoungerJanuary 3, 2003 at 4:28 pm #642154The routine works fine now:
Private Sub Togglered()
Dim Z As Integer
For Z = 1 To 17
If Me.Controls(“TglUnit” & Format(Z, “00”)) = True Then
Me(“tglUnit” & Format$(Z, “00”)).ForeColor = vbRed
Else
Me(“tglUnit” & Format$(Z, “00”)).ForeColor = vbBlue
End If
Next Z
End SubOn the form load event I’ve added:
‘set all toggles to blue
Dim Z As Integer
For Z = 1 To 17
Me(“tglUnit” & Format$(Z, “00”)).ForeColor = vbBlue
Next ZAnd also in the cmdNext – Prev etc.
I don’t quite think they’re where you suggested Hans but thought I’d take the safer option rather than messing with the other routines.
Thanks anyway
Dave -
WSpatt
AskWoody LoungerJanuary 3, 2003 at 7:08 pm #642188Tell me, do you have 50 routines as follows?
Private Sub Toggle1_Click()
If Me.Toggle1.VALUE = True Then
Me.Toggle1.Tag = 1
Else
Me.Toggle1.Tag = 0
Me.Toggle1.ForeColor = RGB(0, 0, 0)
End If
End SubYou could do it in one Function if you wish. You would simply put =Toggle(1) in the OnClick event of the Toggle1 button, =Toggle(2) in the Onclick event of the Toggle 2 button, etc. The following is then a function that drives the buttons. This was derived from the Switchboard where it uses HandleButtonClick as the private function.
Private Function Toggle(intBtnNo as integer)
If Me(“Toggle” & intBtnNo).VALUE = True Then
Me(“Toggle” & intBtnNo).Tag = 1
Else
Me(“Toggle” & intBtnNo).Tag = 0
Me(“Toggle” & intBtnNo).ForeColor = RGB(0, 0, 0)
End If
End FunctionHTH
Pat -
WSready4data
AskWoody Lounger
-
-
-
WSjohnhutchison
AskWoody Lounger
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
7 tips to get the most out of Windows 11
by
Alex5723
49 minutes ago -
Using Office apps with non-Microsoft cloud services
by
Peter Deegan
31 minutes ago -
I installed Windows 11 24H2
by
Will Fastie
14 minutes ago -
NotifyIcons — Put that System tray to work!
by
Deanna McElveen
1 hour, 31 minutes ago -
Decisions to be made before moving to Windows 11
by
Susan Bradley
1 hour, 32 minutes ago -
Port of Seattle says ransomware breach impacts 90,000 people
by
Nibbled To Death By Ducks
1 hour, 43 minutes ago -
Looking for personal finance software with budgeting capabilities
by
cellsee6
3 hours, 5 minutes ago -
ATT/Yahoo Secure Mail Key
by
Lil88reb
14 hours, 24 minutes ago -
Devices with apps using sprotect.sys driver might stop responding
by
Alex5723
18 hours, 46 minutes ago -
Neowin – 20 times computers embarrassed themselves with public BSODs and goofups
by
EP
1 day, 3 hours ago -
Slow Down in Windows 10 performance after March 2025 updates ??
by
arbrich
5 hours, 36 minutes ago -
Mail from certain domains not delivered to my outlook.com address
by
pumphouse
11 hours, 44 minutes ago -
Is data that is in OneDrive also taking up space on my computer?
by
WShollis1818
22 hours, 24 minutes ago -
Nvidia just fixed an AMD Linux bug
by
Alex5723
2 days, 14 hours ago -
50 years and counting
by
Susan Bradley
4 hours, 43 minutes ago -
Fix Bluetooth Device Failed to Delete in Windows Settings
by
Drcard:))
1 day, 20 hours ago -
Licensing and pricing updates for on-premises server products coming July 2025
by
Alex5723
3 days, 1 hour ago -
Edge : Deprecating window.external.getHostEnvironmentValue()
by
Alex5723
3 days, 1 hour ago -
Rethinking Extension Data Consent: Clarity, Consistency, and Control
by
Alex5723
3 days, 1 hour ago -
OneNote and MS Word 365
by
CWBillow
3 days, 3 hours ago -
Ultimate Mac Buyers Guide 2025: Which Mac is Right For You?
by
Alex5723
3 days, 3 hours ago -
Intel Unison support ends on Windows 11 in June
by
Alex5723
3 days, 3 hours ago -
April 2025 — still issues with AMD + 24H2
by
Kevin Jones
18 hours, 55 minutes ago -
Windows 11 Insider Preview build 26200.5518 released to DEV
by
joep517
3 days, 15 hours ago -
Windows 11 Insider Preview build 26120.3671 (24H2) released to BETA
by
joep517
3 days, 15 hours ago -
Forcing(or trying to) save Local Documents to OneDrive
by
PateWilliam
4 days ago -
Hotpatch for Windows client now available (Enterprise)
by
Alex5723
3 days, 12 hours ago -
MS-DEFCON 2: Seven months and counting
by
Susan Bradley
2 days, 13 hours ago -
My 3 monitors go black & then the Taskbar is moved to center monitor
by
saturn2233
4 days, 9 hours ago -
Apple backports fixes
by
Susan Bradley
3 days, 15 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.