Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 17th, 2005, 04:15 PM
Registered User
 
Join Date: Aug 2005
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.
 
Old August 19th, 2005, 02:58 AM
Friend of Wrox
 
Join Date: Jan 2005
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
 
Old August 21st, 2005, 03:30 PM
Registered User
 
Join Date: Aug 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your suggestion.
 
Old August 24th, 2005, 02:59 AM
Authorized User
 
Join Date: Jun 2003
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
 
Old August 24th, 2005, 08:35 PM
Registered User
 
Join Date: Aug 2005
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





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





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