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

VBA Newbie: Referencing Control Names in a Form

Status
Not open for further replies.

shetzel

Programmer
Jul 28, 2003
4
US
OK.. I'm a complete n00b to VBA programming. I have come to need it for a project I'm now working on, and I'm having trouble making it work. (Keep in mind that I have no formal VBA training/experience, this is purely from referencing online tutorials, etc... SO, there are likely to be a LOT of mistakes.)

What I'm trying to do is make the macros behind the form scalable. I want the code to go through and find the control name for each listbox in the form. Here's what I have so far:

***
Private Sub Blah()
Dim ctrl As Controls
Dim objectName As Object
For Each ctrl In Form.AirplanePrograms.Controls
If TypeOf ctrl Is ListBox Then
objectName = ctrl
Call ColorsUpdate(objectName)
End If
Next ctrl
End Sub
***

I don't know how to reference the name of the control. Please help! (And sorry if this is really poor code! :))
 
Try this

Private Sub Blah()
Dim ctrl As Control
Dim strName As String
For Each ctrl In Me.Controls
If ctrl.ControlType = acListBox Then
strName = ctrl.Name
Call ColorsUpdate(strName)
End If
Next ctrl
End Sub
 
Thanks for your help! :-> I'm gettin an error however.

Alright, I'm getting a type-mismatch error because we are attempting to pass the control name as a string, when the function is expecting the control(object). Is there anyway to associate the string which the control-name which an object? Essentially, how do I pass the control-object instead of the string?

 
well, the way you did it, but I'd be surprised if you had needed to pass an Object instead of its string name.

PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top