Results 1 to 7 of 7
  1. #1
    PatrickKissane
    Guest

    Controls on the fly

    Does anyone know if it is possible to create controls on an Access Form/Report on the fly? VB has an 'add' method for a form's 'controls' collection, but I haven't been able to find a VBA equivalent.

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Brisbane, Queensland, Australia
    Posts
    352
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Controls on the fly

    I don't think you can do this. One workaround is to create all the controls you might want and make them invisible. I did this to make a gantt chart. I had a stack of invisible boxes I could move to wherever I wanted them by code.
    David Grugeon
    Brisbane Australia

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Controls on the fly

    Actually, you can do it with forms but not with reports. The question is, why would you want to?

    It's much cleaner to put unbound controls on a form and manipulate their properties and contents on the fly. And if you create a runtime application, the user can't modify objects, so you've built an automatically broken app.
    Charlotte

  4. #4
    PatrickKissane
    Guest

    Re: Controls on the fly

    I am evaluating different methods of displaying the results of a dynamic query to the user - the data source to the form returns with different columns each time, and I wanted to avoid a datasheet interface.
    One method is, as David mentioned, to have a swag of invisible boxes that can be manipulated upon the form's Load event, and this is probably the easiest method.
    I was just seeing if I could avoid the 'baggage' of unused controls by building them as needed.
    I hadn't thought of the run-time issue, though - this application won't be run-timed, but it is something to keep in mind.
    Where can I find the syntax?

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Controls on the fly

    Well, first off, I'd suggest using a report instead of a form if all you want to do is display the results. Look in the Solutions9.mdb, which should be somewhere on the Office CD or can be downloaded from the MS site, for examples of dynamic reports.

    The problem with adding controls to the form on the fly is that you have to do it in design view of the form. The method is CreateControl, and you can get to a sample by typing createcontrol in the Answer Wizard in on-line help.
    Charlotte

  6. #6
    dmatherton
    Guest

    Re: Controls on the fly

    Patrick,

    It sounds as though I'm working on something similar, but from another angle.

    I've been trying to put a Report Wizard together and have been playing with the "CreateReportControl" function in Access 97 (I assume it carried forward into 2000).

    This lets you create a new control of a specified type into any section of your report - but that's where I come unstuck. You should be able to reference particular report sections using named enums (acGroupLevel1Header, etc.). Help has some reasonably useful examples, but I think my coding is screwed up elsewhere.

    Try looking up CreateReportControl and Section in Help.

    Hope this helps.....


    D.

  7. #7
    carl g.
    Guest

    Re: Controls on the fly

    Patrick,

    I've added controls to a form on the fly quite alot - it's useful if you want to create a form based on a table, and then be able to "drill down" on that information, once the form is revealed in datasheet format.
    Here's a bit of my code (not very well documented) that may help:

    For intCountFields = 1 To rstSQL.fields.Count '

    strFieldName = rstSQL(intCountFields).Name
    intDataX = x * 850
    Set ctlText = CreateControl(frm.Name, acTextBox, acDetail, _ "", strFieldName, intDataX, intDataY)
    ctlText.ControlSource = strFieldName
    ctlText.Name = strFieldName
    ctlText.DecimalPlaces = 0
    ctlText.Format = "Standard"
    ctlText.Width = 800
    With mdl
    .InsertText CodeString(strFieldName) 'This picks up some code as string from the module basFormCode
    End With
    x = x + 1
    Debug.Print "strFieldName " & strFieldName
    Next intCountFields

    Note that you need to set up a template form

    I've attached all of the code that is in that procedure.

    Regards,

    Carl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •