By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

How to access a control through code?

P: n/a
Is there a way to access a control on a form directly by name? I've used
the code shown below but it requires looping through each control on the
form and seems a bit inefficient. I know me.cmdTest would work but I want
to be able to pass the "cmdTest" part as a string into the function hence
this doesn't work. Any help would be appriciated.

Thanks
Dustin

Dim ctr As Control
strControlName = "cmdTest"
For Each ctr In Form
If ctr.Name= strControlName Then
'* Do something here
End If
Next ctr
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You could use Me.Controls(strControlName)

HTH
- Turtle

"Viper" <Vi***@nospam.com> wrote in message
news:Uq*****************@news1.mts.net...
Is there a way to access a control on a form directly by name? I've used
the code shown below but it requires looping through each control on the
form and seems a bit inefficient. I know me.cmdTest would work but I want
to be able to pass the "cmdTest" part as a string into the function hence
this doesn't work. Any help would be appriciated.

Thanks
Dustin

Dim ctr As Control
strControlName = "cmdTest"
For Each ctr In Form
If ctr.Name= strControlName Then
'* Do something here
End If
Next ctr

Nov 12 '05 #2

P: n/a
On Tue, 10 Feb 2004 21:17:12 -0600, Viper wrote:
Is there a way to access a control on a form directly by name? I've used
the code shown below but it requires looping through each control on the
form and seems a bit inefficient. I know me.cmdTest would work but I want
to be able to pass the "cmdTest" part as a string into the function hence
this doesn't work. Any help would be appriciated.

Thanks
Dustin

Dim ctr As Control
strControlName = "cmdTest"
For Each ctr In Form
If ctr.Name= strControlName Then
'* Do something here
End If
Next ctr


All controls on a form are part of the form's Controls collection. As with
most collections, you can reference any item by it's index or (usually)
name. Use formobject.Controls(controlname)

--
Mike Storr
veraccess.com
Nov 12 '05 #3

P: n/a
that's exactly what I needed. Thanks Mike

Regards
Dustin

"Mike Storr" <st******@sympatico.ca> wrote in message
news:tq***************************@40tude.net...
On Tue, 10 Feb 2004 21:17:12 -0600, Viper wrote:
Is there a way to access a control on a form directly by name? I've used the code shown below but it requires looping through each control on the
form and seems a bit inefficient. I know me.cmdTest would work but I want to be able to pass the "cmdTest" part as a string into the function hence this doesn't work. Any help would be appriciated.

Thanks
Dustin

Dim ctr As Control
strControlName = "cmdTest"
For Each ctr In Form
If ctr.Name= strControlName Then
'* Do something here
End If
Next ctr


All controls on a form are part of the form's Controls collection. As with
most collections, you can reference any item by it's index or (usually)
name. Use formobject.Controls(controlname)

--
Mike Storr
veraccess.com

Nov 12 '05 #4

P: n/a
Jim
me.controls(controlname)

in your example

Dim strControl as String
strControl="cmdTest"

Me.Controls(strControl) refers to the control

"Viper" <Vi***@nospam.com> wrote in message
news:Uq*****************@news1.mts.net...
Is there a way to access a control on a form directly by name? I've used
the code shown below but it requires looping through each control on the
form and seems a bit inefficient. I know me.cmdTest would work but I want
to be able to pass the "cmdTest" part as a string into the function hence
this doesn't work. Any help would be appriciated.

Thanks
Dustin

Dim ctr As Control
strControlName = "cmdTest"
For Each ctr In Form
If ctr.Name= strControlName Then
'* Do something here
End If
Next ctr

Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.