positioning problems with dynamic VBA userforms

D

drako

Hi,

I'm attempting to create some dynamic controls within a Word VBA
userform.

What I am doing is reading in filenames from a directory, parsing the
filename, then creating a list of labels and textboxes based upon the
directory contents.

However, a strange problem I am having is that the LAST textbox to be
created always displays at the top left hand corner of the userform
(and does not display the text content either).

I really can't figure out what is wrong here.

Any suggestions ?

Code Extract below:

////////////////////////////////////

Private Sub UserForm_Initialize()

Dim t As Integer

t = 40

Dim counter As Integer

counter = 0

Dim tempString As String
Dim FName As String
FName = Dir(assetpath & "*.doc")
While FName <> ""

tempString = left(FName, Len(FName) - 4)
tempString = Replace(tempString, "_", " ")

FName = Dir

Dim mylabel As Label
Dim mylabel2 As Label
Dim myTextBox As TextBox


Set mylabel = asset_allocation_current.Controls.Add _
("Forms.Label.1", "asset_allocation_label_" & counter)

With mylabel
.left = 45
.Height = 16
.Top = t + (25 * counter)
.Width = 240
.Caption = tempString
End With

Set myTextBox = asset_allocation_current.Controls.Add _
("Forms.TextBox.1", "asset_allocation_boxes_" & counter)

With myTextBox
.left = 288
.Height = 16
.Top = (t + (25 * counter))
.Width = 60
.Text = counter
End With

Set mylabel2 = asset_allocation_current.Controls.Add _
("Forms.Label.1", "asset_allocation_label2_" & counter)

With mylabel2
.left = 350
.Height = 16
.Top = t + (25 * (counter))
.Width = 10
.Caption = "%"
End With

counter = counter + 1

Wend


End Sub
 
D

drako

I've just tried to add some debugging code to output the positions of
each box:

Debug.Print "counter: " & counter & " top: " & (t + (25 * counter))

The results are:

counter: 0 top: 40
counter: 1 top: 65
counter: 2 top: 90
counter: 3 top: 115
counter: 4 top: 140
counter: 5 top: 165
counter: 6 top: 190
counter: 7 top: 215
counter: 8 top: 240
counter: 9 top: 265
counter: 10 top: 290
counter: 11 top: 315
counter: 12 top: 340
counter: 13 top: 365

Therefore, I just can't figure out why the No.12 textbox is showing in
the top left hand corner.

HHmmmm.....
 

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