Add Text Box to Userform using VBA

mswoods1

Board Regular
Joined
Aug 6, 2010
Messages
60
I'm trying to add a text box to a userform using VBA. So far it is not working.

Here's what I got from searching the interwebs:

Code:
    Dim cCntrl As Control
    Set cCntrl = Me.Controls.Add("Forms.TextBox.1", "MyTextBox", True)
    With cCntrl
        .Width = 150
        .Height = 25
        .Top = 10
        .Left = 10
        .ZOrder (0)
    End With

This is presumably giving me a textbox, but I'm not able to do anything with it. I can't seem to edit the text or anything. And I'd prefer for it to be an actual "TextBox" object in VBA, so I can see all the properties that I can manipulate and so forth... the "control" object doesn't have a "Text" property, for example.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi mswoods1,

Based on your description, I think whatever it is you've currently done is simply displaying an empty userform-- you SHOULD see a textbox that you can type in.

I have a feeling though, that rather than looking to create a dynamic userform that changes at runtime, you're simply wanting to design a basic, functional, introductory userform (If my assumption is incorrect, I apologize.)

I went through this experience myself several months ago and found google and forum searches for a basic "how to" to be unhelpful (although this may say more about my search skills than it does about available info). In this spirit, I'm writing up this little introductory tutorial.

So again, assuming you're not looking to create userform controls at runtime, there is no reason to create them programatically (all that height=yy, width=xx bunk). The UserForm design mode is the way to go. Here's what to do...

Go to the VB Editor, Insert->Module then add:
Code:
Sub abc()
    UserForm1.Show
End Sub

Now, Insert-> Userform. Then View->Toolbox. Here is where you click/drag/drop various controls onto your form. What you see is what you get. Note at the top of the Project Explorer window (under "Project- VBA Project") are two buttons that allow you to toggle between the userform design mode and userform module code.

So, for a simple example, we'll have the user enter a number into a textbox and will display that number times 1000 in the second one:

Create two textboxes, the first one on top of the second. You can add labels (seperate controls) to give the user instructions. For the second textbox, where we'll display the answer, right click it->properties. Here you can see various design attributes that can be changed for this control. Set enabled=false (so user can't type in it).

To the right of the first textbox, add a CommandButton control with wording "Calculate". Double click this CommandButton. This takes you to the userform-code view and automatically creates a commandbutton click event. Here type:
Code:
Private Sub CommandButton1_Click()
    TextBox2.Value = TextBox1.Value * 1000
    TextBox1.Value="" 

End Sub

Here in the code you can reference controls by name and see their various properties after the "." when typing. There are no declarations needed if they are created in design mode.

As you noted there is also ".Text" for textboxes. Off the top of my head, not sure what the difference is b/w it and ".Value".

These controls are actually ActiveX , not VBA-- that's why the documentation in the VBA reference manual is so sparse. Here's some info from MSFT I found helpful [http://support.microsoft.com/kb/829070

HTH.
 
Upvote 0
mswoods1

Where is the code located?

Normally you would put this type of code in the userform's Initialize event.

There should be
nothing stopping you using properties like Text in the code but you won't see them in the Intellisense because you've only declared it as a Control, not specifically a Textbox control.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top