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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically add optionbuttons to a frame within a form

Status
Not open for further replies.

bdmangum

Technical User
Dec 6, 2006
171
US
Howdy,

For some reason I am unable to get this ocde to work. I'm trying to programmatically add two optionbuttons to a frame which is also created in the macro. I created a test file and had no trouble adding the button to a frame which was created prior to compiling. I'm guessing the trouble lies in the frame code. here's what I have code wise:

Code:
'Adds frame for option buttons
With Summary_Final
    Set ppFrame = .Controls.Add("Forms.Frame.1")
    With ppFrame
        .Left = ppLabel1.Left + ppLabel1.Width
        .Top = ppLabel1.Top - 1
        .Width = 35
        .Height = 14
        .Caption = ""
        .BorderStyle = 0    'None
        .BorderColor = &H8000000F
        .ForeColor = &H8000000F
        .BackColor = &H8000000F
        .SpecialEffect = 0  'Flat
        .Visible = True
    End With
End With

'Adds option buttons
With ppFrame
    Set ppOpBut1 = .Controls.Add("Forms.OptionButton.1")
    With ppOpBut1
        .Left = ppLabel1.Left + ppLabel1.Width + 5
        .Top = ppLabel1.Top
        .Width = 12
        .Height = 12
        .Caption = ""
        .Locked = False
    End With
End With

Here is the test code for the frame which begins in the form before compilation. This code works exactly how I what the previous code to work. I'm not seeing the difference. Perhaps someone can point out what I'm apparently blind to.

Note the code below is for only one option button, not both.

Code:
Dim q As Object

With Frame1
    Set q = .Controls.Add("Forms.optionButton.1")
    q.Top = Frame1.Top + 10
End With

Thanks in advance!

 
I am unable to get this ocde to work
What happens ? Any error message ? Unexpected behaviour ? Computer crash ? ... ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
bdmangum,
A guess, [tt]ppFrame[/tt] and [tt]ppOpBut1[/tt] are declared at the procedure level, not the form level? Try this pasting the following into a new blank UserForm and see if it does what you want:
Code:
Option Explicit
Private WithEvents ppFrame As MSForms.Frame
Private WithEvents ppOpBut1 As MSForms.OptionButton

Private Sub UserForm_Click()
'Adds frame for option buttons
With Me
    Set ppFrame = .Controls.Add("Forms.Frame.1", "Testframe", True)
    With ppFrame
        .Width = 70
        .Height = 20
        .Caption = ""
    End With
End With

'Adds option buttons
With ppFrame
    Set ppOpBut1 = .Controls.Add("Forms.OptionButton.1")
    With ppOpBut1
        .Caption = "ppOpBut1"
        .Locked = False
    End With
End With
End Sub

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Thanks for the help. I figured out the problem. It turns out the "Left" and "Top" commands were causing the problem. As PH asked, no there are no error messages, it simply wasn't displaying it correctly.

I was setting the .left of the optionbutton to something around 160. It occurred to me that since the button is set in the frame, the .left command is relative to the frame. This is why it wasn't appearing in my form, it couldn't fit in the frame. I simply set the .left = 0 and .top = 0 and it works just fine.

I wish I had thought of it sooner.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top