• Toggle Buttons (A2K)

    Author
    Topic
    #381286

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #641829

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

      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!

      • #641839

        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.

        • #641857

          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? shrug
          Pat cheers

          • #641882

            Patt,
            They are all individual toggle buttons not an option group so you can select as many as you want
            cheers

            • #641898

              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.

            • #642038

              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

            • #642045

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

            • #642066

              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 ?

            • #642074

              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.

            • #642078

              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

            • #642091

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

            • #642109

              Your welcome.

            • #642071

              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:

              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.

            • #642108

              Charlotte,
              Here is my logic flawed as it may be shrug
              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.

            • #642113

              maybe I’m off what you want to achieve but instead of this removing then adding why not respond to the button click when it is already selected to prompt for the region you want to re-assign it to?

            • #642154

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

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

              And 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

            • #642188

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

              You 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 Function

              HTH
              Pat smile

            • #642195

              Thanks Patt

        • #641884

          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.

    Viewing 0 reply threads
    Reply To: Toggle Buttons (A2K)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: