Solved

Creating controls on the fly

Posted on 2011-03-25
7
217 Views
Last Modified: 2012-05-11
Hi,

I have a couple of questions about creating controls at runtime.

My logic is this:

1.      Create a new form
2.      on new form create new controls (combo)
3.      the code for creating these controls is within a loop
4.      set the recordset of the controls for each control during each pass of the loop

So far I managed to do steps 1,2,3.  However I cannot set up the recordset.

Here is the code I have:

    ' Create new form with Orders table as its record source.
    Set frm = CreateForm
    ' Set positioning values for new controls.
    intLabelX = 100
    intLabelY = 100
    intDataX = 1000
    intDataY = 100
   
    For intCounter = 1 To 10
   
        ' Create unbound default-size text box in detail section.
    Set ctlText = CreateControl(frm.Name, acComboBox, , "", "", _
        intDataX, intDataY)
        ctlText.Name = intCounter
        intDataY = intDataY + 400
       
        ctlText.Recordset = GetADORecordset("SELECT receipt_number, running_qty, identifier, order_number FROM " & _
"tbl_receipt")
       
            ' Create child label control for text box.
    Set ctlLabel = CreateControl(frm.Name, acLabel, , _
         ctlText.Name, "NewLabel", intLabelX, intLabelY)
         intLabelY = intLabelY + 400
    Next

Questions:

1.      What is best way to set up the recordset of each of the controls
2.      Can the form name be sepcififed (defaults to Form1, Form2, etc…)  I’ve tried frm.name = “new name” however it gives a read only message.

Many Thanks.
0
Comment
Question by:andyw27
7 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 35216499
You apply a recordset to a Form , not a control.

The controlsource is just the field name from the recordset.

Look here for some sample code:
http://msdn.microsoft.com/en-us/library/aa221170(office.11).aspx
0
 
LVL 84
ID: 35216502
You have to "set" a recordset. Assuming your GetADORecordset returns an ADO recordset:

Set ctl.Recordset = GetADORecordset(blah blah)

I'm not sure about the form name. I long ago gave up trying to do things of this nature, due to the troubles accompanied with the methods (i.e. instability, decompilation, etc etc)

0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 35216527
ditto with LSM
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:andyw27
ID: 35216672
Yes I agree, not the perfect solution.

It would work okay if I could change this to include a variable:

Forms!Form1.cbo & intKey

so it would like this to the system:

Forms!Form1.cbo1
0
 
LVL 77

Expert Comment

by:peter57r
ID: 35216945
To do
Forms!Form1.cbo & intKey


you do..
Forms!Form1.controls("cbo" & intKey)

(I will just add my agreement to the previous 'don't do this' comments. It would be a 'last resort' for me to use this approach)
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35217108
^

Yes, it is always helpful to state *Why* you need to do this, ...perhaps there is a better alternative...
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 35217340
You may have to do this:

Forms!Form1.Controls("cbo" & Cstr(intKey))

While Access is very good about casting and converting, I've had issues in the past referring to controls in that fashion. Probably a fluke, but I got into the habit of using the Cstr conversion when doing this.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now