PC Review


Reply
Thread Tools Rate Thread

Add code on the fly to existing userforms

 
 
pan65
Guest
Posts: n/a
 
      3rd Jan 2008
I know I can create a new userform with new controls and new code for the
controls on the fly using VB. I have also added controls to an existing
userform but I am having trouble adding code for the controls. The name of
my userform is frmGSN and Code is a string of code. I am trying the
following code but it doesn't recognize the .CodeModule

With frmGSN.CodeModule
.InsertLines .CountOfLines + 1, Code
End With

Please help. Thanks
 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      3rd Jan 2008
Here is an example

Sub MakeForm()
Dim TempForm As Object
Dim FormName As String
Dim NewButton As Msforms.CommandButton
Dim TextLocation As Long
'
' Create the UserForm
Set TempForm = ThisWorkbook.VBProject. _
VBComponents.Add(3) 'vbext_ct_MSForm
FormName = TempForm.Name
With TempForm
.Properties("Caption") = "Temporary Form"
.Properties("Width") = 200
.Properties("Height") = 100
End With
'
' Add a CommandButton
Set NewButton = TempForm.Designer.Controls _
.Add("forms.CommandButton.1")
With NewButton
.Caption = "Click Me"
.Left = 60
.Top = 40
End With
'
' Add an event-hander sub for the CommandButton
With TempForm.CodeModule
TextLocation = .CreateEventProc("Click", "CommandButton1")
.InsertLines TextLocation + 1, "MsgBox ""Hello!"""
.InsertLines TextLocation + 2, "Unload Me"
End With
'
' Show the form
VBA.UserForms.Add(FormName).Show
'
' Delete the form
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm
End Sub



--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"pan65" <(E-Mail Removed)> wrote in message
news:44466ACC-042E-4296-A3A6-(E-Mail Removed)...
>I know I can create a new userform with new controls and new code for the
> controls on the fly using VB. I have also added controls to an existing
> userform but I am having trouble adding code for the controls. The name
> of
> my userform is frmGSN and Code is a string of code. I am trying the
> following code but it doesn't recognize the .CodeModule
>
> With frmGSN.CodeModule
> .InsertLines .CountOfLines + 1, Code
> End With
>
> Please help. Thanks



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      3rd Jan 2008
Another option would be to create the userform with those controls on it--and
the code behind those controls, too.

Then just hide/show the controls you want hidden/shown.

pan65 wrote:
>
> I know I can create a new userform with new controls and new code for the
> controls on the fly using VB. I have also added controls to an existing
> userform but I am having trouble adding code for the controls. The name of
> my userform is frmGSN and Code is a string of code. I am trying the
> following code but it doesn't recognize the .CodeModule
>
> With frmGSN.CodeModule
> .InsertLines .CountOfLines + 1, Code
> End With
>
> Please help. Thanks


--

Dave Peterson
 
Reply With Quote
 
pan65
Guest
Posts: n/a
 
      4th Jan 2008
I am gathering from your response and from Bob's response, that I just need
to start with a new userform.

Thanks for the help, it is much appreciated.

"Dave Peterson" wrote:

> Another option would be to create the userform with those controls on it--and
> the code behind those controls, too.
>
> Then just hide/show the controls you want hidden/shown.
>
> pan65 wrote:
> >
> > I know I can create a new userform with new controls and new code for the
> > controls on the fly using VB. I have also added controls to an existing
> > userform but I am having trouble adding code for the controls. The name of
> > my userform is frmGSN and Code is a string of code. I am trying the
> > following code but it doesn't recognize the .CodeModule
> >
> > With frmGSN.CodeModule
> > .InsertLines .CountOfLines + 1, Code
> > End With
> >
> > Please help. Thanks

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      4th Jan 2008
Bob created a new userform, but you could use an existing one.

I suggested that you create the extra controls while you're still designing the
form--not at run time.

pan65 wrote:
>
> I am gathering from your response and from Bob's response, that I just need
> to start with a new userform.
>
> Thanks for the help, it is much appreciated.
>
> "Dave Peterson" wrote:
>
> > Another option would be to create the userform with those controls on it--and
> > the code behind those controls, too.
> >
> > Then just hide/show the controls you want hidden/shown.
> >
> > pan65 wrote:
> > >
> > > I know I can create a new userform with new controls and new code for the
> > > controls on the fly using VB. I have also added controls to an existing
> > > userform but I am having trouble adding code for the controls. The name of
> > > my userform is frmGSN and Code is a string of code. I am trying the
> > > following code but it doesn't recognize the .CodeModule
> > >
> > > With frmGSN.CodeModule
> > > .InsertLines .CountOfLines + 1, Code
> > > End With
> > >
> > > Please help. Thanks

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      4th Jan 2008
You said you were creating a new userform, I just gave some code that
included that creation as well as the controls and the code.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"pan65" <(E-Mail Removed)> wrote in message
news:EF639D3C-6047-4B47-A2F2-(E-Mail Removed)...
>I am gathering from your response and from Bob's response, that I just need
> to start with a new userform.
>
> Thanks for the help, it is much appreciated.
>
> "Dave Peterson" wrote:
>
>> Another option would be to create the userform with those controls on
>> it--and
>> the code behind those controls, too.
>>
>> Then just hide/show the controls you want hidden/shown.
>>
>> pan65 wrote:
>> >
>> > I know I can create a new userform with new controls and new code for
>> > the
>> > controls on the fly using VB. I have also added controls to an
>> > existing
>> > userform but I am having trouble adding code for the controls. The
>> > name of
>> > my userform is frmGSN and Code is a string of code. I am trying the
>> > following code but it doesn't recognize the .CodeModule
>> >
>> > With frmGSN.CodeModule
>> > .InsertLines .CountOfLines + 1, Code
>> > End With
>> >
>> > Please help. Thanks

>>
>> --
>>
>> Dave Peterson
>>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      4th Jan 2008
I think you misread the OP.

The subject adds more info, too.



Bob Phillips wrote:
>
> You said you were creating a new userform, I just gave some code that
> included that creation as well as the controls and the code.
>
> --
> ---
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
> "pan65" <(E-Mail Removed)> wrote in message
> news:EF639D3C-6047-4B47-A2F2-(E-Mail Removed)...
> >I am gathering from your response and from Bob's response, that I just need
> > to start with a new userform.
> >
> > Thanks for the help, it is much appreciated.
> >
> > "Dave Peterson" wrote:
> >
> >> Another option would be to create the userform with those controls on
> >> it--and
> >> the code behind those controls, too.
> >>
> >> Then just hide/show the controls you want hidden/shown.
> >>
> >> pan65 wrote:
> >> >
> >> > I know I can create a new userform with new controls and new code for
> >> > the
> >> > controls on the fly using VB. I have also added controls to an
> >> > existing
> >> > userform but I am having trouble adding code for the controls. The
> >> > name of
> >> > my userform is frmGSN and Code is a string of code. I am trying the
> >> > following code but it doesn't recognize the .CodeModule
> >> >
> >> > With frmGSN.CodeModule
> >> > .InsertLines .CountOfLines + 1, Code
> >> > End With
> >> >
> >> > Please help. Thanks
> >>
> >> --
> >>
> >> Dave Peterson
> >>


--

Dave Peterson
 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      4th Jan 2008
This seems explicit to me ...

I know I can create a new userform with new controls and new code for the
controls on the fly using VB.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I think you misread the OP.
>
> The subject adds more info, too.
>
>
>
> Bob Phillips wrote:
>>
>> You said you were creating a new userform, I just gave some code that
>> included that creation as well as the controls and the code.
>>
>> --
>> ---
>> HTH
>>
>> Bob
>>
>> (there's no email, no snail mail, but somewhere should be gmail in my
>> addy)
>>
>> "pan65" <(E-Mail Removed)> wrote in message
>> news:EF639D3C-6047-4B47-A2F2-(E-Mail Removed)...
>> >I am gathering from your response and from Bob's response, that I just
>> >need
>> > to start with a new userform.
>> >
>> > Thanks for the help, it is much appreciated.
>> >
>> > "Dave Peterson" wrote:
>> >
>> >> Another option would be to create the userform with those controls on
>> >> it--and
>> >> the code behind those controls, too.
>> >>
>> >> Then just hide/show the controls you want hidden/shown.
>> >>
>> >> pan65 wrote:
>> >> >
>> >> > I know I can create a new userform with new controls and new code
>> >> > for
>> >> > the
>> >> > controls on the fly using VB. I have also added controls to an
>> >> > existing
>> >> > userform but I am having trouble adding code for the controls. The
>> >> > name of
>> >> > my userform is frmGSN and Code is a string of code. I am trying the
>> >> > following code but it doesn't recognize the .CodeModule
>> >> >
>> >> > With frmGSN.CodeModule
>> >> > .InsertLines .CountOfLines + 1, Code
>> >> > End With
>> >> >
>> >> > Please help. Thanks
>> >>
>> >> --
>> >>
>> >> Dave Peterson
>> >>

>
> --
>
> Dave Peterson



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      4th Jan 2008
But it's out of context.

Add in the subject and the last couple of sentences and it looks like the OP,
well, you know what I think <bg>.


Bob Phillips wrote:
>
> This seems explicit to me ...
>
> I know I can create a new userform with new controls and new code for the
> controls on the fly using VB.
>
> --
> ---
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
> "Dave Peterson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >I think you misread the OP.
> >
> > The subject adds more info, too.
> >
> >
> >
> > Bob Phillips wrote:
> >>
> >> You said you were creating a new userform, I just gave some code that
> >> included that creation as well as the controls and the code.
> >>
> >> --
> >> ---
> >> HTH
> >>
> >> Bob
> >>
> >> (there's no email, no snail mail, but somewhere should be gmail in my
> >> addy)
> >>
> >> "pan65" <(E-Mail Removed)> wrote in message
> >> news:EF639D3C-6047-4B47-A2F2-(E-Mail Removed)...
> >> >I am gathering from your response and from Bob's response, that I just
> >> >need
> >> > to start with a new userform.
> >> >
> >> > Thanks for the help, it is much appreciated.
> >> >
> >> > "Dave Peterson" wrote:
> >> >
> >> >> Another option would be to create the userform with those controls on
> >> >> it--and
> >> >> the code behind those controls, too.
> >> >>
> >> >> Then just hide/show the controls you want hidden/shown.
> >> >>
> >> >> pan65 wrote:
> >> >> >
> >> >> > I know I can create a new userform with new controls and new code
> >> >> > for
> >> >> > the
> >> >> > controls on the fly using VB. I have also added controls to an
> >> >> > existing
> >> >> > userform but I am having trouble adding code for the controls. The
> >> >> > name of
> >> >> > my userform is frmGSN and Code is a string of code. I am trying the
> >> >> > following code but it doesn't recognize the .CodeModule
> >> >> >
> >> >> > With frmGSN.CodeModule
> >> >> > .InsertLines .CountOfLines + 1, Code
> >> >> > End With
> >> >> >
> >> >> > Please help. Thanks
> >> >>
> >> >> --
> >> >>
> >> >> Dave Peterson
> >> >>

> >
> > --
> >
> > Dave Peterson


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display one sheet or two userforms while code is vba code is runningon another RJQMAN Microsoft Excel Programming 1 30th Dec 2009 12:15 AM
New to userforms - can you help with this code? Anthony Microsoft Excel Programming 10 11th Dec 2007 06:49 PM
Modify existing code to dynamic code Ixtreme Microsoft Excel Programming 5 31st Aug 2007 11:42 AM
Lost code and non functioning userforms in a workgroup template... =?Utf-8?B?am9lcDM=?= Microsoft Word Document Management 6 18th Apr 2006 07:03 PM
Userforms keep popping up, need code to stop it rammieib Microsoft Excel Programming 3 24th Feb 2006 05:24 PM


Features
 

Advertising
 

Newsgroups
 


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