Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about a for loop 1

Status
Not open for further replies.

netooi25

Programmer
Jul 30, 2004
25
US
I have a bunch of text boxes on a report called
id_1, id_2, id_3, etc...

is it possible to use a for loop to access these text boxes
i'm doing it the long way right now and i think thats probably the way i'll have to do it. i'm just curious.

ex. of long ay.
if (num == 1)
id_1 = ...
if (num == 2)
id_2 = ...

is this possible?
ex. for (i = 1; i < num_id; i++)
'access the relevant box here.

thanks for any help
 
netooi25,

I think this is along the lines of what you are looking for:
Code:
Private Sub cmdTest_Click()
Dim Cntrl As Control
Dim CurrentControl As String
For Each Cntrl In Forms(0).Controls
    If Left(Cntrl.Name, 3) = (id_) Then
        CurrentControl = Right(Cntrl.Name, 1)
        'Put your code here
    End If
Next Cntrl
End Sub

Please let me know if it is not, and I can help you further.

Thanks,

Fred
 
or

[tt]dim lCounter as long
for lCounter=1 to N ' your last box
debug.print me("id_" & lcounter).value
next lCounter[/tt]

BTW - Welcome to Tek-Tips!

To get the most out of the membership, have a look at FAQ222-2244. And check also out the other Access fora for Access questions (see the Related Forums box at right)

Good Luck!

Roy-Vidar
 
If I may elaborate on Roy's idea, if the example you gave , is indicative of your If/then format then, I may suggest.

dim lCounter as long
for lCounter = 1 to N ' your last box
If (num == lCounter ) 'this may need re-syntaxing(not sure what it is, control variable?)
Me.("id_" & lCounter) = ...
next lCounter

Hope this helps, thank-you Roy!
 
Ok.. thats really cool, i didnt know you could do that..

Is it possible to access fields from a recordset like that also?

say for example i have a recordset called rtemp

and the fields in the table query are called id_1, id_2, id_3, etc...

can i make a for loop again to access the elements in the recordset..

for (i = 0; i < numid; i++)
if not isnull(rtemp![("id_" + i)])
x = rtemp![("id_" + i)]

I tried something like this and it didnt work.
 
In VBA, same a above just replace "Me" with rtemp - ie without the bang (!), and if brackets, theyl be around the field name ro avoid problems with spaces, special characters,,,):

[tt]if not isnull(rtemp("id_" + i)) then[/tt]

[tt]if not isnull(rtemp("[id_" + i & "]")) then[/tt]

Another way to "loop" (all) the fields would be:

[tt]for lCounter = + to rtemp.fields.count-1
debug.print rtemp.fileds(0).value
next i[/tt]

Roy-Vidar
 
Gee - that was a lot of typos, the important ones:

[tt]for lcounter=0 to rtemp.fields.count-1
x=rtemp.fields(lcounter).value
next lcounter[/tt]

- btw - you are performing this in Access? Else find some forum related to the programming language you're using, see the search thingie on top.

Roy-Vidar
 
Actually, i just tried to implement the examples given above...

me.("id_" & lCounter) = ...
Doing it dboulos's way gives me a syntax error

debug.print Me("id_" & lCounter).value
and doing it royvidar's way gives me an object doesnt support this property or method..

I need to be able to do it this way and also with the record set as mentioned in my previous note.
Thanks for any help
 
Ok, got the recordset thing working like i need it using .fields

I still cant get the debug.print me("id_" & lcounter).value
working though to access the textbox on the report
 
Sorry, i think i do have it working now :).. must have just had a typo somewhere

thanks a bunch
Netooi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top