+ Reply to Thread
Results 1 to 17 of 17

neede help for creating commandbutton during runtime error

  1. #1
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    neede help for creating commandbutton during runtime error

    0down votefavorite





    i have a module that creates command buttons suring runtime. it will create the commandbuttons in a specificed userform

    program works fine, when i execute the module.

    However, when i use the userform to call the module instead, i have a error stating

    Run-time error '91': Object variable or With block variable not set
    Please Login or Register  to view this content.
    End Sub
    Kindly advise.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: neede help for creating commandbutton during runtime error

    attach file, I can not test without
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: neede help for creating commandbutton during runtime error

    Hi, have attached the codes.

    module1 is where i run my code. when i run it in module1, it creates the commandbutton in form1.

    when i run form1, i can click on the submit buttons.

    but when i try to call the module from form1 instead, i get the error.
    Attached Files Attached Files

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why are you creating controls at runtime?
    If posting code please use code tags, see here.

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: neede help for creating commandbutton during runtime error

    your code works, but see http://www.eng-tips.com/faqs.cfm?fid=110
    you have to activate library and set workbook macro protection to developers setting
    Last edited by patel45; 01-01-2013 at 07:56 AM.

  6. #6
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: neede help for creating commandbutton during runtime error

    Hi,tried adding the reference, and ensuring that all macros are allowed.

    However, i still recieve the same error.

  7. #7
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: neede help for creating commandbutton during runtime error

    it is not sufficient, Excel Options. In the Trust Center category, click Trust Center Settings, and then click the Macro Settings category, developers setting

  8. #8
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: neede help for creating commandbutton during runtime error

    Hi patel.

    i tried clicking the developer's macro settings checkbox, and re run the code. however, i still face the same problem if i call the module from the userform.

    if i run it straight from the module, all is fine.

    Thank you

  9. #9
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: neede help for creating commandbutton during runtime error

    Hi Norie.

    i was thinking of creating a page, that allows user to specify the number of fields he wants to key in.
    hence i would like to create dynamic textboxes, as well as commandbuttons to read inputs.

    i have managed to create, and attach event handlers to the command buttons, however, i can only run it successfully from a module.

    if i use the userform to call that module, then the error occurs.

    Thank you

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why not use a listbox, textbox(es) and command button?

    The user enters data in the textbox(es), presses the command button to transfer the data to the listbox and clear the textbox(es) ready for the next input.

  11. #11
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: neede help for creating commandbutton during runtime error

    Quote Originally Posted by aaronkoh View Post
    i still face the same problem if i call the module from the userform.
    I don't understand, how you call module from the userform ? did you attach the correct file ?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: neede help for creating commandbutton during runtime error

    If you want to add command buttons, or any other control, at runtime you can use something like this.
    Please Login or Register  to view this content.
    Controls added in this way won''t persist, but it doesn't sound like you need them to.

    As for code for these controls, you can use a class module for that.

    By the way, have you considered that not all people might be able to run code like you posted due to security settings.

  13. #13
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: neede help for creating commandbutton during runtime error

    Hi Norie.

    I didnt set the textboxes and command buttons before hand, as i want it to be dynamic.

    e.g. if i hardcode 5 textboxes, and in the event that i instead have 10 strings to show, then i will have to re-write the codes. "inclusion of naother 5 textboxes".


    Hi patel, in the UserForm page, i added "AddButtonAndShow2()".

    the module "AddButtonAndShow2()" works fine on its own when run. but when i called it from UserForm1, the error appears.

  14. #14
    Registered User
    Join Date
    12-18-2012
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: neede help for creating commandbutton during runtime error

    Hi Norie.

    I didnt set the textboxes and command buttons before hand, as i want it to be dynamic.

    e.g. if i hardcode 5 textboxes, and in the event that i instead have 10 strings to show, then i will have to re-write the codes. "inclusion of naother 5 textboxes".


    Hi patel, in the UserForm page, i added "AddButtonAndShow2()".

    the module "AddButtonAndShow2()" works fine on its own when run. but when i called it from UserForm1, the error appears.

  15. #15
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: neede help for creating commandbutton during runtime error

    Please have a look at the link enclosed

    http://stackoverflow.com/questions/1...-to-a-userform
    If you like my contribution click the star icon!

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: neede help for creating commandbutton during runtime error

    Or create a userform from scratch using VBA

    http://j-walk.com/ss/excel/tips/modGetOption.bas

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: neede help for creating commandbutton during runtime error

    I'm not suggesting setting anything beforehand, if you use code like I posted you will create buttons at run-time.

    What you are currently doing isn't creating buttons at run-time, the buttons you create will still be on the form after it's closed.

    Also, every time you run the code you'll be adding new buttons to the already existing buttons.

    For example, if the first time you run it 10 buttons are added and the next time 15 buttons are added you'll have a total of 25 buttons on the form.

    Have a look at the attached workbook which has a userform with controls created at runtime.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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