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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a string to a variable name

Status
Not open for further replies.

wrbodine

Programmer
Aug 24, 2000
302
US
Hi,
I have a function I'm creating where I want to be able to pass a label name (or command button name, etc.), append the string ".visible" to it, and toggle the value of this property. My problem is, right now in my comparison this property is a string, so I'm getting a type mismatch:

cmdVariable = "SomeCommandButtonName"

strVariable = cmdVariable & ".visible"

if strVariable = True then
... here I want to change it to false, etc.
End if

In the code above, strVariable is being treated as "cmdVariable.visible" instead of cmdVariable.visible

So how can I make a string to be treated as a normal name, without the quotation marks???

Tia,
Ray
 
Pass your control's name to this sub and it will toggle the control's visibility.
Code:
Public Sub ToggleVis(CtrlName As String)
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.Name = CtrlName Then
            Select Case ctl.Visible
            Case True
                ctl.Visible = False
            Case Else
                ctl.Visible = True
            End Select
        End If
    Next
End Sub
 
Get the reference to the command from the form's control collection using the conrol name (as a string) forthe index. Once you have control reference, you can call any method/attribute on the control using a string with the CallByName method. If you want an example, just ask and I'll post it . . .otherwise, this should be enough to get you started. - Jeff Marler B-)
 
Code:
Dim strName as string
strName = Ucase(CtrlName)
For Each ctl in Me.Controls
    If Ucase(ctl.Name) = strName Then
        On error resume next ' Visible absent on some controls
            ctl.Visible = Not (ctl.Visible = True)
        On Error goto 0    
     End If
Next
 
Thanks, these replys are helpful....

I ended up using an object, which I found from another post, and using that with MisterC's function

Sub ToggleVis(myObj as Object)
...
End Sub

Sub OtherSub()
Dim objArray(22) as Object

Set objArray(0) = cmbComboBox
etc...
End Sub

Just curious, does anyone know of a way to declare all 23 members of the above array in one statement? Or to do that for a string array?
 
Maybe
'* I prefer starting at 1 so an array of 0 can eist
Dim lngCount As Long
Dim aryCtl() As Object
Dim I As Long
lngCount = Me.Controls.Count
If lngCount > 0 Then
ReDim aryCtl(lngCount)
For I = 1 To lngCount
Set aryCtl(I) = Me.Controls(I-1)
Next
End If
 
OOPs. Hit submit instead of edit.

'* I prefer starting at 1 so a Ubound(array) = 0
'* means empty
Dim lngCount As Long
Dim aryCtl() As Object
Dim I As Long
lngCount = Me.Controls.Count
ReDim aryCtl(lngCount)
For I = 1 To lngCount
Set aryCtl(I) = Me.Controls(I-1)
Next
 
======================================================================
OK . . . you guys are making this more complicated than it really is. Yes, you can store all of the controls in an array, but you already have that data in the form's control collection. Here is code that will do EXACTLY what you want in 1 single line.

Assume that I have a command button name "Command1" on a form, and I want to access the Visible property on the command button to hide or show it. I want to do using only string names of the conrols and/or attributes. Here it is . . .

Code:
Call CallByName(Me.Controls.Item("Command1"), "Visible", VbLet, True)

And that is all you have to do . . . no arrays, no looping . . . Also, the CallByName funciton can be used to read values, call functions, all kinds of cool stuff.
Give it a try and if you want more info on how/why this works, let me know . . .I'll gladly explain it in depth. - Jeff Marler B-)
 
======================================================================
Or . . . to make the command button disappear . . .


Code:
Call CallByName(Me.Controls.Item("Command1"), "Visible", VbLet, False)
- Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top