• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12800
  • Last Modified:

VBA - Create Text Box

Hi,
Can someone tell me how I would create a text box on my form using VBA? Would there also be a way to position it where I like?
Thanks
0
andyb7901
Asked:
andyb7901
  • 4
  • 3
  • 2
  • +1
1 Solution
 
oldmanbimCommented:
Take a look at
CreateControl(formname, controltype[, section[, parent[, columnname[, left[, top[, width[, height]]]]]]])
0
 
oldmanbimCommented:
PS: You'll need to be in design mode to use this.
0
 
Jeroen RosinkCommented:
You might take a look at:
How to Programmatically Manipulate a UserForm
http://support.microsoft.com/default.aspx?scid=kb;en-us;185774

Jeroen
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
andyb7901Author Commented:
oldmanbim - do you mean I would need to be in the VB editor? I am doing this in Acces?

Jeroen - I would like to know how to do this in Access if possible?
0
 
Jeroen RosinkCommented:
here some code to add textbox to control:
Sub CommandButton1_Click()
'http://support.microsoft.com/default.aspx?scid=kb;en-us;185774
'Uses Microsoft Visual Basic for Applications Extensibility"
      ' Declare variables.
      Dim mynewform As Object
      Dim mycheckbox As Object

      ' Add a Textbox to the new UserForm.
      Set myTextBox = Me.Controls.Add("Forms.TextBox.1")

      With myTextBox
         .Left = 10
         .Top = 10
         .Height = 20
         .Width = 60
         .Visible = True
      End With

   End Sub

regards
Jeroen
0
 
andyb7901Author Commented:
It does not like the line, specifically the ".add" part.

Set myTextBox = Me.Controls.Add("Forms.TextBox.1")
0
 
oldmanbimCommented:
I would suggest doing this in VBA and not by using macros in Access.  

Note that the "cmdMakeTBX_Click" is NOT called from frmMyForm

' Creates a text box of the name "tbxTest" using the control source "txtTest"
' The text box is 1 in wide, 2 inches high, placed 0.5 inches from the
' top and left of the detail
Private Sub cmdMakeTBX_Click()
    Const TWIPS As Integer = 1440 ' text box dimensions are in twips
    Dim ctl As Control
    Dim intLeft As Integer
    Dim intTop As Integer
    Dim intWidth As Integer
    Dim intHeight As Integer
    intLeft = 0.5 * TWIPS
    intTop = 0.5 * TWIPS
    intWidth = 1 * TWIPS
    intHeight = 2 * TWIPS
   
    DoCmd.OpenForm "frmMyForm", acDesign
    Set ctl = CreateControl("frmMyForm", acTextBox, acDetail, , "txtDesc1", intLeft, intTop, intWidth, intHeight)
   
    ctl.Name = "tbxTest"
    DoCmd.Close acForm, "frmMyForm", acSaveYes
End Sub

There may be other ways to do this, but this works.
0
 
andyb7901Author Commented:
Thanks for the code! Is there anyway to run this so it runs for the current form, and not opening another form to be updated?
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Also, I'd wonder about the need to do this at runtime. Unless you're allowing users to create their own forms, then there is no reason to create a control at runtime in Access.

I'd also argue that if you need to allow users to create their own forms, you'd be muuuucccchhhh better off in another programming environment (VB6 or .NET, for example) since those platforms allow you to do this much more easily. Adding controls at runtime can cause corruption and will definitely bloat the application, which can lead to slowdowns and, again, corruption.

You can also add controls to your form, then Show/Hide them as needed using the .Visible property.

0
 
oldmanbimCommented:
You might be able to get this to work with the current form, but I've never played with this function very much.  Usually, if I need to "create" a text box on the fly, I do that by making a "spare" invisible textbox become visible when I need it.

I would have to agree with LSM Consulting, however if you're not familiar with VB6 etc already, the "invisible" textbox might be the way to go.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now