adding command buttons at runtime 
Author Message
 adding command buttons at runtime

hello,

i need to add a command button to a form during runtime
(which i have succeeded in doing) and have found no way to
tell this control what should happen when it is PRESSED.
the click event in the form's code is not executed.

pleas help me!

patrick

(word 97 vba)



Fri, 02 Apr 2004 23:18:03 GMT  
 adding command buttons at runtime

Quote:

> hello,

> i need to add a command button to a form during runtime
> (which i have succeeded in doing) and have found no way to
> tell this control what should happen when it is PRESSED.
> the click event in the form's code is not executed.

> pleas help me!

> patrick

> (word 97 vba)

Patrick,

I answered a similar question just the other day regarding dynamic
checkboxes.  I guess you can adapt it for you requirement.

I've cut and pasted my previous mail for you here!

<snip>
I had another look at this problem and this is what I found...
It might be of some use to you.

What you need to do is:

1. Create a new workbook.

2. Add a new user form with this code behind it.

Option Explicit

Private moCol_EventHandlers     As Collection

Private Sub UserForm_Activate()

    Dim i       As Integer
    Dim lTop    As Long

    Dim oCtrl                   As Control
    Dim oChkBox_EventHandler    As CCheckBox

    Set moCol_EventHandlers = New Collection

    For i = 1 To 5
        Set oCtrl = Controls.Add("Forms.CheckBox.1", "chk" & CStr(i),
True)
        Set oChkBox_EventHandler = New CCheckBox
        Set oChkBox_EventHandler.SetCheckBox = oCtrl
        oCtrl.Caption = "Checkbox" & CStr(i)
        oCtrl.Top = lTop
        moCol_EventHandlers.Add oChkBox_EventHandler, oCtrl.Name
        lTop = lTop + 20
    Next i

End Sub

3. Add a new class module named CChkBox with this code:

Option Explicit

Private WithEvents moChkBox As msforms.CheckBox

Public Property Set SetCheckBox(ChkBox As msforms.CheckBox)
    Set moChkBox = ChkBox
End Property

Private Sub moChkBox_Click()
    Select Case moChkBox.Name
    Case "chk1"
        MsgBox "1"
    Case "chk2"
        MsgBox "2"
    Case Else
        MsgBox ">2"
    End Select
End Sub

This may not be exactly what you want but you may be able to work with
it from here.
<snip>

Hope this helps.

Regards

Graham

--------------
Progressive Data Solutions
http://www.pdsolutions.com.au
Home of VB Code Cutter - VB/VBA Code Library & Dev Tool with Free code
formatting/indenting



Sat, 03 Apr 2004 06:15:02 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Adding command buttons to a form at runtime

2. Command Buttons And Pictures at runtime

3. Runtime create graphical command button?

4. How to add a button event handler at runtime from external class

5. Add button to worksheet at runtime

6. Add an event to a dynamic made button (at runtime)

7. Add code behind button at runtime

8. Adding option button at runtime?

9. Adding an array of buttons at runtime

10. add a button to worksheet at runtime

11. Adding buttons/labels to forms at runtime

12. Command button doesn't execute macro when added via code

 

 
Powered by phpBB® Forum Software