VBA object properties appear in lower case
Asked Answered
R

2

6

Sometimes when developing you find that typical property names show up in lower case.

TempVars.Item("abc")

Might appear like this.

TempVars.item("abc")

Or

tbxMyTextbox.Value

Shows up as

tbxMyTextbox.value

The question is why does this happen and how do you fix it?

Rounds answered 28/7, 2016 at 15:2 Comment(0)
R
20

I've asked myself this question several times and seen others ask it in SO and elsewhere. In most cases the question comes up when searching for answers on other coding errors. Sometimes the developer (me too) wonders if there's something wrong that they're missing that causes this lower case issue.

A while back I ran across an answer (I'll add the original reference when I run across it again) that made the most sense and actually allowed me to correct this nagging issue.

Apparently it happens when you use a variable in lower case that has a name that's the same as a property.

So

 Dim value as string

will result in

 myObject.Value

appearing as

 myObject.value

Solution?

Because VBE apparently considers this variable across the entire IDE -- apparently without regard to scope; the way to revert to the proper case is to temporarily create the variable in upper case and avoid naming variables with the same name as existing properties in your application.

So the solution for the .value / .Value issue above would be to temporarily include the line

Dim Value as string

in a module within your app. Run the code that includes this line.

Afterwards, remove this temporary line of code.

Then as hoped, .value properties will again appear as .Value and not .value.

Of course, we should avoid those reserved words in the first place, but if at some point that wasn't done the above should fix the issue.

Here are some reserved word links:

Thanks to @ScottCraner for the nudge about avoiding reserved words.

Rounds answered 28/7, 2016 at 15:2 Comment(7)
The real fix would be to find the variable and change the name to one that is not a reserved name in vba. Therefore avoiding any confusion as to which Value one is referring.Xerophyte
@ScottCraner that's definitely true, however sometimes folks miss that point. I'll add that into the solution above along w/ a link to the reserved words.Rounds
In some instances you can simply type in the line with the desired case e.g. Dim Value as String and the IDE will respond by setting the case of the property properly - even without running the code.Rounds
Just removing the variable isn't enough. First you need to restore the correct casing to the variable, then delete it. Otherwise, even after removing it, the property will be forever wrongly cased! To fix, add the variable back with the right casing, then remove it.Sphenogram
As a use case as to where this is a massive problem. I'm having to deserialise some json into an object in VBA (I know right...) this json data includes a data value. I can't change this property because it's a third parties json. So I have to use a property data which is getting changed by the IDE into Data until I add the above (lets face it it's a) hack.. Can I go back to using C# now please?!Pessimist
2021 and very useful fix to a frustrating thingPersson
Just in case this might help someone else, I found in my case the problem was due to a module and a procedure with a reserved name.Frederico
B
-1

Try something like this:

CallByName(myObject, "value", VbGet)

Byproduct answered 28/4, 2021 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.