Incrementing field value to get variable from form problem

Marcin

New member
Local time
Yesterday, 16:54
Joined
May 24, 2011
Messages
3
Hi,
I have a pretty crazy problem when trying to do the following. I have a simple form with several textboxes. I would like to read the textbox value one by one by incrementing the textbox number (say we have 5 textboxes numbered Text1 to Text5. Here is the example of the code:

Code:
Private Sub Command2_Click()
For i = 1 To 5
Readtxt = [Forms]![Form1]![Text" & i & "]
MsgBox Readtxt
Next i
End Sub

However, this code gives me an error, because it does not recognizes the variable "i". I tried almost everything: double quotes, simple qoutes around double quotes, etc...but nothing works. Not even substituting a string with the textbox name e.g. String1 = "Text1" in the Readtxt expression.

Anybody has a briliant idea?

Thanks,

Marcin
 
Try the following code:

Code:
Dim i As Byte
Dim Readtxt As String
 
For i = 1 To 5
    Readtxt = Forms!frmNewTest.Controls("Text" & i)
    MsgBox Readtxt
Next i

Note the declaration of each variable used. The variable "i" can be defined as the type of variable you need, but in this case if the count of your text boxes is not going to exceed 256 then a byte type variable will be sufficient.
 
Dear Mr. B,
This works greatly!
Was my mistake not to declare the "i" variable, or simply the syntax I used within the brackets was wrong?

Thanks again,

Marcin
 

Users who are viewing this thread

Back
Top Bottom