+ Reply to Thread
Results 1 to 22 of 22

Userform & Dynamic Controls & Class Module

  1. #1
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Userform & Dynamic Controls & Class Module

    Have the following code:

    Userform: UserForm1

    Please Login or Register  to view this content.
    Class: clsObjHandler

    Please Login or Register  to view this content.
    Have 5 lines of

    Quantity Unit Price Item Price

    Name of textbox fields on each line are:

    txtBoxQuantity1 txtBoxUnitPrice1 txtBoxItemPrice1
    txtBoxQuantity2 txtBoxUnitPrice2 txtBoxItemPrice2
    txtBoxQuantity3 txtBoxUnitPrice3 txtBoxItemPrice3
    txtBoxQuantity4 txtBoxUnitPrice4 txtBoxItemPrice4
    txtBoxQuantity5 txtBoxUnitPrice5 txtBoxItemPrice5

    Am using Keydown Event to determine when the user hits the Return key so that the Quantity value isn't used until the full value has been entered. Can't figure out how to get UnitPrice from the Control Collection so that I can then multiple the Quantity * Unit Price for the Row to calculate Item Price for the Row.

    Thanks
    Last edited by Leith Ross; 09-08-2014 at 06:08 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    re: Userform & Dynamic Controls & Class Module

    Hello sarndt01,

    When asking for help with complex VBA problems like this one, you should always post your work. Recreating it is not only time consuming but often inaccurate as well.

    Please redact any sensitive or confidential information before you upload the workbook.

    How To Post Your Workbook
    1. At the bottom right of the Reply window, Click the button Go Advanced
    2. At the top of the Your Message Window, in the first row of icons, Click the Paperclip icon.
    3. Click the Add Files button at the top right in the dialog box. This displays the File Manager dialog.
    4. Click the Select Files button at the bottom middle of the dialog.
    5. In the new window Find the file you want to upload, select it, and Click Open.
    6. You will now be back in the File Manager dialog. Click the bottom Middle button Upload File.
    7. Wait until the file has completely uploaded before you exit the File Manager dialog.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    re: Userform & Dynamic Controls & Class Module

    You might want to add .LinkedQuantityBox .LinkedUnitBox and .LinkedItemBox properties to your class so you can keep track of which goes with who.

    Another approach would be to add a .Index property to your class.
    And, instead of putting all your run-time text boxes in the same collection, put the Quantity boxes in one collection, Unit boxes in another and Item a third collection.

    collQuantiy(i) matches with collUnit(i) and collItem(i) and collItem(myCustomBox.Index) is the Item box that matches with myCustomBox
    Last edited by mikerickson; 09-08-2014 at 09:57 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    re: Userform & Dynamic Controls & Class Module

    Thanks for your quick reply...

    I've separated the textboxes into separate collections - but I'm still struggling to figure out how to use the values in the collection. I've attached a sample of what I have so far. Note: it error outs in the class textbox keydown event.
    Attached Files Attached Files

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    re: Userform & Dynamic Controls & Class Module

    I think I found a better approach. Make the class a Trio of added text boxes.
    I haven't looked at your sheet, but the attached creates 12 run-time text boxes, 4 Trios, and puts the product of two text boxes in the third of that row.
    It could be adapted for your use.

    Note that each clsAddedTextBoxTrio (the class module) has three text boxes, each with events.
    The calculation is triggered by KeyDown and MouseDown events, but is not reliable. The developer should explicitly call the CalculateAll sub to force calculation of all trios.

    In a class Module
    Please Login or Register  to view this content.
    In the user form's code module
    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    re: Userform & Dynamic Controls & Class Module

    Worked great!
    Thanks

  7. #7
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    One more question - Need to create a running total of the calculated amount from each trio. I've added a static field to the userform. How can I recalculate this in the class?

    Thanks

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform & Dynamic Controls & Class Module

    I wouldn't use the class, but in the userform I would cycle through the AddedTrios collection, adding the .LinkedExtentionBox es.

    If you wanted the code in the Class, I'd do something like

    Please Login or Register  to view this content.
    Last edited by mikerickson; 09-10-2014 at 02:27 PM.

  9. #9
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    My code was similar to yours. But yours fixed some issues I was having with mine. Thanks for all of your help. I appreciate it!

  10. #10
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    Another question: How would I move the cursor from one textbox in a trio to the same or another textbox in a different trio?

    Thanks

  11. #11
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    Also - I've added a checkbox to the trio that the user can set to highlight rows they want to delete from the collection. I have also added a delete button on the userform that when triggered, will loop through the collection in the class module and delete rows in the collection. I can determine which checkboxes have been selected for deletion, but can't figure out to identify the index for each trio in the collection and then delete the item that corresponds to the trio.

    Dim oneThing As clsObjHandler

    For Each oneThing In UserForm1.Coll_Items
    If (oneThing.LinkedDeleteBox.Value = True) Then
    UserForm1.Coll_Items.Remove ???
    End If
    Next oneThing


    And last, I want to then be able to delete the trio (collection of controls) on the userform and then recreate the controls for each items in the collection. And if no items are left, start fresh with a new set of controls for each item.

    Thanks

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform & Dynamic Controls & Class Module

    You might try something like the attached.
    The AddRow button adds a row of text boxes with their check box.
    Delete button deletes the selected rows.

    Note that the code for the text box calculation is in the user form module, not in the class module.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    This is great! Thanks - I'll adapt what I was doing to use your code. The only thing I notice is when I ran your code as is, I can't put the cursor in the fields and/or change the field values.

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform & Dynamic Controls & Class Module

    It works for me.
    I've changed the sub AddARow to make some properties explicit.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by mikerickson; 09-14-2014 at 01:57 AM.

  15. #15
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    I figured out the problem after my last reply. The Font was set to 2. So the field value was changing, but I couldn't see it.
    Thanks

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform & Dynamic Controls & Class Module

    You also asked about tabbing between controls, there was code added to address that.

    Edit: I just occurred to me that if your font size is 2, then the created text boxes have some unusual property settings. It would be prudent to specify all the properties at time of creation rather than relying on defaults.
    Last edited by mikerickson; 09-14-2014 at 02:42 PM.

  17. #17
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    Without relying on the checkbox caption, how do I identify the row number for the active row, so that when the cursor is on the last field on a row, I can move it to the first textbox in the next row?
    Thanks

  18. #18
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform & Dynamic Controls & Class Module

    The .TabStop properties are set at creation to tab from one row to the next.
    Alternatly, you could look at the .Top property of a row and back calculate to find it's row.

    .Top = 10 + (rowNumber * 32)
    RowNumber = (.Top - 10)/32

    The constants in those equations vary with the .Height of the text box so you should check your situation.

  19. #19
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    But once I have the rownumber how do I set the cursor using setfocus on to the first textbox in the next row? I understand how to do it if the name of the textbox has the row number in it by creating a concatenated textbox name and then setting focus to it. but not sure how I do it with Row Collection.

  20. #20
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform & Dynamic Controls & Class Module

    The way the collection is structured, AddedRows(I) is immediately above AddedRows(I +1).
    The DressRows routine sets the .Top property of the custom class according to it's index within the collection.

  21. #21
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    Thought you might see what I came up with...

    Thanks for all your help...
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    09-06-2013
    Location
    Portland Oregon
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: Userform & Dynamic Controls & Class Module

    Hi - Can you help me with one more issue...

    I found code online that I added for Watching Userform events so that I can handle Enter/Exits in my dynamic fields. The code works if I put it in a separate class but fails if I combine it with my Row Control Class. It appears to fail using the Public WithEvents FormCtrl As UserForm1 statement when the corresponding events in the class are compiled/used. I get a circular dependencies between modules error. I've included an updated copy of the sample spreadsheet that I provided last week.

    Thanks
    Attached Files Attached Files
    Last edited by sarndt01; 09-18-2014 at 01:58 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Using Class Module for handling events of dynamically created controls
    By jagman in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2018, 01:14 PM
  2. Replies: 0
    Last Post: 03-08-2014, 08:02 AM
  3. Replies: 1
    Last Post: 10-12-2012, 04:36 AM
  4. (Closed)Dynamic Userform Controls With Event Handlers
    By stuart010 in forum Excel General
    Replies: 0
    Last Post: 04-30-2012, 11:32 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1