Making a control invisible

See my code below where I build up the OptionIDStr string.

The code crashes in the "IF" statement.
E.g. Me.OptionIDStr.Visible is not valid even tho' OptionIDStr contains "Option6" which is a control.

I understand the principle of what is wrong but cannot correct it.

Any thoughts out there??
For OptionID = 1 To 20
OptionIDStr = "Option" & OptionID

If Len(OptionIDStr & "") < 1 Then Me.OptionIDStr.Visible = 
False Else Me.OptionStr.Visible = True

Next OptionID

Open in new window

Patrick O'DeaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Try this syntax:

If Len(OptionIDStr & "") < 1 Then Me(OptionIDStr).Visible =

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I don't understand your logic:
OptionIDStr = "Option" & OptionID
so Len(OptionIDStr & "") will be always more then 6.
What are you trying to check?
Patrick O'DeaAuthor Commented:
Thank you both,

...You are (obviously) totally correct als315 - my logic was a little suspect!

However, I managed to get it right.

By the way, experts, what is the precise logic of;

When I put the Me followed by brackets ... what am I actually saying?
I think I know but would appreciate a comment from the experts.
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
"Me" refers to the active object - in this case, your Form where this code is running. The default property of that object is the Controls collection, so referring to


Is the same as writing


Note that in your code, OptionIDStr is a VARIABLE, which means you do NOT enclose it in double quotes. If you are using ONLY the name of that control, then you would need to enclose it in double quotes.

So for a Variable, like you're using:


If you instead use the actual Name of the Control:


And not to be nit-picky, but you're not using "brackets", you're using parentheses. A Bracket is [ or ] (i.e. a "square bracket"). They mean entirely different things to VBA, so be careful with your terminology.
Patrick O'DeaAuthor Commented:
Thanks all,

I'm learning!

Esp thanks to LSM for your comments
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Sorry, it was 1 am and I had to hit the sack. I did wait a bit for an initial response, but I guess we just missed each other.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.