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

Assistance on adding a checkbox to a form

Status
Not open for further replies.

GeorgeDurkee

Programmer
Feb 22, 2000
47
US
I am trying to dynamically add a check box to my form and can't seem to get the controls.add function to work right.

I've found a number of references to the corect code, like this: Set Control = object.Add( ProgID [, Name [, Visible]])

Here is my code:
Dim Mycmd As Control
Private Sub CommandButton1_Click()

Set Mycmd = Controls.Add("MSForms.CommandButton.1")

Mycmd.Left = 18
Mycmd.Top = 150
Mycmd.Width = 175
Mycmd.Height = 20
Mycmd.Caption = "This is fun." & Mycmd.Name

End Sub

When I run the code, I get an error 'Method or data member not found' on the .Add clause.

This tells me that I am missing a reference, but I don't know how to determine what I'm missing. I have the Microsoft Forms 2.0 Object Library loaded, but that doesn't seem to be the one want.

Any ideas?

Thanks
 
I don't know the context of your project but it might be easier to have all the checkboxes you need already created but invisible. Then from code you can set them to visible as needed and change their datasource to whatever you need. hope this helps
 
I agree that having the check boxes on the form would be beter, but for this, the number of checkboxes and what I call them will depend on a selection from a combo box elsewhere on the form.

Loading all of the checkboxes at design time will make the form much bigger and therefore slower.

Thanks
 
In that case I think you have the wrong code. You are looking in the Object class and you should be in Control. The code you posted is used to add or insert a Tab or Page in a TabStrip or MultiPage, or adds a control by its programmatic identifier (ProgID) to a page or form. I think what you want looks like this:
Code:
Dim frm As Form
    Dim ctlLabel As Control, ctlText As Control
    Dim intDataX As Integer, intDataY As Integer
    Dim intLabelX As Integer, intLabelY As Integer

    ' Create new form with Orders table as its record source.
    Set frm = CreateForm
    frm.RecordSource = "Orders"
    ' Set positioning values for new controls.
    intLabelX = 100
    intLabelY = 100
    intDataX = 1000
    intDataY = 100
    ' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)

' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)

You should be able to find this in the help file under create control methods. Hope this helps!
 
That works, thanks

What it does not do and Access doesn't seem to support is creating a control on an existing form at run time. I get an error message that says 'You must be in design mode to add or delte controls', so I'll try it another way.

Again, thanks for the help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top