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

Adding Controls at runtime.

Status
Not open for further replies.

StormbringerX

Programmer
Dec 14, 2000
102
US
Hello everyone, it's been a while.

My problem is this: I'm adding textboxes to a form at runtime. Using the following code:

Private Sub AddBoxesForTaxes()

Dim intNumTaxes As Integer
Dim intLoopit As Integer

Let intNumTaxes = 10
Dim intTop As Integer
Dim intLeft As Integer
Dim intHeight As Integer
intTop = 360
intHeight = 285
intLeft = 120
Dim strTextBoxName As String

For intLoopit = 1 To intNumTaxes
strTextBoxName = "frmPetitionRefundtxtbox" & intLoopit
frmPetitionRefund.Controls.Add "VB.TextBox", strTextBoxName, Frame(2)
MsgBox strTextBoxName
With frmPetitionRefund.Controls(strTextBoxName)
.Top = intTop
.Left = intLeft
.Height = intHeight
.Visible = True
.Text = intLoopit ' So I can see that it's doing something

End With
intTop = intTop + 480
Next
'frmPetitionRefundtxtbox2.Text = "This is 2"
End Sub

The textboxes are added without any problem and display correctly. The msgbox is in there just so that I could be sure that it was adding them using the names that I wanted, which it is.

However anytime I try to pull the form to the screen with a line of code that changes any of the textboxes properties, (see the last remarked line where I attempted to set the text property) I get the error 'Compile Error: Variable not defined' The error does not occur at the time I try to access the text box properties, it is displayed as soon as I try to pull the form up.

It displays properly as long as I do not try to access any of the properties of the textboxes through code.

I searched the FAQ's and the forum but found nothing that would help me out.

Any suggestions will be welcomed and appreciated.

Dave
 
Unless I'm missing something, you have to refer to the instance of the control array.

frmPetitionRefundtxtbox(0).Text = "This is 0"
frmPetitionRefundtxtbox(1).Text = "This is 1"
frmPetitionRefundtxtbox(2).Text = "This is 2
 
Actually, I'm not using an arry, I'm building textboxes with unique names. Using the For/Next loop, appending the textbox name with the number of the loop, and creating it.

What's being created is:

frmPetitionRefundtxtbox1
frmPetitionRefundtxtbox2
frmPetitionRefundtxtbox3

I will probably change this to an array because the loop WILL be variable depending on where it's installed, but I don't understand why I'm getting the error...

This is more of a nuts and bolts question, you know, why doesn't it work? Or am I missing something.


 
Does this help?

Code:
If you add a control at run time, you must use the exclamation syntax to reference properties of that control. For example, to return the Text property of a control added at run time, use the following syntax:

userform1!thebox.text
I alway's use control array's, so I'm no expert on this subject.
 
Which is the same as

form.Controls(thebox).text

the syntax that StormbringerX correctly used to to position and reveal the textboxes - and then abandons in favour of gett ing an error ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top