Error when programmatically adding text to existing userform

run-time error '-2147319767 (80028029)
vba automation error invalid forward reference

I want to add a text box to a pre-existing user form, and also change the height of the user form in VBA Excel 2010. I want the text box added, and height changed at design time but I need to make it with code. The following code is what I have so far.

Sub Practice()
Dim hght As Single
Dim NameUserForm As String
Dim MyUserForm As Object

'Name of userform
NameUserForm = "test"

Set MyUserForm = ThisWorkbook.VBProject _
.VBComponents(NameUserForm)

hght = MyUserForm.Properties("Height")

With MyUserForm

    .Properties("Height") = hght + 25

End With

Set NewTextBox = MyUserForm.Designer.Controls.Add("Forms.TextBox.1")

    With NewTextBox
        .TextAlign = fmTextAlignCenter
        .Width = 66
        .Height = 18
        .Left = 40
        .Top = hght
    End With
test.Show

End Sub

I get an error on the following line

Set NewTextBox = MyUserForm.Designer.Controls.Add("Forms.TextBox.1")

And the error reads as:

Run-time error '-2147319767 (80028029)' Invalid forward reference, or reference to uncompiled type.

I have no idea what I'm doing wrong. I've also asked this same question on mrexcel.com, here's a link to that one http://www.mrexcel.com/forum/excel-questions/861582-use-visual-basic-applications-add-textbox-existing-userform.html

I know this is an old thread, but I ran into this error as well. It's baffling, the error gives you no hits as to what is going on. In my scenario I'm programmatically adding a pivot chart. The weird thing is, I have known working code. I was working on an update in an unrelated area and my working code started showing this error. I couldn't tell what was going on, so I started stepping through connection objects. Eventually, I repro'ed this error when enumerating through ListObjects.

Sub ViewListObjectsWithQueryTables()
Dim sh, itm
For Each sh In ActiveWorkbook.Sheets
    Dim i
    i = 1
    Do While i <= sh.ListObjects.Count
        itm = sh.ListObjects.Item(i)
        If sh.ListObjects.Item(i).SourceType = xlSrcQuery Then
            itm = sh.ListObjects.Item(i)
        End If
        i = i + 1
    Loop
Next
End Sub

Put a line break at...

i = i +1

...and inspect values.

It turned out that when I hit a particular worksheet, and tried to read ListObjects.Count, I would get the error. It had nothing to do with my own code.

Resolution

  1. Delete the horked spreadsheet
  2. Recreate

This solved my problem. And possibly explains why @Jonny Oliver was able to fix, but didn't know why. The worksheet was in a horked state. Likely during his troubleshooting references behind the scenes may have changed. As for his code, there doesn't appear to be any reason why his original code would have resulted any different than his revised code.

Hope this helps.

How to programmatically append an error to a FormGroup control , control.setErrors({ (control.errors || {}), 'newError': 'text of the error' }). You just have to get the previous errors and spread them into your new error object. Re: Update existing data using a UserForm No worries, Firstly keep R as a global variable, visible to all sub routines, do this by moving it to the very top, first line of your code, outside of all the sub routines.

If you want to change the properties of a userform as part of the code that will Show the form, you are making changes at runtime. I think your code is mixing up making changes to the design of the saved form design with making changes to the current instance of the form.

The code below handles the userform as an object. The new textbox is added to the temporary instance of the form.

Sub Practice()
Dim hght As Single
Dim MyUserForm As testUserForm
Dim NewTextBox As Control

    Set MyUserForm = New testUserForm

    hght = MyUserForm.Height

    With MyUserForm

        .Height = hght + 25

    End With

    Set NewTextBox = MyUserForm.Controls.Add("Forms.TextBox.1")

    With NewTextBox
        .TextAlign = fmTextAlignCenter
        .Width = 66
        .Height = 18
        .Left = 40
        .Top = hght
    End With
    MyUserForm.Show

    ' Do something with the form, user clicks Ok or close button
    Set MyUserForm = Nothing
End Sub

How to programmatically add controls to Windows forms at run time , Describes how to programmatically add controls to Windows forms at run time by using Visual C#. <div class="error-content text-center"> <div><span class="win​-icon When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is Text = "Run-time Controls"; If you want to change the properties of a userform as part of the code that will Show the form, you are making changes at runtime. I think your code is mixing up making changes to the design of the saved form design with making changes to the current instance of the form. The code below handles the userform as an object.

Well I got something to work. I'm not sure what I did that fixed it, but here it is for anyone else struggling with the same thing. (Note: I made some changes to variable names)

Sub DesignTimeTxtBox()

Dim txtBox As Variant
Dim NameUserForm As String
Dim hght As Single

NameUserForm = "test"

Set MyUserForm = ThisWorkbook.VBProject _
    .VBComponents(NameUserForm)

hght = MyUserForm.Properties("Height") + 25
MyUserForm.Properties("Height") = hght

Set txtBox = ThisWorkbook.VBProject.VBComponents(NameUserForm).Designer.Controls.Add("Forms.TextBox.1")
With txtBox
    .Width = 66
    .Top = 133
    .Left = 42
End With
End Sub

Overview of forms, Form controls, and ActiveX controls on a , By using forms and the many controls and objects that you can add to them, you can Use a data form when a simple form of text boxes that list the column You can attach an existing macro to a control, or write or record a new macro. link text in a Shape or text box object to a worksheet cell and dynamically display​  Excel VBA Learn how to add TextBox to UserForm programatically The code used in this video: Private Sub UserForm_Initialize() Dim cTextBox As Control Set cTextBox = Controls.Add("Forms.TextBox.1

Creating Controls at Runtime, On the Fly. Excel Training VBA 2 , At times when creating a project in Excel VBA which incorporates the use of a The syntax for the Add Method for adding Controls to a UserForm is as shown below: Property and the AutoSize Property to ensure the full Caption text is displayed. This method is far simpler and less prone to errors than going down the  This document, titled « VBA - Adding controls dynamically into a Userform », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM ().

Microsoft Visual C# 2005 Unleashed, as well as buttons for creating new records and deleting the current record. in the report to live runtime data sources programmatically or even at design time. providing error and help text to other controls on a form, and even things like  - There are many different controls…that you can add to a user form to allow user input.…In this movie, I will show you how to add a textbox,…which allows a user to type in text…that you would then transfer to a worksheet.…My sample file is the AddTextBox.xlsm file,…and you can find it in the chapter six folder…of the exercise files collection.…I won't be using the worksheet

API Documentation, When set to firstError (default), only first error from each field will be gathered. [​register]) return ( <form onSubmit={handleSubmit(onSubmit)}> <input type="text" This function allows you to dynamically set registered input/select value. swap, move, insert } = useFieldArray( { control, // control props comes from useForm  Re: Add Textbox On Form Programmatically. thank alot, sorry my fault, i didn't tell specifically where the vba code apply to. i believe this code only apply to Excel. i'm creating in MS Access and using the code below but it doesn't work, please help. thanks

Comments
  • You say "height changed at design time". Do you mean "design time" or "run time"? You attempt to show the form, so I'd guess you mean the latter.
  • Sorry I'm pretty novice with vba. I want everything done in design time so it's permanent.
  • Thanks for taking the time to look through my code and respond. You mention that "The new textbox is added to the temporary instance of the form." What changes would need to be made to the code to make permanent changes to the userform. Again the changes need to be made with code. Making the changes in design view is not what I'm looking for.