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!

dynamically add control 1

Status
Not open for further replies.

miha75

Programmer
Sep 9, 2003
4
RO
how can i dynamically add a control on a form in Access? i tried but i only managed to do that in Visual Basic, not Microsoft Access
 
What I've done in the past is to take (2) approaches.

One is to create all the buttons and hide the ones not requried, based on a user level I assigned.

Another approach was to again create several buttons and again hide them, but the difference is that I modify the text and related click code to call as specific procedure. That way I could order the buttons and have a sequential list. In approach one, I sometimes had blank areas on the main menu. htwh,

Steve Medvid
"IT Consultant & Web Master"

Chester County, PA Residents
Please Show Your Support...
 
Hi

You can create a control on a form in vba

Open the form in design view, and use CreateControl() command,

however

it is often easier to create the control a hidden control (ie .visible = false) and then make it visible under relevant circumstances

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
I have an example at my offic that would answer your question. However, I am at a clients site today and will not be able to get that to you until after 4:00pm CST.

Here is a snipit of code from Access Help that might just do that. You may need to add the msadox.dll (Microsoft ADO Ext. 2.? for DLL and Security).

Code:
Sub NewControls()
    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)
    ' Restore form.
    DoCmd.Restore
End Sub
 
The code I used to creat a for based from a dynamic query of a table is quite complex. If you would like the code, I will post it. I used several arrays to manage where the control was created (i.e. on what tab of the created tab control are the checks boxes to be created, and x,y values, and all properties). Here is where I create the check boxes.
Code:
 'ADD CHECKBOX
        Set ctlcheck(i) = _
            CreateControl("frmDynamic", _
                acCheckBox, , _
                ctlViews.Pages(iFNo(i)).name, _
                "chkcheckbox" & i, _
                intDataX(iCheckCount(iFNo(i)), _
                iFNo(i)) + 1, _
                intDataY(iCheckCount(iFNo(i)), _
                iFNo(i)) + 1)
        With ctlcheck(i)
            .name = szFieldNames(i, 0)
            .ControlSource = ""
        End With
        'ADD LABLE
        Set ctllable(i) = _
            CreateControl("frmDynamic", _
                acLabel, acDetail, _
                ctlViews.Pages(iFNo(i)).name, _
                "lblcheck" & i, _
                intDataX(iCheckCount(iFNo(i)), _
                iFNo(i)) + 200, _
                intDataY(iCheckCount(iFNo(i)), _
                iFNo(i)) - 40)
        With ctllable(i)
            .name = szFieldNames(i, 0) & "lbl"
            .Caption = szFieldNames(i, 0)
            .FontSize = 9
            .SizeToFit
        End With
        iCheckCount(iFNo(i)) = iCheckCount(iFNo(i)) + 1
        iRowCount(iFNo(i)) = iRowCount(iFNo(i)) + 1
 
thank you all for your help. If you have some code and you can post it, it will be greatly appreciated
 
I'll have to make a file for you to download. There is just too much code. I'll try to have it ready for you on monday and give you the url so you can download the mdb.

Thanks,
Dalton
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top