adding controls to userform at runtime

O

Ouka

Hi all,

Need some syntax help here.

How do you add a calendar control to a runtime-built userform?

I can do it with some code I snipped from somewhere for standard MS
controls ala:


Code:
--------------------

Function CreateForm

Dim TempForm As Object
Dim OKButton as MSForms.CommandButton
Dim Code as String

Set OKButton = TempForm.Designer.Controls.Add("forms.CommandButton.1")' ***

With OKButton
.caption = "OK"
.Height = 18
.Width = 44
.Left = 10
.Top = 10
End With

'insert additional code to set size of form

'following code defines actions on click:
Code = ""
Code = Code & Sub OKButton_click()" & vbCrLf
Code = Code & " Unload Me" & vbCrLf
Code = Code & "End Sub" & vbCrLf

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

'Delete the form
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm

End Function

--------------------


I have successfully Dim'ed the calendar ala:

Dim NewCalendar as MSACal.Calendar

but I can't seem to actually add the control. I believe my problem is
at the *** above and getting the right syntax for adding the calendar
with a similar block.

When the code runs, no form is created but no error is thrown. When I
remove the calendar-specific code, the form is created properly.

Any help would be most appreciated!
 
M

michelxld

Hello

to add a calendar at run time in a userform , you may try


Dim Usf As Object
Dim Obj As MSACal.Calendar

Set Usf = ThisWorkbook.VBProject.VBComponents("Userform1")
Set Obj = Usf.Designer.Controls.Add("MSCAL.Calendar.7")

With Obj
..Left = 20: .Top = 20: .Width = 200: .Height = 150
End With

VBA.UserForms.Add (Usf.Name)
UserForms(0).Show



Regards
michel
 
O

Ouka

Thank you for the reply.

THis code works when I'm using it as a stand alone, but for some reason
when I incorperate it into the userform I've already built it does not.
The code fires, everything that is supposed to happen, happens except
that no userform is displayed.

I remove the calendar code and the form is displayed again.

What excatly does the "7" do in:

Set Obj = Usf.Designer.Controls.Add("MSCAL.Calendar.7")
?
 
O

Ouka

Nevermind, they are index numbers. Now I see what was going wrong.
everyone /bonk the newbie!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top