using an array to add several textboxes

casey

Registered User.
Local time
Today, 08:16
Joined
Dec 5, 2000
Messages
448
I'm trying to add the values in several textboxes by using a variable array that concatenates the string "Text" & the incremented value(i), adds the value and then loops through code until the final textbox is evaluated. My code looks like this:

Private Sub Combo33_Click()
Dim i As Integer
Dim CompCost As Integer
Dim TextID As String
i = 236
Do While i < 266
TextID = "[" & "Text" & i & "]"
CompCost = Me!TextID
i = i + 1
Loop
txtCompCost = CompCost
End Sub

During the first pass through the code, when it comes to the line,
CompCost = Me!TextID,
an error informs me that object "[Text236]" can't be found. , I think that this may be the problem because it's viewed as a string. Any Ideas?? Thanks.
 
I suggest you replace the code in the do while loop with this

Do while i < 236
TextID = "Text" & i
CompCost = Me.Controls(TextID)
i = i + 1
loop
 
You are victim of a tremendous syntax error, since variable fieldnames have to be put in brackets as the following code demonstrates. You should also omit the []-brackets and change your code as follows:

Dim i As Integer
Dim CompCost As Integer

for i = 236 to 265
CompCost = Me("TextID" & ltrim(str(i))
next i
 
Thanks for your help. Both ways worked fine. I really do appreciate it. Thanks again.
 

Users who are viewing this thread

Back
Top Bottom