+ Reply to Thread
Results 1 to 16 of 16

Is it possible to create sub for the same event but for several objects??

  1. #1
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Is it possible to create sub for the same event but for several objects??

    Hi,

    i have 9 buttons on the User form. There are actually 9 lines of objects:

    txtPart1, txtQty1, txtUnit1, txtTotal1 and btnDelete1
    txtPart2, txtQty2, txtUnit2, txtTotal2 and btnDelete2
    txtPart3, txtQty3, txtUnit3, txtTotal3 and btnDelete3
    txtPart4, txtQty4, txtUnit4, txtTotal4 and btnDelete4
    ...
    ...
    txtPart9, txtQty9, txtUnit9, txtTotal9 and btnDelete9


    where all objects with leading "txt" are textboxes, and btnDelete(i) are command buttons.

    My question is if it is possible to make one sub for btnDelete(i)_click that depending on which commandbutton was clicked, change properties of those textboxes in the same line. Or just need to make 9 subs for each command button?

    My code for one button is:

    Please Login or Register  to view this content.
    And what, the same code I should repeat for each button? There should be some other way..
    Pleeeease help me out with that..
    Last edited by etminasm; 09-15-2010 at 02:25 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Is it possible to create sub for the same event but for several objects??

    btnDelete(i)_click is not correct. Try attaching a workbook so we can understand what you mean
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is it possible to create sub for the same event but for several objects??

    Your best bet will be to use a class module for this, or alter your form layout completely and use a listbox.
    A sample workbook would definitely make life easier though.
    Remember what the dormouse said
    Feed your head

  4. #4
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Is it possible to create sub for the same event but for several objects??

    yea, I\m not so good on those forums.. how can I attach files? I can't attach whole workbook, but I could attach my user form. Then you could see how it looks like and what I mean..

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is it possible to create sub for the same event but for several objects??

    Click the 'Go Advanced' button, then click 'Manage Attachments', then follow the instructions. (see the FAQ here)

  6. #6
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Is it possible to create sub for the same event but for several objects??

    on the attachement there are shown my user form with those 9 lines. And by pressing one of those buttons "X", properties of textboxes in that line should be changed (backgroung should go grey, text should be strickedtrough and button caption should change to "U" (it is for undo. I will make it later)). So, is it possible to make one sub for all those 9 buttons or need to make one sub on each button?

    My code for 3 first buttons is:

    Please Login or Register  to view this content.
    it is quite big...

    Maybe I could make one main sub for those all actions and just call it from btnDelete(i) sub..?
    But how to find which button was been pressed?

    sory for such a big post..
    Attached Images Attached Images

  7. #7
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Is it possible to create sub for the same event but for several objects??

    Please post a workbook, not a picture. Provide a slimmed-down version if necessary.

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is it possible to create sub for the same event but for several objects??

    Here's a picture of a class module that should help.
    Attached Images Attached Images

  9. #9
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Is it possible to create sub for the same event but for several objects??

    romperstomper,

    it looks like it is what i was searching for. I will study it and adapt to my situation. Thank you!!

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is it possible to create sub for the same event but for several objects??

    If you provided a sample workbook, I would provide the actual code, instead of a picture...

  11. #11
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Is it possible to create sub for the same event but for several objects??

    romperstomper: that is wicked!

  12. #12
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Is it possible to create sub for the same event but for several objects??

    Hi,

    I made an example of what I want to do.. This example works ok, but it is quite big.. I think that it should be some shorter way to programme this.. romperstomper, you said that need to make new class and so on, I tried to figure it out, but hopless with my knowledge of vba. Could you then guide me!

    Thank you in advance!!
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Is it possible to create sub for the same event but for several objects??

    sorry, forgot to write.. that to start user form press Ctrl+e

  14. #14
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is it possible to create sub for the same event but for several objects??

    Here you go.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    09-09-2010
    Location
    Haugesund, Norway
    MS-Off Ver
    Excel 2003
    Posts
    33

    Re: Is it possible to create sub for the same event but for several objects??

    HEHEEEIII ROMPERSTOMPER,

    fantastic!!! It is not so small either, but looks more professional.. Many thanks!!!

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Is it possible to create sub for the same event but for several objects??

    Glad to help.
    Using a class also has the advantage that if you need to add an event for all the Total textboxes for example, you can just add one event to the class (and declare the textbox variable WithEvents in the class), rather than adding it to all the textboxes on the form.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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