Activating TextBox with a CommandButton

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • girolle
    New Member
    • Jul 2015
    • 9

    Activating TextBox with a CommandButton

    I have multiple textboxes in a userform and wish to enter text into a specific textbox with a command button.

    For example: when the form is opened, textbox1 is "active" so when I click a button that text is displayed in textbox1. Then I click a "next" command button and textbox2 becomes active so when I click a button that text is displayed in textbox2. I click the "next" button and textbox3 becomes active, and so on.

    I don't really know anything about VBA so I don't even know how to begin to research this on my own. I also apologize if I use any incorrect terminology.

    Thanks so much.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    I think that by "Activate" you mean "Enable". If the enabled property is set to false, it will appear grayed out and you can't click it. To activate it, you would set its enabled property to True.
    Code:
    Me.Textbox_Name.Enabled = True
    To add text to a textbox you just assign text to it.
    Code:
    Me.Textbox_Name = "My Text"
    If you are wanting to do this on the click of a button, you would put this code in the button's On Click event.

    Comment

    • girolle
      New Member
      • Jul 2015
      • 9

      #3
      It's difficult to explain what exactly I want to do. I'll try to be more specific (with a screen shot as well):

      This is my userform. I'm wanting to create a form to score figure skating programs (hence the technical elements). An example skating program would be (in order of technical elements executed):

      1. 3F
      2. 3F+3T<
      3.
      ...

      Thus, when the first element (a triple flip jump) is executed, I want to click that button (labeled 3 in the Flip row) to input that element into the textbox. The second element is a triple flip-triple toe loop combination jump, so I want to click next to go to the second textbox and click the appropriate buttons (button labeled 3 in the Flip row followed by the button labeled 3 in the Toe Loop row), etc. I suppose instead of clicking next, I could just click into the textbox but... This applies for the Grade of Execution marks as well (the -3, -2, -1, 0, ...).

      However, I also need to be able to input the elements appropriately. If it's possible, it needs to be entered as +3T (or whatever the second or third jump in the combination is) BUT if the 3T is by itself, I only need it entered as "3T" not "+3T."

      Or maybe this isn't possible?

      [IMGNOTHUMB]http://i200.photobucke t.com/albums/aa24/trentonjamesdav is/Screen%20Shot%2 02015-07-11%20at%204.02. 32%20PM_zps0wtz fgmn.png[/IMGNOTHUMB]
      Last edited by NeoPa; Jul 12 '15, 04:05 PM. Reason: Made image easier to see with text.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        That is an ambitious project there!

        Your form is one way to work this; however, it seems to me that your database isn't normalized please read: Database Normalization and Table Structures .

        Let's take a look at your column numbered one thru thirteen.
        If there is only one element in the program then only the first box would be filled?
        If there are two elements then the first element would be in box one, and then the second element would be in box two along with the element in box one (concatenating the entry in box one with the new value for entry into box two)
        If there were a third element then the entry in box three would be the concatenation of the entry in box two with the new element so that the string would have three values (3F+3T+1S...)

        Do I understand that correctly?

        What you want done is possible; however, there may be an easier method once you have read about the normalization of the database.

        Comment

        • girolle
          New Member
          • Jul 2015
          • 9

          #5
          Yes, it is an ambitious project. And I will take a look at that!

          Not quite. The elements aren't concatenated; as in, each element (and textbox) is distinct and separate from the one(s) before it and the one(s) after it. So it could be:

          1. 3F
          2. 3T+2S
          3. 2S
          4. 3F+2T+2T
          etc.

          So they don't need to be linked to one another.

          I understand how to click a button and have a specific textbox filled with the text of that button (e.g. 3F in textbox1) but I need that button (in this case, the 3 button in the flip row) to be nonspecific/the text of that button to fill any of the textboxes (rather, a textbox of my choosing, whether it's textbox1 or textbox10).

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32653

            #6
            You have 13 TextBoxes by 13 Command Buttons.

            Is each TextBox assigned a certain specific value or do they vary?

            If they vary, how do you determine which text value goes in each? By typing it in after the TextBox is enabled or is ther some specific logic to each one?

            Ultimately, regardless of how the data is stored, or even whether it's stored, there is a great deal that can be done with code behind a Command Button. With a clearer understanding of your actual requirements we can help more directly.

            Comment

            • girolle
              New Member
              • Jul 2015
              • 9

              #7
              There are more than 13 command buttons on this form:

              24 command buttons for Jumps (left-hand side)
              54 command buttons for Spins (right-hand side)
              6 command buttons for step/choreographic sequences
              4 command buttons for Jump information (Bonus, +COMBO, +SEQ, +REP)
              9 command buttons for Deductions & Violations
              7 command buttons for Grade of Execution

              Each of the 13 textboxes are not assigned a specific value. Each textbox can be filled based on which command button is clicked. I would like to determine which textbox is filled (hence my use of the term "selected" or "activated" ) by using the "Next" and "Back" buttons (though I suppose using the Tab key could suffice). Again, for example:

              A short program in figure skating consists of 7 elements (3 jumps, 3 spins, a step sequence). If the skater has executed 4 of 7 elements, the fifth texbox is ready to be filled. The spins, jumps, and step sequence can be of any nature (e.g 3F or 3T or 3A; StSq1 or StSq4, etc.) and in any order. As stated above, some of the jumps can be combinations of individual jumps (e.g. 3F+3T or 3Lz+2T, etc.) so in those cases, the textbox must be filled exactly as that: [Jump 1]+[Jump 2].

              Each textbox will be filled as each button is clicked. For example: for textbox5 (the fifth technical element) the skater executes a 3F+3T< jump combination (< denotes an under-rotated jump is all). So first the 3 button in the Flip row will be clicked (3F will fill the textbox) then the 3 button in the Toe Loop row will be clicked (+3T will fill the textbox, giving 3F+3T), then the < button will be clicked (< will fill the textbox, giving 3F+3T<). Element number 5 has now been called and textbox5 is complete and I am ready to fill textbox6. This input method is the same for spins: For a Level 4 sit spin with a flying entrance the following buttons will be clicked in this order: F button followed by 4 button in the Sit row, giving FSSp4 (F comes from clicking the F button and SSp4 comes from 4 button).

              The smaller textboxes to the right of the larger textboxes are for Grade of Execution marks (the 7 buttons labeled as such: -3 to +3).

              Storage of the data is another thing (I'll address that much, much later). This form is only getting at calling what elements are executed. I will ultimately want the data that is filled in the textboxes to be transferred to a spreadsheet (via the Confirm Elements command button) where they will, hopefully, be automatically matched with their [numerical] point values and calculated.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32653

                #8
                That changes my understanding somewhat. Here's another important question :
                Assuming that we can determine which of the TextBoxes is next to be active, does each Command Button specify a full value in the active TextBox or do you envisage pressing a combination of the Command Buttons which together result in the full string before moving on to another TextBox?

                If each Command Button has a specific and full value then we needn't worry about determining which TextBox is active separately. OTOH if a combination of Command Buttons is possible to build up the string value required then we would need one other Command Button whose task would be to identify and activate the next appropriate TextBox. If you provide an activation Command Button for each TextBox then you can be sure the operator will select the wrong one sometimes and confuse the process.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32653

                  #9
                  Looking at the picture of the form it occurs to me that Back, Clear and Next under the 13 TextBoxes indicate that your intention is actually very much in line with the idea of multiple possible Command Button entries into each TextBox with the idea of navigating through the list forwards, backwards or even clearing the currently active one. Nice design BTW. I might suggest losing the dots after the numbers if I were being finicky ;-) Also, it's possible to use Labels instead of TextBoxes for this as it appears we will always be putting the data in by VBA code and that can write to the .Caption property of a Label just as easily as it writes to a TextBox, without worrying about the operator typing anything in there directly. KISS. The simpler the better but, that's just an option.

                  I refer to the thirteen TextBoxes, yet there are thirteen pairs of them by the look of things. I'll assume for now that the one on the right is irrelevant to our discussion.

                  The approach I'd suggest for what you're trying to do, therefore, would be to build a Collection of the relevant Controls when the form is opened. Collections are very flexible and you can use the .Tag property of a Control to flag it as one you want in the Collection. You would keep a Control variable available at all times to reflect the currently active one and the Next and Back Command Buttons would simply select the next or previous Control item from the Collection.

                  Changing the .BackgroundColo r of the currently selected Control would be helpful to enable the operator more clearly to see where they're at, at any particular point in the process.

                  Does that all make sense?
                  Last edited by NeoPa; Jul 12 '15, 11:30 PM.

                  Comment

                  • girolle
                    New Member
                    • Jul 2015
                    • 9

                    #10
                    I can't take credit for the layout per se. Lol That goes to Swiss Timing. This is kind of the basic layout of the official software (which is much more complex as it spans multiple judges and thus multiple computers (upwards of 14 separate computers).

                    [IMGnothumb]http://i200.photobucke t.com/albums/aa24/trentonjamesdav is/Screen%20Shot%2 02015-07-12%20at%208.58. 22%20PM_zpseevc tswg.png[/IMGnothumb]

                    As you can see, the first pair of boxes are highlighted/colored white, indicating that those boxes are active.

                    Yes, in some cases multiple buttons will need to be pressed to create a string (because sometimes technical elements are made up of multiple "individual " elements, as in the jump combinations (e.g. 3F+3T where two buttons will need to be clicked, or 2A+2T+SEQ where three buttons will need to be clicked) or spins with different features (e.g. FCSSp4 where three buttons will need to be pressed)).

                    Thanks everyone for their input and suggestion, by the way. I greatly appreciate it.

                    Each button has a specific caption (don't know if you would call that a value technically):

                    Buttons 1, 2, 3, and 4 in the Toe Loop row have the captions 1T, 2T, 3T, and 4T; for Loop: 1Lo, 2Lo, 3Lo, 4Lo, etc. For buttons B, 1, 2, 3, 4, V, and V2 in the Sit [spin] row: SSpB, SSp1, SSp2, etc. For all F and C buttons in the spins (right side), they're non-specific in that they're just F and C (they could actually just be two long buttons that span all six spins).

                    I considered using labels instead of textboxes but wasn't sure if that were possible; I will probably swap the textboxes for labels. And yes, it's more appropriate to say that there's 13 pairs of textboxes/labels, so it's important that both pairs are selected.

                    When the form is opened, the first pair of labels should be active (because the first executed element and accompanying Grade of Execution (GOE) will fill those pairs). After that, the user will click the NEXT button and the second pair of labels will be active, and so on. In the event that a correction needs to be made, I want to be able to use the back button to go back to the label with the mistake, clear it, and re-enter. This is why it is important for only one pair of labels to be active at a single time, so that which ever element button is clicked, only that label will be filled.

                    I'm not understanding the Collection but will look into that more.
                    Last edited by zmbd; Jul 14 '15, 03:24 AM. Reason: [z{expanded image},note that not all of our experts can see 3rd party image sites}]

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32653

                      #11
                      If each item is actually a pair of Controls then you may need to create a UDT (User Defined Type) that contains two Controls in it. The Collection would then be of elements which are of this UDT.

                      BTW. A Collection can be considered as very similar to an array but where each element may be referenced, not only by its relative offset, but also by a mnemonic. In your case you may consider that an array is perfectly adequate for your needs as the identifier in most cases will be the number anyway.

                      Let us know if you need any further help with this - particularly if you get stuck on any point.

                      OT.
                      NB. I'm surprised to see a technical post where :
                      1. Separate is spelled correctly.
                        Originally posted by Girolle
                        Girolle:
                        upwards of 14 separate computers
                      2. The subjunctive of To Be is used correctly.
                        Originally posted by Girolle
                        Girolle:
                        I considered using labels instead of textboxes but wasn't sure if that were possible;

                      Such a pleasant change from the norm ;-)

                      Comment

                      • girolle
                        New Member
                        • Jul 2015
                        • 9

                        #12
                        I'll keep you updated! It's difficult and confusing but I'll eventually figure it out myself (hopefully).

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32653

                          #13
                          We're here to help if you need it ;-)

                          Comment

                          • girolle
                            New Member
                            • Jul 2015
                            • 9

                            #14
                            ...okay, I'm pretty confused. I've research collections, looping though controls, etc. but can't make any sense of it/apply it to what I want to do. I've actually made no progress in these three weeks. :-p

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32653

                              #15
                              I know that makes it feel like everything is in Greek but if you look at it dispassionately and calmly you can generally focus in on the individual elements that are confusing you. We're going to need you to do that for us as we really aren't in a position to help you unless you identify for us where you're having the problems.

                              You don't come across as someone who shies away at the fences. Have another look at it and see if you can identify a single, or maybe even a couple, of points where you are getting stuck. If we start with those we can then move on from there. We have to start somewhere though, and knowing you are struggling with the whole concept of Collections but not why, is really not a good place to start from.

                              As a very basic, it may help to know that much in Access is held in Collections. EG. Forms, Reports, Macros, Tables etc. I say it may help as you're probably already using them without necessarily realising it's Collections you're dealing with.
                              Last edited by NeoPa; Aug 17 '15, 12:05 AM.

                              Comment

                              Working...