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
-
Uninstalr Updates
by
jv16
1 hour, 25 minutes ago -
Apple zero days for April
by
Susan Bradley
1 hour, 56 minutes ago -
CVE program gets last-minute funding from CISA – and maybe a new home
by
Nibbled To Death By Ducks
11 hours, 37 minutes ago -
Whistleblower describes DOGE IT dept rumpus at America’s labor watchdog
by
Nibbled To Death By Ducks
11 hours, 47 minutes ago -
Seeing BSOD’s on 24H2?
by
Susan Bradley
34 minutes ago -
TUT For Private Llama LLM, Local Installation and Isolated from the Internet.
by
bbearren
2 hours, 11 minutes ago -
Upgrade from Windows 10 to 11
by
Holdsworth8
20 hours, 27 minutes ago -
Microsoft : AI-powered deception: Emerging fraud threats and countermeasures
by
Alex5723
23 hours, 17 minutes ago -
0patch
by
WSjcgc50
16 minutes ago -
Devices might encounter blue screen exception with the recent Windows updates
by
Susan Bradley
16 hours, 39 minutes ago -
Windows 11 Insider Preview Build 22631.5261 (23H2) released to Release Preview
by
joep517
1 day, 2 hours ago -
Problem opening image attachments
by
RobertG
1 day, 3 hours ago -
advice for setting up a new windows computer
by
routtco1001
1 day, 18 hours ago -
It’s Identity Theft Day!
by
Susan Bradley
22 hours, 53 minutes ago -
Android 15 require minimum 32GB of storage
by
Alex5723
1 day, 23 hours ago -
Mac Mini 2018, iPhone 6s 2015 Are Now Vintage
by
Alex5723
1 day, 23 hours ago -
Hertz says hackers stole customer credit card and driver’s license data
by
Alex5723
2 days ago -
Firefox became sluggish
by
Rick Corbett
1 day, 21 hours ago -
Windows 10 Build 19045.5794 (22H2) to Release Preview Channel
by
joep517
2 days, 4 hours ago -
Windows 11 Insider Preview Build 22635.5235 (23H2) released to BETA
by
joep517
2 days, 4 hours ago -
A Funny Thing Happened on the Way to the Forum
by
bbearren
1 day, 1 hour ago -
Download speeds only 0.3Mbps after 24H2 upgrade on WiFi and Ethernet
by
John
6 hours, 24 minutes ago -
T-Mobile 5G Wireless Internet
by
WSmmi16
1 day, 2 hours ago -
Clock missing above calendar in Windows 10
by
WSCape Sand
1 day, 3 hours ago -
Formula to Calculate Q1, Q2, Q3, or Q4 of the Year?
by
WSJon5
2 days, 19 hours ago -
The time has come for AI-generated art
by
Catherine Barrett
1 day, 23 hours ago -
Hackers are using two-factor authentication to infect you
by
B. Livingston
2 days, 9 hours ago -
23 and you
by
Max Stul Oppenheimer
2 days, 16 hours ago -
April’s deluge of patches
by
Susan Bradley
20 hours, 9 minutes ago -
Windows 11 Windows Updater question
by
Tex265
10 hours, 5 minutes 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.