Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I assign the Click event to a Command button at runtime

Status
Not open for further replies.

barnard90

IS-IT--Management
Mar 6, 2005
73
US
Hi

I am creating a command button at run time called "OK ".
I need to assign a procedure or a function whenever it is clicked .
It needs to have a message box " Data Saved ".

How do I assign that .
The Code I have written so far is as under
Thanks


Dim theControl as Control

Set theControl = frmAutoForm.Controls.Add("Forms.CommandButton.1", "cmdCancel", True)
With theControl

.Caption = "Cancel"
.Left = ctl_width + lbl_width - 100
.Width = 50
.Top = new_ctl_pos
End With

Set theControl = frmAutoForm.Controls.Add("Forms.CommandButton.1", "cmdOK", True)
With theControl

.Caption = "OK"
.Left = ctl_width + lbl_width + (2 * col_gutter) - 50
.Width = 50
.Top = new_ctl_pos
End With
 
You could create a control array with the first button hidden (index=0) but already containing the code. Create a new instance on the form (index=1) - that's visible and position it where needed - and the code will already be there!
 
I think you need the WithEvents attribute added to the Dim statement for theControl. Then you should be able to write a theControl_Click event handler.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
You also need to change type in declaration:
Dim WithEvents theControl As CommandButton

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top