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

Variable reference to controlname

Status
Not open for further replies.

ytakbob

Programmer
Jul 20, 2000
105
US
I am calling a subroutine passing the control's name:
Check_status(ActiveControl.Name)

Private Sub Check_status(controlname As String)

If Me.ostatus.Value = "Can" Then
controlname.backcolor = 255
End If


I get a compile error of invalid qualifier. on controlname
Now I know that controlname has the value of the control, in
this case "TestID" how do I get VB to interpret the above
assignment as TestID.backcolor = 255

I am in effect passing a variable of a variable name.



Bob Schmid
bob_schmid@hmis.org
330-746-1010 ext. 1347
 
You do not have a control named "controlname". You must look up the control in the controls collection.
Private Sub Check_status(controlname As String)
Dim MyControl As Control
If Me.ostatus.Value = "Can" Then
For Each MyControl In Controls
If Lcase($MyControl.Name) = Lcase$(controlname) Then
mycontrol.backcolor = 255
End If
Next
End if
End sub

Compare Code (Text)
Generate Sort in VB or VBScript
 


Your passing a string to the Check_Status routine. Instead you want to pass the actual control object. Try

Check_status(ActiveControl)

Private Sub Check_status(MyControl As Control)

If Me.ostatus.Value = "Can" Then
MyControl.Properties("BackColor") = 255
End If
 
Here is what worked guys:
Private Sub TestID_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.Name
Check_status (strControlName)

End Sub


Private Sub Check_status(controlname)

Dim MyControl As Control
If Me.ostatus.Value = "Can" Then
For Each MyControl In Controls
If MyControl.Properties("Name") = controlname Then
MyControl.Properties("BackColor") = 12632256
End If
Next
End If
End Sub



I received a syntax error when attempting to use the $ in
John's solution and I received a runtime error "91" object variable or with block variable not set.

I kind of combined the 2 solutions. If you have any comments on what I may have done wrong in trying to implement your individual solutions....let me know.
I'm running '97.

THanks alot guys....the moral of the story is...your solutions got me there ! Bob Schmid
bob_schmid@hmis.org
330-746-1010 ext. 1347
 
Hi Bob!

When you pass the controlname you can use it like this:

Me.Controls(controlname).BackColor = 12632256

hth Jeff Bridgham
bridgham@purdue.edu
 
Hi, I have a little problem with some controls in my form. I have a checkbox and a textbox. I want the textbox only appear when the checkbox is checked.

The name of the checkbox is Frontpage_Server_ext
The name of the textbox is frontpagewebmasterpass


Here is my code:

Private Sub Frontpage_Server_Ext_Click()
Dim ctlFrontpage_Server_ext As Control
Dim ctlfrontpagewebmasterpass As Control
Set ctlFrontpage_Server_ext = Frontpage_Server_Ext
Set ctlfrontpagewebmasterpass = frontpagewebmasterpass

If ctlFrontpage_Server_ext Is Selected Then
ctlfrontpagewebmasterpass.Visible = True
Else
ctlfrontpagewebmasterpass.Visible = False
End If
End Sub

thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top