Storing part of a vba code line in a variable

B

Bill Cunningham

Hello,

I'm trying to get a macro to check the values of a set of
textboxes in a vba userform, and create a single string of
their contents.

Any one or more of the textboxes might have text in them,
and the set of textboxes may be filled in multiple times
(basically, there are 5 available boxes for data. When the
user has filled them all in they have the choice of ending
there or using more boxes. If the click "More", the macro
will create a string of all the current values, then clear
them and allow the user to enter 5 more. This may continue
indefinately.)

In order for this to work, though, I need to put the vba
code representing the name of the userform textboxes into
a variable. The textboxes are named:

txtListItem1, txtListItem2, ... etc.

Here is the code I'm trying to make work:

----------------------------------------

Sub WriteList()

Dim ItemName as String, ItemNo As Integer

For ItemNo = 1 To 5

ItemName = "txtListItem" & ItemNo

If Len(Trim(ListString)) = 0 Then
If Len(Trim(ItemName)) <> 0 Then
ListString = ItemName
End If
Else
If Len(Trim(ItemName)) <> 0 Then
ListString = ListString & vbCr & ItemName
End If
End If

Next

End Sub

-------------------------------------

Of course, what is happening is the varible "ItemName" is
just being filled with the actual name of the textbox, not
the value of the textbox.

How can I get the ItemName variable to take on the value
of the textbox I'm naming?

Thanks!
 
H

Helmut Weber

Hi,
maybe you are looking for the Result-Property
of formfield of type 70!
wdFieldFormTextInput = 70 (&H46)

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, XP, NT4.0, W98
 
A

Astrid

Hi Helmut,

How about:

----------------------------------------------
Dim i As Integer
Dim sResult As String
Const csTextBoxName As String = "TextBox"

For i = 1 To 3
sResult = sResult & Me.Controls(csTextBoxName & i).Text
Next i
Debug.Print sResult
----------------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/
 
B

bill cunningham

That was the answer, Astrid, thank you very much!

-----Original Message-----
Hi Helmut,

How about:

----------------------------------------------
Dim i As Integer
Dim sResult As String
Const csTextBoxName As String = "TextBox"

For i = 1 To 3
sResult = sResult & Me.Controls(csTextBoxName & i).Text
Next i
Debug.Print sResult
----------------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post
all follow-ups to the newsgroup.
 

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