Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Atlanta, Georgia, USA
    Posts
    274
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Toggle Buttons (A2K)

    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.

  2. #2
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    Given 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? <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>

    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!
    <font face="Comic Sans MS"><font color=blue>~Shane</font color=blue></font face=comic>

  3. #3
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Atlanta, Georgia, USA
    Posts
    274
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    Thanks 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.
    Attached Images Attached Images

  4. #4
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Toggle Buttons (A2K)

    What you could do is to change the ForeColour of the buttons to show the ones selected (say, in red) as opposed to the one not selected (say, in black).
    BTW, how did you show those three buttons in a depressed state? <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Pat <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

  5. #5
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Atlanta, Georgia, USA
    Posts
    274
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    Patt,
    They are all individual toggle buttons not an option group so you can select as many as you want
    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

  6. #6
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Toggle Buttons (A2K)

    Instead of setting the tag proprty to 1, you could set it to the name of the state. Then you could build the where clause directly from the non null tags.
    Regards
    John



  7. #7
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Toggle Buttons (A2K)

    I understand that they are individual buttons, I missed the point about them being toggle buttons.
    What John says has merit.
    I would set the tag to the caption of the button, as it already shows the state.

  8. #8
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Atlanta, Georgia, USA
    Posts
    274
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    John,
    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 interested

    Here 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 Sub

    Here 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 Sub

    Here is the code for the modify regions button:
    Private Sub cmdMODIFY1_Click()
    Dim i As Integer
    Dim strWHERE As String

    For 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 Sub

    And 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
    Attached Images Attached Images

  9. #9
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Altnau, Thurgau, Switzerland
    Posts
    447
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    Just a comment - the green is rather bright making the form unpleasant to look at.

  10. #10
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    I'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 Sub

    On 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 ?

  11. #11
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Toggle Buttons (A2K)

    I'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:

    <pre>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</pre>


    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.
    Charlotte

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Toggle Buttons (A2K)

    Dave,

    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.

  13. #13
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Altnau, Thurgau, Switzerland
    Posts
    447
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    At 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

  14. #14
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    Thanks all for the replies, I have some good material to work with here.

  15. #15
    3 Star Lounger
    Join Date
    Nov 2001
    Location
    Atlanta, Georgia, USA
    Posts
    274
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Toggle Buttons (A2K)

    Charlotte,
    Here is my logic flawed as it may be <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    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.

Page 1 of 2 12 LastLast

Posting Permissions

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