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

Making controls to a form by VBA

Status
Not open for further replies.

someotherdude

Programmer
Oct 31, 2002
5
FI
Hi!

Is it possible to make controls like text boxes and buttons to a form by vba coding? For example, clicking a button on a form makes a new text box to the form.

If this is possible, how can it be done?

 

Thanks for your quick reply! Actually I found the help topic after I had sent the question. Now I know how to add new controls to a new form, but I haven't managed to add controls to an existing form.

When try the following code I keep getting error message:

Run-time error '29054':
Microsoft Access can't add, rename, or delete the control(s) you requested.

Here is the code sample:
Code:
Private Sub btnAdd_Click()

Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
Dim strName As String

strName = "frmTest"
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100

DoCmd.OpenForm strName, acDesign
Set ctlText = CreateControl(strName, acTextBox, , "", "", intDataX, intDataY)
Set ctlLabel = CreateControl(strName, acLabel, , ctlText.Name, "NewLabel", intLabelX, intLabelY)
DoCmd.OpenForm strName, acNormal

End Sub

What is wrong with the code? There is a form frmTest where the code is called and where I would like to add the controls.
 
HI

Not an expert on this, have only done it once before!, but, I think you need something like:

Dim frm as Form
... etc as you have it

DoCmd.OpenForm strName, acDesign
Set frm = Forms(strName) <--- see here and next line
Set ctlText = CreateControl(frm, acTextBox, , &quot;&quot;, &quot;&quot;, intDataX, intDataY)
Set ctlLabel = CreateControl(frm, acLabel, , ctlText.Name, &quot;NewLabel&quot;, intLabelX, intLabelY)
' *** need to close and save Form in here?
DoCmd.OpenForm strName, acNormal

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Thank you for your effort, but I have to say that I didn't get it to work. Now I get

Compile error:
Type mismatch

and the CreateControl is highlighted in Visual Basic window.
I have tried different variations of this code, but nothing seems to work.

Code:
Private Sub btnAdd_Click()

Dim ctlLabel As Label
Dim ctlText As TextBox
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
Dim strName As String
Dim frm As Form

strName = &quot;frmTest&quot;
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100

DoCmd.OpenForm strName, acDesign
Set frm = Forms(strName)
Set ctlText = CreateControl(frm, acTextBox, , &quot;&quot;, &quot;&quot;, intDataX, intDataY)
Set ctlLabel = CreateControl(frm, acLabel, , ctlText.Name, &quot;NewLabel&quot;, intLabelX, intLabelY)
DoCmd.Save
DoCmd.Close
DoCmd.OpenForm strName, acNormal
End Sub

 
HI

AS I say I have only done this once before, if you can be patient I will dig out that project and check how I did it.

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top