Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 17th, 2005, 04:15 PM
Registered User
 
Join Date: Aug 2005
Location: Auckland, North Shore, New Zealand.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Class modules

Can you please help me.

I would like to add a control to a form at runtime and be able to assign an event procedure to the newly created control. This would enable me to click on the control to call the procedure.

ie: Have a Commandbutton added to a form at runtime and be able to click on this Commandbutton to call an event procedure.

Thanks for your help.
Reply With Quote
  #2 (permalink)  
Old August 19th, 2005, 02:58 AM
Friend of Wrox
 
Join Date: Jan 2005
Location: Bournemouth, Dorset, United Kingdom.
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would set the form up with the command button from the outset, only have the visible property set to false. When you need the button change this property to True and back to false when finished.

Cheers

Matt
Reply With Quote
  #3 (permalink)  
Old August 21st, 2005, 03:30 PM
Registered User
 
Join Date: Aug 2005
Location: Auckland, North Shore, New Zealand.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your suggestion.
Reply With Quote
  #4 (permalink)  
Old August 24th, 2005, 02:59 AM
Authorized User
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

to truly add a control at runtime in VBA you need to execute the Add method of the UserForm object's Controls property. This method expects the ProgID of the control you want to add - something like the following code should be added to your test UserForm:

Code:
Option Explicit

Private WithEvents mRuntimeButton As MSForms.CommandButton

Private Sub mRuntimeButton_Click()

  MsgBox "runtime button clicked"

End Sub

Private Sub UserForm_Initialize()

  Set mRuntimeButton = Me.Controls.Add("Forms.CommandButton.1", "mRuntimeButton", True)

End Sub
You'll additionally need to set a whole bunch of properties (Height, Width, Caption, etc) in order to get the button looking as you want it

If you need to know more, highlight the Add method in your VBA environment and hit F1 for some help
Reply With Quote
  #5 (permalink)  
Old August 24th, 2005, 08:35 PM
Registered User
 
Join Date: Aug 2005
Location: Auckland, North Shore, New Zealand.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you this is really good stuff!

Never be afraid to try something new.
Remember: Amateurs built the ark. Professionals built the Titanic
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using modules drmacy BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 September 21st, 2007 10:39 AM
how to pass values between class modules ? yvon Beginning VB 6 1 December 7th, 2005 05:13 PM
What is Meant by Instancing in Class Modules ramk_1978 VB Components 1 February 2nd, 2005 12:09 AM
Class Modules ezzedin Beginning VB 6 2 June 9th, 2003 03:04 AM



All times are GMT -4. The time now is 01:49 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.