Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default programming the VBE for a new event procedure

i'm trying to add code to give function to a newly created command button on
the form "frmTimeClock"
but, am not quite getting it right... (?)

the error occurs at line 7 (57017, event handler is invalid)

----------------------------------------------
1 Private Sub CommandButton1_Click()
2 Dim StartLineNum As Long
3 Dim mycmd As MSForms.CommandButton

4 Set mycmd = frmTimeClock.Controls.Add("forms.commandbutton.1")
5 mycmd.Caption = mycmd.Name

6 With ActiveWorkbook.VBProject.VBComponents("frmTimeCloc k").CodeModule
7 StartLineNum = .CreateEventProc("click", mycmd.Name) + 1
8 .InsertLines StartLineNum, _
9 "msgbox""You done it"",vbOKOnly"
10 End With

11 End Sub
------------------------------------------------


thanks (as always) in advance
mark


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default programming the VBE for a new event procedure

? "msgbox""You done it"",vbOKOnly"
msgbox"You done it",vbOKOnly

do you just need some spaces?

--
Regards,
Tom Ogilvy

"mark kubicki" wrote in message
...
i'm trying to add code to give function to a newly created command button

on
the form "frmTimeClock"
but, am not quite getting it right... (?)

the error occurs at line 7 (57017, event handler is invalid)

----------------------------------------------
1 Private Sub CommandButton1_Click()
2 Dim StartLineNum As Long
3 Dim mycmd As MSForms.CommandButton

4 Set mycmd = frmTimeClock.Controls.Add("forms.commandbutton.1")
5 mycmd.Caption = mycmd.Name

6 With ActiveWorkbook.VBProject.VBComponents("frmTimeCloc k").CodeModule
7 StartLineNum = .CreateEventProc("click", mycmd.Name) + 1
8 .InsertLines StartLineNum, _
9 "msgbox""You done it"",vbOKOnly"
10 End With

11 End Sub
------------------------------------------------


thanks (as always) in advance
mark




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 89
Default programming the VBE for a new event procedure

i had to change the line to be 2-part
msgtext = "You done it"
.InsertLines StartLineNum, (MsgBox(msgText, vbOKOnly))
this eliminated a error i was getting with the code
however, the event handler error is still there

i tried this alternate approach which did create the code, but the code does not function when i press the newly created command button
(ALL OF THIS IS HAPPENING ON THE SAME FORM WHICH REMAINS LOADED AND OPEN FOR THE DURATION -is this part of the problem?)


Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("frmTimeClock" ).CodeModule
With VBCodeMod
msgtext = "you done it"
StartLineNum = .CountOfLines + 1
.InsertLines StartLineNum, "Private sub " & mycmd.Name & "_click()" & Chr(13) & _
"msgbox (msgtext)" & Chr(13) & _
"end sub"
End With

additionally, after running this procedure, i will get this message upon clicking another (but not all) of the other command buttons on the sheet:
"object invoked disconnected from client"

UGH !


--------------------------------------------------------------------------------
"Tom Ogilvy" wrote in message ...
? "msgbox""You done it"",vbOKOnly"
msgbox"You done it",vbOKOnly

do you just need some spaces?

--
Regards,
Tom Ogilvy

"mark kubicki" wrote in message
...
i'm trying to add code to give function to a newly created command button

on
the form "frmTimeClock"
but, am not quite getting it right... (?)

the error occurs at line 7 (57017, event handler is invalid)

----------------------------------------------
1 Private Sub CommandButton1_Click()
2 Dim StartLineNum As Long
3 Dim mycmd As MSForms.CommandButton

4 Set mycmd = frmTimeClock.Controls.Add("forms.commandbutton.1")
5 mycmd.Caption = mycmd.Name

6 With ActiveWorkbook.VBProject.VBComponents("frmTimeCloc k").CodeModule
7 StartLineNum = .CreateEventProc("click", mycmd.Name) + 1
8 .InsertLines StartLineNum, _
9 "msgbox""You done it"",vbOKOnly"
10 End With

11 End Sub
------------------------------------------------


thanks (as always) in advance
mark




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default programming the VBE for a new event procedure

This isn't something I have attempted, but I was able to get a working procedure into the userform code module from a button on the form. I say working, because I dropped the form and added the button manually (the code was still there) and ran the form and the event fired. As a trouble shooting aid, I put in a textbox and a commandbutton to put the code from the module on the form. This ran fine before I programmatically added the button, but if I did it after I added the button (and code), I got the error you report. My code to create the event is in the userform code module as I suspect yours is. On Chip Pearson's site

http://www.cpearson.com/excel/vbe.htm

he cautions against this:
"Also, you may get unpredictable results if you attempt to modify a code module's code from that same module. That is, having code in Module1 modify the contents of Module1. I recommend that you do not do this."

My personal preference is to create all the controls up front with the appropriate events and then make them visible or not depending on the situation. This can also be done by controlling the height of the userform. I am sure you have seen dialogs that have a button to show detail and the dialog get much longer with additional controls when the button is pushed. This is simply done by having a complete userform with the height set to show only the top portion of the form, then changing it to include the remainder when the button is pushed.

John Walkenbach shows how to create a userform completely with code, but he does this before he attempts to show the form and uses the designer object which is equivalent to doing it manually as I understand it. Once everything is in place, he shows the form.

So if you still think you need to modify the code, you might drop the form, return to the calling routine where you call code to make the changes, then show the form again. I didn't try that, but if I had to, I think that is the next thing I would try.

--
Regards,
Tom Ogilvy


"mark kubicki" wrote in message ...
i had to change the line to be 2-part
msgtext = "You done it"
.InsertLines StartLineNum, (MsgBox(msgText, vbOKOnly))
this eliminated a error i was getting with the code
however, the event handler error is still there

i tried this alternate approach which did create the code, but the code does not function when i press the newly created command button
(ALL OF THIS IS HAPPENING ON THE SAME FORM WHICH REMAINS LOADED AND OPEN FOR THE DURATION -is this part of the problem?)


Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("frmTimeClock" ).CodeModule
With VBCodeMod
msgtext = "you done it"
StartLineNum = .CountOfLines + 1
.InsertLines StartLineNum, "Private sub " & mycmd.Name & "_click()" & Chr(13) & _
"msgbox (msgtext)" & Chr(13) & _
"end sub"
End With

additionally, after running this procedure, i will get this message upon clicking another (but not all) of the other command buttons on the sheet:
"object invoked disconnected from client"

UGH !


------------------------------------------------------------------------------
"Tom Ogilvy" wrote in message ...
? "msgbox""You done it"",vbOKOnly"
msgbox"You done it",vbOKOnly

do you just need some spaces?

--
Regards,
Tom Ogilvy

"mark kubicki" wrote in message
...
i'm trying to add code to give function to a newly created command button

on
the form "frmTimeClock"
but, am not quite getting it right... (?)

the error occurs at line 7 (57017, event handler is invalid)

----------------------------------------------
1 Private Sub CommandButton1_Click()
2 Dim StartLineNum As Long
3 Dim mycmd As MSForms.CommandButton

4 Set mycmd = frmTimeClock.Controls.Add("forms.commandbutton.1")
5 mycmd.Caption = mycmd.Name

6 With ActiveWorkbook.VBProject.VBComponents("frmTimeCloc k").CodeModule
7 StartLineNum = .CreateEventProc("click", mycmd.Name) + 1
8 .InsertLines StartLineNum, _
9 "msgbox""You done it"",vbOKOnly"
10 End With

11 End Sub
------------------------------------------------


thanks (as always) in advance
mark




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
workbook_Open event Procedure peerless Excel Discussion (Misc queries) 1 May 22nd 06 10:21 PM
workbook_Open event Procedure peerless Excel Discussion (Misc queries) 0 May 22nd 06 10:16 PM
Event Procedure Programming Jeff Armstrong Excel Programming 1 July 29th 04 03:54 PM
OnTime event not firing in Workbook_Open event procedure GingerTommy Excel Programming 0 September 24th 03 03:18 PM
Programming a current procedure Dave Peterson[_3_] Excel Programming 1 July 19th 03 01:58 PM


All times are GMT +1. The time now is 05:34 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"