Consulting

Results 1 to 11 of 11

Thread: record and play macro to create commandbutton gives different results

  1. #1
    VBAX Newbie
    Joined
    Jun 2009
    Posts
    5
    Location

    Angry record and play macro to create commandbutton gives different results

    Please help explain this apparent mystery:

    If I record a macro using View->Toolbars->Control Toolbox->Command Button, I get a commandbutton whose right-click menu (under Design Mode) shows me a Properties option and at the bottom a Format Control option, and I get the code below. So far so good; that's what I want.

    ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1",
      Link:=False, DisplayAsIcon:=False, Left:=314, Top:=293, Width:=72, 
      Height:=24).Select
    However, if I execute this newly created macro, I get a commandbutton whose right-click menu doesn't contain a Properties Option and at the bottom a Format Object option). A different animal. Why? why? why?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You have discovered why most of us don't create controls on the fly and why no one has addressed your problem.

    Can you not make them visible or not visible instead?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Newbie
    Joined
    Jun 2009
    Posts
    5
    Location
    Actually, my purpose is not to create the control on the fly but to create it from code. Since I wasn't getting the expected result, I then tried the macro recorder and ended up with the puzzling result.

    Your solution - if I understood well - is to create it on the fly (apparently the only way) and then playing with it from then on thru code. Not a bad idea, I might end up doing that. But still, the question lingers: why?

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    creating controls on the fly means creating them using code.

    Create them normally in the vbe and change thier visibility to suit your need.

    That way the coding can be done in the vbe and you are done.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    attached is a very simple example.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Newbie
    Joined
    Jun 2009
    Posts
    5
    Location
    Ok, I misunderstood. But that is precisely the point: The control I create thru code (at least with OLEObjects.Add etc) is not the one I want (even though the one created with the macro recorder is - and the code is the same!).

    But forget the recorder. Again: I need to create thru code a control (say a commandbutton) that upon right click includes the Properties option and at the end the Format Control option (what I get now doesn't have the Properties option and the last option is Format Object).

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    see, if you create it with code you then have to write code to be added to the new control........becomes a daunting and unnessessary task.

    I think you are still confused.

    If this is on a sheet, the kind of button you wish to create which is an activeX control can be adde to the sheet by:

    right click on any toolbar and make sure that "visual basic" is checked.

    from that toolbar add your control to the sheet and you will be able to right click on it as you require......
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    VBAX Newbie
    Joined
    Jun 2009
    Posts
    5
    Location
    Thanks a lot for your efforts, Lucas. However I do need to create the ActiveX control (thanks for the name!) thru code. As a matter of fact, I create it in a given module and then another module changes some of its properties and later another one actually deletes the control. I'm only having this rather unexpected problem with the creation (I've already done a lot of duanting tasks

    I guess I could create it manually and instead of deleting it I could just hide it. But, at least for the challenge: Must I finally assume that it is impossible to create a "properly behaving" ActiveX control thru code or at least not with the OLEObjects.Add method?

    By the way, the "improperly behaving" control I do get actually does have a number a properties I can modify thru code, but they are very limited in number. I have tried to define what kind of control it is that I'm getting; from the options I get upon right-clicking it seems to be an "object" (?). I even checked the right-click options for a forms-generated commandbutton, and even though there ar some differences it still much resembles the ActiveX control behavior. Any ideas?

    I'm starting to believe that I'm unable to clearly explain this issue...

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Maybe this will give you some information that would help:
    http://www.vbaexpress.com/kb/getarticle.php?kb_id=274

    a search of the knowledgebase will glean even more ideas......


    If after some research you still have questions, post them here.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Newbie
    Joined
    Jun 2009
    Posts
    5
    Location

    Smile SOLVED (Mostly)

    I discovered that once the properties window for a "proper" control is displayed, simply clicking on the "improper" control shows exactly the same properties as the "proper" one.

    Furthermore, these properties are all accesible and modifiable thru code for both controls. Conclusion: They are the same! Well, with one small difference: Right-clicking shows a different set of options. Why? Beats me ...

    I hope this helps other people; I was going nuts assuming I couldn't modify the properties of the control just because upon right-clicking it didn't show the option.

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I saw somthing similar to this once. Seems like it was an html object....been to long to be sure.when you select it in design view what is its name in the name box located to the upper left of the grid.....to the left of the formula bar.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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