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

Add Text Box control to a Form at RunTime 3

Status
Not open for further replies.

LittleSmudge

Programmer
Mar 18, 2002
2,848
GB
I'm using A2k ( ADO etc )

I need to add text box controls to a form at runtime.

I realise it's basically appending Control objects to the Form.Controls collection - but I've searched the 'yelp' system and I can't find anything to help me.

Any ideas folks ?



G LS

 
Hi

I find Access2000 help very poor compared to Access97, unless you know what you are looking for you stand no chance!

CreateControl is what you need:

CreateControl, CreateReportControl Methods



The CreateControl method creates a control on a specified open form.


The CreateReportControl method creates a control on a specified open report.

For example, suppose you are building a custom wizard that allows users to easily construct a particular form. You can use the CreateControl method in your wizard to add the appropriate controls to the form.

Syntax

CreateControl(formname, controltype[, section[, parent[, columnname[, left[, top[, width[, height]]]]]]])

CreateReportControl(reportname, controltype[, section[, parent[, columnname[, left[, top[, width[, height]]]]]]])

The CreateControl and CreateReportControl methods have the following arguments.

Argument Description
formname, reportname A string expression identifying the name of the open form or report on which you want to create the control.
controltype One of the following intrinsic constants identifying the type of control you want to create. To view these constants and paste them into your code from the Object Browser, click Object Browser on the Visual Basic toolbar, then click Access in the Project/Library box, and click AcControlType in the Classes box.
Constant Control
acLabel Label
acRectangle Rectangle
acLine Line
acImage Image
acCommandButton Command button
acOptionButton Option button
acCheckBox Check box
acOptionGroup Option group
acBoundObjectFrame Bound object frame
acTextBox Text box
acListBox List box
acComboBox Combo box
acSubform Subform
acObjectFrame Unbound object frame
acPage Page
acPageBreak Page break
acCustomControl ActiveX control
acToggleButton Toggle button
acTabCtl Tab control
section One of the following intrinsic constants identifying the section that will contain the new control. To view these constants and paste them into your code from the Object Browser, click Object Browser on the Visual Basic toolbar, then click Access in the Project/Library box, and click AcSection in the Classes box.
Constant Section

acDetail (Default) Detail section
acHeader Form or report header
acFooter Form or report footer
acPageHeader Page header
acPageFooter Page footer
acGroupLevel1Header Group-level 1 header (reports only)
acGroupLevel1Footer Group-level 1 footer (reports only)
acGroupLevel2Header Group-level 2 header (reports only)
acGroupLevel2Footer Group-level 2 footer (reports only)
If a report has additional group levels, the header/footer pairs are numbered consecutively, beginning with 9.
parent A string expression identifying the name of the parent control of an attached control. For controls that have no parent control, use a zero-length string for this argument, or omit it.
columnname The name of the field to which the control will be bound, if it is to be a data-bound control.
If you are creating a control that won't be bound to a field, use a zero-length string for this argument.
left, top Numeric expressions indicating the coordinates for the upper-left corner of the control in twips.
width, height Numeric expressions indicating the width and height of the control in twips.


Remarks

You can use the CreateControl and CreateReportControl methods in a custom wizard to create controls on a form or report. Both methods return a Control object.

You can use the CreateControl and CreateReportControl methods only in form Design view or report Design view, respectively.

You use the parent argument to identify the relationship between a main control and a subordinate control. For example, if a text box has an attached label, the text box is the main (or parent) control and the label is the subordinate (or child) control. When you create the label control, set its parent argument to a string identifying the name of the parent control. When you create the text box, set its parent argument to a zero-length string.

You also set the parent argument when you create check boxes, option buttons, or toggle buttons. An option group is the parent control of any check boxes, option buttons, or toggle buttons that it contains. The only controls that can have a parent control are a label, check box, option button, or toggle button. All of these controls can also be created independently, without a parent control.

Set the columnname argument according to the type of control you are creating and whether or not it will be bound to a field in a table. The controls that may be bound to a field include the text box, list box, combo box, option group, and bound object frame. Additionally, the toggle button, option button, and check box controls may be bound to a field if they are not contained in an option group.

If you specify the name of a field for the columnname argument, you create a control that is bound to that field. All of the control's properties are then automatically set to the settings of any corresponding field properties. For example, the value of the control's ValidationRule property will be the same as the value of that property for the field.

Note If your wizard creates controls on a new or existing form or report, it must first open the form or report in Design view.

To remove a control from a form or report, use the DeleteControl and DeleteReportControl statements.


Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Hi,

Access 97 has the CreateControl command you could try searching A2K help for this?

Below is example from A97 help...

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.

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

There are two ways to write error-free programs; only the third one works.
 
Ken, you're always one step ahead...


There are two ways to write error-free programs; only the third one works.
 
Now then chaps don't squabble !

Thank you for letting KenReay know this post was helpful.
Thank you for letting GHolden know this post was helpful.


A star for both of you - can't say fairer than that.

In the spirit of furthering the body of knowledge - here's what I did with your guidance.

I actually need to initiate the process of adding the control to a form from that form itself. ( Note all of the examples provided show the control being added to a new form. )

The CreateControl method requires the form to be in design view.
Now it's "diffecult" for a form to be running code - recognise that it needs the new control, switch to design view and carry on running code to add the control !

So- thought I, run the following line of code on the form ( Lets call it frmGrow )
Do.Cmd.OpenForm "frmAddControlToGrow",,,,,acHidden

Then in frmAddControlToGrow_Open event put code to

Collect data from frmGrow
Close frmGrow
Open frmGrow in Design Mode
Add Control to frmGrow - as per the CreateControl method detailed in earlier posts

Close frmGrow with Save
Open frmGrow in Form View


All seems okay - but it didn't run
I could extend the Detail.Height to make room for the new control - no problem but it always stops at the CreateControl line saying "Can't Add, rename or Delete the control"


Finally realised that it works if the frmGrow is already in design mode and frmAddControlToGrow is opened from the database window. So it must be something to do with the frmGrow still running the code that opened the form in the first place.

SOLUTION
frmGrow opens frmAddControlToGrow

frmAddControlToGrow_Open event does the following
Gathers data from frmGrow
Closes frmGrow
Sets frmAddControlToGrow.TimerInterval = 10
nothing else

Then the On_Timer event
Opens frmGrow in design mode
Adds the control ( sucessfully )
Saves frmGrow
Closes itself.


'ope-that-'elps.








G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
Good work, have a star youself.



There are two ways to write error-free programs; only the third one works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top