PDA

View Full Version : Solved: adding buttons and frames programmatically



bigdoggit
07-21-2006, 01:28 PM
I'm trying to add almost all of a userforms command buttons, textboxes, and labels at run time . However, clicking on a command button added at run time gets no response.


This code is part of the userfrom initialize procedure. The other two buttons are created the same way with just their attributes changed, like Name and position properties.

bsccntrl is declared as an object
iBtmLstCntrl is declared as an integer. It helps determine location based on how many labels and textboxes get added.


Set bsccntrl = Controls.Add("Forms.CommandButton.1", "cmd_EditCancel", True)
With bsccntrl
.Top = iBtmLstCntrl + 6
.Width = 84
.Left = 6
.Caption = "Cancel"
.Enabled = True
End With

Below is the sub that worked fine when I added the button at design time. Doesn't work now that I add the button at run time.

Private Sub cmd_EditCancel_Click()
Unload Me
Application.EnableEvents = True
Worksheets("MasterSheet").Protect Password:="RICHUNCLE"
End Sub


The buttons get added fine, location is perfect, just can't get them to respond to being clicked. If anyone needs more info, please let me know. Thanx in advance for advice.

malik641
07-21-2006, 04:53 PM
Hey bigdoggit, and welcome to VBAX :hi:

I just wanted to subscribe to this thread to check up on it to see if anyone could figure out where your problem lies...sorry I don't have a solution for you yet :dunno

Trust me, I'm working on it :type

bigdoggit
07-21-2006, 05:57 PM
I recently realized I could create the buttons at design time, then just reposition them at run time based on how many labels and text boxes are created. It works if I need it to. Still, I crave to know what syntax I have wrong with my preferred solution. Someday I want to do this kind of thing professionaly, of course, Michigan may be the wrong state to find a computer niche. Not much technology that I see.

While I'm writing, another issue I'm having.

I would like to create frames then add text boxes and labels. Now, I can't figure out how one adds a control to a frame at run time. Microsoft help seems great if you already know the answer sometimes, you know? The frames would help to visually organize information being edited or solicited from the user. Not necessary, just a plus as far as my eyes are concerned.

malik641
07-21-2006, 06:24 PM
Don't know about frames...but I got this far with the adding of the button...it's kinda sloppy (I think)


Place this into a standard module:
Option Explicit

Sub AddButton()
Dim BscCntrl As CommandButton
Dim iBtmLstCntrl As Integer

Set BscCntrl = ThisWorkbook.VBProject.VBComponents("UserForm1").Designer _
.Controls.Add("Forms.CommandButton.1", "cmd_EditCancel", True)

With BscCntrl
.Top = iBtmLstCntrl + 6
.Width = 84
.Left = 6
.Caption = "Cancel"
.Enabled = True
End With

Call ShowForm
End Sub

Sub ShowForm()
UserForm1.Show
End Sub

Sub RemoveButton()
ThisWorkbook.VBProject.VBComponents("UserForm1").Designer _
.Controls.Remove "cmd_EditCancel"
End Sub
And this is what the UserForm1 module looks like:
Option Explicit

Private Sub UserForm_Initialize()

End Sub

Private Sub cmd_EditCancel_Click()
MsgBox "hey"

Application.OnTime EarliestTime:=Now + TimeSerial(0, 0, 1), procedure:="RemoveButton", _
Schedule:=True
Unload Me
End Sub
I tried a multitude of other things...but they failed. This is the only thing that worked for me so far.


I couldn't figure out why the command button wouldn't reference the Click event just because it was created during runtime.....apparently it has to be created during design time...which is what my code does.

But that doesn't make any sense why you would even create a command button at runtime if you can't use it? (when the code is already in the UserForm module).

EDIT: Sorry, I forgot to tell you, just run the AddButton procedure to get things rolling.

bigdoggit
07-22-2006, 06:54 PM
thanx for the suggestion. I haven't ventured into the realm of vbproject and such, so I had to set the reference, but it works. I'm a little confused, like you were saying, as to why one can create a button in the userform initialize event but not use the click event. Wierd. So I have to create buttons in a standard module first, then show the userform, and everything works fine. Nice work, good thinking. :cool: I wonder how many straight jackets are collecting dust thanks to forums like these.......

malik641
07-22-2006, 07:47 PM
thanx for the suggestion. I haven't ventured into the realm of vbproject and such, so I had to set the reference, but it works. I'm a little confused, like you were saying, as to why one can create a button in the userform initialize event but not use the click event. Wierd. So I have to create buttons in a standard module first, then show the userform, and everything works fine. Nice work, good thinking. :cool:
No problem :thumb

Sorry I forgot to tell you about the reference :doh:but I'm glad to see you got it working.

The other methods I saw was to create the userform alltogether at run time then add it, show it, then delete it. But I liked the challenge of what you had (which was having the userform first...then adding a button and making it work at runtime).

If it interests you here's the method I'm talking about (they called it a "Simple Example"....this is from Excel 2000 Power Programming with VBA):

Option Explicit

Sub MakeForm()
Dim TempForm As Object
Dim NewButton As Object
Dim Line As Integer

Application.VBE.MainWindow.Visible = False

'Create the UserForm
Set TempForm = ThisWorkbook.VBProject. _
VBComponents.Add(3) 'vbext_ct_MSForm
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
Line = .CountOfLines
.InsertLines Line + 1, "Sub CommandButton1_Click()"
.InsertLines Line + 2, " MsgBox ""Hello!"""
.InsertLines Line + 3, " Unload Me"
.InsertLines Line + 4, "End Sub"
End With

'Show the form
VBA.UserForms.Add(TempForm.Name).Show

'Delete the form
ThisWorkbook.VBProject.VBComponents.Remove TempForm
End Sub
And you don't need the reference for this code.Maybe this will give you some ideas for the future :)

malik641
07-22-2006, 07:50 PM
:cool: I wonder how many straight jackets are collecting dust thanks to forums like these....... Mine's on that pile :yes I love this site. I've learned so much just from helping other people and other people helping me. And this site is the friendliest help forum I've ever seen. Cheers to VBAX :beerchug: