Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract string from a variable name

Status
Not open for further replies.

hammy

Programmer
Sep 6, 2001
73
GB
Hi,

To speed up some very repetitive coding I want to extract a string value from a variable name,
e.g. extract the word "Area" from a field defined
Dim mstrArea as String

Is this possible?

Hammy.
 
Look into the InStr() function and other similar string manipulation functions. Thanks and Good Luck!

zemp
 
Exactly what is it you want? Do you want the names of your variables to be parsed by VB? This won't work unless you write some sort of an add-in that can parse your project files at design time....
Greetings,
Rick
 
Why not use Constants with the names yourneed rather than going top all the trouble of parsing your variable names??
 
I'm not sure if this is what you're looking for, but the code below demonstrates how to use the same function for controls that have different names.

To see how it works, create a new project, place two combo box controls on the form, double-click on the form and copy the following code there:

Private Sub Form_Load()
Combo1.AddItem "1"
Combo1.AddItem "2"
Combo1.AddItem "3"

Combo2.AddItem "A"
Combo2.AddItem "B"
Combo2.AddItem "C"

Call SetComboBox(Combo1, "2")
Call SetComboBox(Combo2, "C")
End Sub

'Set a combo box to a particular item
Public Sub SetComboBox(ByVal cboBox As ComboBox, ByVal strValue As String)
Dim lngCtr As Long
For lngCtr = 0 To cboBox.ListCount
If cboBox.List(lngCtr) = strValue Then
cboBox.ListIndex = lngCtr
Exit For
End If
Next
End Sub
 
This code is much more efficient:

'Set a combo box to a particular item
Public Sub SetComboBox(ByVal cboBox As ComboBox, ByVal strValue As String)
cbobox.Text = strValue
End Sub
 
No, you can not programmatically extract a portion of the a variable declaration in your run-time code. That is called "self-modifying code" and that approach went out in the 1960's.

What you can do is write an Add-In to IDE that treats your source code as data. I have written a couple of those, but it well beyond the scope of this forum to get into it.

I believe that Dan Appleman has some "how-to" information. His URL is They can steer you to appropriate book(s).
 
>approach went out in the 1960's.

You jest.

And why do you think that IDE Add-Ins are beyond the scope of this forum? Or do you just mean your specific add-in code?
 

[shocked] WeLL! I was just going to say... [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
strongm (MIS)

>And why do you think that IDE Add-Ins are beyond the scope of this forum? Or do you just mean your specific add-in code?

Why do feel the need to be insulting in your reply?
 
Er...it wasn't. Or at least wasn't mean to be. I genuinely wanted to know if you thought add-ins in general were beyond the scope of this VB5&6 forum (and if so, why), or whether you meant the particular add-ins that you had written and referred to.
 
I misunderstood your intent. My apologies.

My only excuse ist that, absent body language and intonation, intent is so easily construed.

I do believe that a tutorial on the writing of Add-Ins is beyond the scope of this forum. I have yet to see the topic covered in under 40 pages. The reason I suggested Dan Appleman is that, when I got in a bind writing a similar add-in to the one required for hammy's situation, I called Desaware, Dan's company, and got help from one of his programmers.
 
If you're only dealing with strings, you could do this by adding them to a collection object, using the keyname in place of the variable name. This would let you achieve what you're after, but only for strings.

Failing that, if you need to move beyond strings, I would declare a new object that holds the value in a variant property, and then use collections with that...


Just an idea...

mmilan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top