Looping

  • Thread starter Thread starter Carlos1815
  • Start date Start date
C

Carlos1815

This may seem like an easy question considering all the difficult
questions I've been peppering everyone with, but it confounds me!

I have a rather large set of code that needs to be used for each topic
I have (18 of them!). Don't ask why I did it that way, it kind of
snowballed. Now, I can achieve the end result I'm looking for if I do
it the long way. Let me illustrate:

********************************************************************

If Me.RecordSource = "Topic1" Then
Lots of code...
Lots of code...
Lots of code...
Lots of code...
End if

If Me.RecordSource = "Topic2" Then
Lots of code...
Lots of code...
Lots of code...
Lots of code...
End if

********************************************************************

I'd rather not have to keep copying and pasting so much code 18 times,
and I thought I could use a variable; something like this:

Dim i as Integer
Do while i = 1 to 18
If Me.RecordSource = "Topic" & i Then
Lots of code...
Lots of code...
Lots of code...
Lots of code...
End if
Loop

I tried using that above code, though instead of "i = 1 to 18", I used
"i < 19" and that made Access very, very angry at me; so I set
everything back and didn't even try the other way. Can anyone give me
the correct way to code that? It would help greatly since I have a
lot of sections where I needed to repeat code multiple times and this
would streamline my VB immensely. Thanks in advance!

Carlos
 
The Loop idea is only going to work if you're processing the same code
for each RecordSource, in which case I guess I'm missing the point
of all this. In other words, if you're running the same code regardless
of the the RecordSource, then what's the point of referring to the
RecordSource in the first place?

On the other hand, if you're running different code for each RecordSource
then you can't use the Loop (at least not that I can see). It might be
slightly
easier to code using a Select Case statement;

Select Case Me.RecordSource
Case "Topic1"
some code
Case "Topic2"
some other code
etc. etc.
End Select

I guess I won't ask why you have 18 different Topic tables.
 
Carlos1815 said:
This may seem like an easy question considering all the difficult
questions I've been peppering everyone with, but it confounds me!

I have a rather large set of code that needs to be used for each topic
I have (18 of them!). Don't ask why I did it that way, it kind of
snowballed. Now, I can achieve the end result I'm looking for if I do
it the long way. Let me illustrate:

Without getting into "why," aren't you looking for:

Dim i as Integer
For i = 1 to 18
If Me.RecordSource = "Topic" & i Then
Lots of code...
Lots of code...
Lots of code...
Lots of code...
End if
Next i
 
Back
Top