Dynamic Coding Loop - Never thought I'd miss pointers...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 20 labels that I update with DLookup and DCount functions. I have
used a naming scheme to where I start with "stat1" and end on "stat20". I
have a line of code for each box to update the field:

Me!stat1.Caption = DLookup("[status]", "tbl_tourstatus", "[index] = 1")
Me!stat2.Caption = DLookup("[status]", "tbl_tourstatus", "[index] = 2")
....

and so on. You can probably see where this is going. Techincally what I've
got works, but it'd be much better to have it in a loop:

' i = 0
' Do Until lbl > 10
' Me!stat & i & .Caption = DLookup("[status]", "tbl_tourstatus",
"[index] = i")
' i = i + 1
' Loop

except that I can't use the '&' operator so liberally, because the
compiler/interpreter/whatever doesn't exactly support dymanic naming like
that. In fact... well hell, that'd be a very very useful function to
implement, don't you think? :-) Naming variables with variables is
definitely something that should be supported in all programming languages.

Any coding gurus out there have a solution?

Thanks!

Nick
 
Excellent! That did the trick - thanks David!!

Nick

David C. Holley said:
Try

strControlName = "stat" & i
Me.Controls(strControlName).Caption
I have 20 labels that I update with DLookup and DCount functions. I have
used a naming scheme to where I start with "stat1" and end on "stat20". I
have a line of code for each box to update the field:

Me!stat1.Caption = DLookup("[status]", "tbl_tourstatus", "[index] = 1")
Me!stat2.Caption = DLookup("[status]", "tbl_tourstatus", "[index] = 2")
...

and so on. You can probably see where this is going. Techincally what I've
got works, but it'd be much better to have it in a loop:

' i = 0
' Do Until lbl > 10
' Me!stat & i & .Caption = DLookup("[status]", "tbl_tourstatus",
"[index] = i")
' i = i + 1
' Loop

except that I can't use the '&' operator so liberally, because the
compiler/interpreter/whatever doesn't exactly support dymanic naming like
that. In fact... well hell, that'd be a very very useful function to
implement, don't you think? :-) Naming variables with variables is
definitely something that should be supported in all programming languages.

Any coding gurus out there have a solution?

Thanks!

Nick
 
Back
Top