Reuse varialbe in sucessive Case statements

A

aarrgghh765

Hi,

I have a problem with some code I'm trying to write. The idea was to
have a value passed in to a sub and use Select Case to perform an
action based on the Case.

i.e. I have;

dim sfrm as subform
dim ctl as control

set sfrm = me.controls("Containers Subform")

select case strPassedValue

Case "Value1"
for each ctl in sfrm.controls
'do stuff
next

Case "Value2"
for each ctl in sfrm.controls
'do stuff
next
end select


When the code hits the line below Case "Value2" I get the error message
"Duplicate declaration in current scope". It looks like the Case
statements aren't seperate code blocks as I thought they were, so
casting ctl more than once won't work.

While writing this out I suppose I could send the code into a Sub which
might solve the problem, I did try a "set ctl = null" statement just
prior to the Case "Value2" line but that didn't work.

Any other ideas on how I might get round this or a better approach?
 
T

TC

You say: "When the code hits the line below Case "Value2" I get the
error message "Duplicate declaration in current scope".

The line below Case "Value2" is: for each ctl in sfrm.controls. But
there is no way that that line, would get the message "Duplicate
declaration in current scope". So you are not showing us the actual
code that is getting the error, IMHO.

Also, to iterate the controls within the form AAA within the subform
control named BBB within the current form CCC, you need:

dim sfctl as control, ctl as control
set sfctl = me![BBB]
for each ctl in sfctl.form.controls

or:

dim sffrm as form, ctl as control
set sffrm = me![BBB].form
for each ctl in sffrm.controls

or just:

for each ctl in me![BBB].form.controls

Note that none of those methods require the name of the form in the
subform control! You use the name of the subform control itself,
qualified with the .Form property.

HTH,
TC (MVP Access)
http://tc2.atspace.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top