Add a Label or Textbox with VBA

F

FGM

windows 2000, Access 2002

I would like to add a label or textbox using vba. It tells me you need to
be in design view. I want to check to see if a textbox or label exists and
if it does not to add it and set the properties.
Can this be done? I tried the following. I want to do this when a user is
openning the form.
thanks.

I tried this:

Private Sub Form_Open(Cancel As Integer)
Dim txt1 As Access.TextBox
Dim frm1 As Access.Form

'Set frm1 = "frmTest"
Dim intLeft As Integer
Dim intOffset As Integer
Dim intwidth As Integer
Dim intHeight As Integer
Dim intLastTop As Integer

intLeft = 144
intOffset = 72
intwidth = 4320
intHeight = 288

Set txt1 = CreateControl("frmTest", acTextBox)
With txt1
..Left = intLeft
..Top = intLastTop + intHeight + intOffset
intLastTop = .Top
..Width = intwidth
..Height = intHeight
..NAME = "txtName"
End With
End Sub
 
D

Douglas J. Steele

As the message is telling you, the form needs to be in Design view in order
to add a control.

To be honest, though, using CreateControl in a production application is a
very unusual thing. Far more common is to ensure that all possible text
boxes and labels exist, hide the ones you don't need and toggle their
visibility when you need them.
 
F

FGM

Thank you. However, I really want to be able to check and see if the user
deleted my label or textbox and if they did I want to add another one.
I have a label that has my information on it and if the user I gave the
database to deletes my information I would like it recreated.
thanks.
 
D

Douglas J. Steele

If you're concerned about the user having changed your form, give them an
MDE (or ACCDE) rather than an MDB (or ACCDB).

You cannot put the code in the form's Open event, as when you open the form,
it's not going to be in design mode.
 
J

Jim Burke in Novi

Why would your user ever go into Design mode? That's the only way they could
ever delete a control. In normal production circumstances the user shouldn't
have the option of going into design mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top