Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access 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 July 27th, 2007, 09:18 AM
Registered User
 
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default add method to control

i am creating dynamic some controls (commandbuttons) based on the results of a query. Now i want to make them all use the same event when they are clicked. Does anybody know how to do that....
Either that or to create dynamic an event and add some code to it..


 
Old August 3rd, 2007, 03:27 PM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Travlos,

Is it possible that you may want to hide and unhide command
buttons based on the results of the query where you can dynamically
alter the captions, etc, or do you actually need to dynamically
create the buttons themselves?

If you still need to dynamically create the buttons, I do have
source code that uses the CreateControl function including a sample
to actually insert the _Click() source code for each button as you
create it. You can actually specify the source code in a string
variable and then call a private function inside the form, which
would be your "common code" for each of the Click events for each
command button. You would pass the name of the calling button
to that common private function.

If you're already using the CreateControl now, put your Click()
VBA code into a string variable (strCode, etc) and you can
do something like this:

Dim ctl As Control
Dim strCode As String

Set ctl = CreateControl(strGFormName, CONTROL_COMMAND_BUTTON, _
    2, "", "", intLeft, intTop, intWidth, intHeight)

ctl.Name = "cmdButton1"
ctl.FontName = "MS Sans Serif"
ctl.FontSize = 8
ctl.FontWeight = FONTWEIGHT_BOLD
ctl.Caption = "Process A"
ctl.TabStop = True

strCode = ""
strCode = strCode & "Sub " & ctl.Name & "_Click ()" & vbCrLf
strCode = strCode & vbTab & Call CommonCode(" & ctl.Name & ")" & vbCrLf
strCode = strCode & "End Sub"

Forms(strFormName).Module.InsertText strCode
ctl.OnClick = "[Event Procedure]"

Some of the intrinsic constants (CONTROL_COMMAND_BUTTON, etc)
may be a little old, but this should give you a start on how
to add code dynamically when creating controls dynamically.

Hope that helps.

Warren
 
Old August 4th, 2007, 02:09 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Excellent reply Warren,

Just thought I would add, if you are using code signing, then it will invalidate the signature on it if you use this method (or any other method where the code is altered at run-time).

I know you are prob not using it, but just in case!

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can’t add method to class in C#? arbab BOOK: Beginning C# 2005 Databases 0 September 29th, 2008 07:35 AM
How to add " " inside a method({ }) and XSLT 7 July 14th, 2008 03:27 AM
error on MXSML2 schema add method sasidhar79 Classic ASP XML 0 May 30th, 2007 01:10 PM
Copy method of a popup control sumit_kalra Excel VBA 0 July 11th, 2003 03:36 AM





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