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!

Add New Control To A Form During Run Time

Status
Not open for further replies.

drfox

Technical User
Jul 10, 2000
3
US
Is there a way to add a new control to a form during run time?  I know you can do that in VB using the add method of a collection object, but I can not find a similar method in VBA?
 
Hi, <br>&nbsp;&nbsp;&nbsp;I don't know if you can add a control during run time, but if you create a control during design time and set it to ctl.Visible = False, then set it back to Visible = True during run time it will work the same way. Take note of the Name of the control<br><br><br>i.e <br>Private&nbsp;&nbsp;Sub Some_On_Click()<br><br>ctlName.Visible = True<br><br>End Sub<br><br>Cheers Radman
 
Thanks a lot for the quick response.&nbsp;&nbsp;The idea is to create the control only when there is a need to.&nbsp;&nbsp;For example I have a form that has a map of cities.&nbsp;&nbsp;The End user can click on any number of cities. Only those cities that has some entered values will be displayed on the map using lables.&nbsp;&nbsp;<br><br>Because the average end user usually enters data of a max of 10 cities, it make sense to only enter the records of selected cities and create labels on the fly on the form to show the values. In VB you can do that as well as in VFP and other languages.&nbsp;&nbsp;I was wondering about VBA?
 
Well then, <br><br>To create a control source in run i haven't done it, but if you look in the help files you will find it there.<br><br>if you are running Access '97 <br><br>Go the index -&gt;&nbsp;&nbsp;&nbsp;&nbsp;controls, creating then select the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(CreateControl, CreateReportControl Functions) out of the dialog box<br><br><br>thats where it explains how to do it.<br><br>If you can't find that example here is the syntax and example for the files.<br><br>Syntax<br><br>CreateControl(formname, controltype[, section[, parent[, columnname[, left[, top[, width[, height]]]]]]])<br>CreateReportControl(reportname, controltype[, section[, parent[, columnname[, left[, top[, width[, height]]]]]]])<br><br><br><br>The following example first creates a new form based on an Orders table. It then uses the CreateControl function to create a text box control and an attached label control on the form.<br><br>Sub NewControls()<br> Dim frm As Form<br> Dim ctlLabel As Control, ctlText As Control<br> Dim intDataX As Integer, intDataY As Integer<br> Dim intLabelX As Integer, intLabelY As Integer<br><br> ' Create new form with Orders table as its record source.<br> Set frm = CreateForm<br> frm.RecordSource = &quot;Orders&quot;<br> ' Set positioning values for new controls.<br> intLabelX = 100<br> intLabelY = 100<br> intDataX = 1000<br> intDataY = 100<br> ' Create unbound default-size text box in detail section.<br><br>Set ctlText = CreateControl(frm.Name, acTextBox, , &quot;&quot;, &quot;&quot;, _<br> intDataX, intDataY)<br> ' Create child label control for text box.<br> Set ctlLabel = CreateControl(frm.Name, acLabel, , ctlText.Name, _<br> &quot;NewLabel&quot;, intLabelX, intLabelY)<br> ' Restore form.<br> DoCmd.Restore<br>End Sub<br><br><br>Cheers Radman
 
Thanks a lot for the swift response. I will try that and let you know the results.<br>&nbsp;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top