An Interesting Excel UserForm Question

D

DennisE

I have a UserForm on which I've placed a great many TextBoxes
named TextBox01 through TextBox50, with Tabindex 0 through 49,
respectively. I now want to write their contents to a file and have
written the following real simple code after opening a sequential file:

Dim ctl as Control
For Each ctl in UserForm.Controls
if Typename(ctl) = "TextBox" then Print #1, ctl.Text
Next
Close #1

What happens, however, is that the resultant file contains the
contents of TextBoxes in a seemingly random order, which
has no relationship to the alphanumeric ordering of the TextBox
names or their respective Tabindexes. When I pull up the list
of controls using Project Explorer, they all appear in proper
alphanumeric order. My immediate work-around has been
to modify the above procedure to extract the right two characters
of the control name and use these as a subscript for a one-
dimensional array to temporarily store the data, and then to
print the temporary file in sequential order. My question is,
is there a way to force Excel to inherently reference the
TextBoxes so that I do not have to resort to messy code
and have the simple version above do the job?

-- Dennis Eisen
 
H

Heiko

Dim n As Long
Dim msg As String
For n = 1 To 50
msg = msg & Me.Controls("Textbox" & Format$(n, "00")) & vbCrLf
Next

Heiko
 

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