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

Creating new controls at runtime 3

Status
Not open for further replies.

Orion45

IS-IT--Management
Feb 6, 2002
155
0
0
US
I'm working on creating a sort of virtual seating chart for use with large numbers of people in various amounts. I would like to set up the code so that at run-time a small command button is created for every seat in the room. I found code to do this using the CreateForm method but I need this to work in an existing form. Also, the code currently makes a form in design view but I need it to be in a running form view. Here is the code I am currently using;
Code:
Set frm = CreateForm()
intHeight = 0.25 * adhcTwipsPerInch
intWidth = 0.3 * adhcTwipsPerInch
intGap = 0.03 * adhcTwipsPerInch

For intI = 1 To 6
    For intJ = 1 To 7
        intCount = intCount + 1
        With CreateControl(FormName:=frm.Name, ControlType:=acCommandButton, _
        Section:=acDetail, Left:=intJ * (intWidth + intGap), Top:=intI * (intHeight + _
        intGap))
        .Name = "cmd" & Format(intI, "00") & Format(intJ, "00")
        .Height = intHeight
        .Width = intWidth
        .Caption = intCount
        End With
    Next intJ
Next intI
Does any one have any ideas on how to dynamically create controls at runtime? Would ActiveX controls be more cooperative? Any help would be appreciated. Thanks
 
Hi!

The code you have now is good, but you cannot create controls on a form unless it is in design view. You could create a large amount of buttons on the form and make them invisible. You could then use a loop similar to the one you have to move them around and make them visible. This may give you the effect you are trying for.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Thanks, I had thought of that but the ammount of buttons I need can vary widely, from 3 to 300. I would much rather create controls dynamically if possible.
 
You can use the CreateControl method without the CreateForm method, i.e. on an existing form, but the form you're creating controls on **does** need to be in design view, if only momentarily. That is to say, when the running (form view) form needs to add a control, you switch it to design view, add the control(s), and switch it back to form view. The user will never have known that the form switched to design view (the form will blink/hiccup in an ugly manner though -- toggling a control's Visible property makes for a much more seamless appearance). There are a few issues with this though.

First, when the form switches to Design view, no code in the form's module can run -- so no code in the form's module can ever actually add controls, or switch back to Form view. So, for the form (in Form view) to alter itself, it needs to call a public sub/function which is in some other module. That function then switches the form's view, adds controls, switches the view back, maybe sets focus on the form, and exits.

The approach described above is easy to implement and does work, but it is slow and ugly. I don't recommend it.

The other issue is that this can never work in a .mde file. If you plan to deploy your database as such...forget it. You cannot switch to Design View in a .mde file, and you cannot add controls without switching to Design View. There is no way around that whatsoever (well, the way around is to "fake" the creation of controls, by hiding, moving, and resizing the controls. I recently created a form, for a .mde, where the detail section grows and shrinks to just contain controls as they are "added"; not obvious what to do, since the detail section cannot shrink beyond the extent of existing controls, even if they're invisible -- the trick was to set Height and Width of the control to zero, Visible to false, and then move the control up into the upper corner of the detail section, then reduce the Height of the detail section...and vice versa, as controls come and go).

Sounds like perhaps a better solution for you would be to use CreateForm and CreateControl to automate the initial creation of your form (so you don't have to hammer out 300 controls by hand), but put all the controls on there and manipulate their properties on the fly as needed.
 
Thanks a lot for your input. I did get the code to work shortly after I posted this message. However, I didn't realize that this kind of thing won't work in a .mde version. For my final release this has to be in a .mde. I can use the CreateForm method as long as I can find a way to dynamically save and delete the forms I create. Originally I abandoned this method for this reason. Do you know of any syntax to save new forms from code? Thanks again for your help.
 
Well, you can use the docmd methods DoMenuItem, RunCommand, Save, and DeleteObject to save and delete forms or whatever else. However, I don't think any of those (or CreateForm) are going to work in the .mde file either. If the final db will be a .mde, you'll pretty much have to decide what forms you want and what controls they'll have up front. You can tweak their properties any way you want though -- there is almost certainly a way to get the form to behave the way you want. It may not be as elegant of a solution as you'd hoped for, but...such is life with Access (makes some things really easy, but is a complete PITA regularly).

All I was suggesting in the last bit of my last post, was that it may be worth writing a little algorithm to generate all those text boxes on your form. That code would run **once** while you were designing the form, i.e. that code would never execute while the database was in normal use/once the db was deployed.

Good luck.
 
Hello Orion45,

I have exactly the same problem to create the control dynamically at run time on the same form.

Can I have the helping hand from you! Thanks

* VBA MS-ACCESS coding *
 
msofficebeginner,
You may have a look at the topic "CreateControl Method" in the VBA help file. There is an example code too.

________________________________________________________
Zameer Abdulla
Help to find Missing people
There’s a world of difference between editorials and advertorials
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top