Control Names and InStr

S

scott

On a continuous form that sorts columns when user clicks the column headers.
I also have some embedded gif icons next to each column header caption that
display ascending and descending order and named them with the convention of
icon_up_TheControlName and icon_down_TheControlName. I also have an embedded
gif icon called "cmdRemove" on the form.

If I run my sub in the "USAGE" section below, my sub FlipSortImages iterates
through the form and correctly changes the icon_up_firstName control visible
property to true and sets all of the other control's visible property to
false. The problem is that I can't seem to exclude the "cmdRemove" icon from
being affected.

In my code I have a simple message box that should fire when the sub reaches
the "cmdRemove" icon, but it won't recognize it. Can someone setup a simple
test form with a few controls using my naming convention and help me find a
conditional test that will exclude any image controls that have the
"cmdRemove" name or for that matter, any image control name that begins with
"cmd"?

I also tried using the InStr() function to no avail.



'USAGE *******************

Call FlipSortImages(Me.icon_up_firstName)



'EXAMPLE IMAGE CONTROL NAMES *******************

icon_up_firstName
icon_down_firstName
icon_up_lastName
icon_down_lastName
cmdRemove


'CODE ************************************

Sub FlipSortImages(ctlImageHot As Control)

Dim ctl As Control

For Each ctl In Me.Controls

If ctl.ControlType = acImage Then
If InStr(ctlImageHot.Name, "icon_up_") > 0 Or
InStr(ctlImageHot.Name, "icon_down_") > 0 Then
If ctl.Name <> ctlImageHot.Name Then
If ctl.Name = "cmdRemove" Then ' this is the part
MsgBox ctl.Name
Else
ctl.Visible = False
End If
Else
ctl.Visible = True
End If
End If
End If


Next ctl

End Sub
 
B

Bob Hairgrove

On a continuous form that sorts columns when user clicks the column headers.
I also have some embedded gif icons next to each column header caption that
display ascending and descending order and named them with the convention of
icon_up_TheControlName and icon_down_TheControlName. I also have an embedded
gif icon called "cmdRemove" on the form.

If I run my sub in the "USAGE" section below, my sub FlipSortImages iterates
through the form and correctly changes the icon_up_firstName control visible
property to true and sets all of the other control's visible property to
false. The problem is that I can't seem to exclude the "cmdRemove" icon from
being affected.

In my code I have a simple message box that should fire when the sub reaches
the "cmdRemove" icon, but it won't recognize it. Can someone setup a simple
test form with a few controls using my naming convention and help me find a
conditional test that will exclude any image controls that have the
"cmdRemove" name or for that matter, any image control name that begins with
"cmd"?

I also tried using the InStr() function to no avail.



'USAGE *******************

Call FlipSortImages(Me.icon_up_firstName)



'EXAMPLE IMAGE CONTROL NAMES *******************

icon_up_firstName
icon_down_firstName
icon_up_lastName
icon_down_lastName
cmdRemove


'CODE ************************************

Sub FlipSortImages(ctlImageHot As Control)

Dim ctl As Control

For Each ctl In Me.Controls

If ctl.ControlType = acImage Then
If InStr(ctlImageHot.Name, "icon_up_") > 0 Or
InStr(ctlImageHot.Name, "icon_down_") > 0 Then
If ctl.Name <> ctlImageHot.Name Then
If ctl.Name = "cmdRemove" Then ' this is the part
MsgBox ctl.Name
Else
ctl.Visible = False
End If
Else
ctl.Visible = True
End If
End If
End If


Next ctl

End Sub

As an additional debugging help, I would suggest either moving the MsgBox
statement up to immediately after the first "If" statement, or else using
Debug.Print to print all of the control names to the immediate window in VBA
view.

Surprisingly often in situations such as this, the control you are concentrating
on actually has a name like "Command23" or "Text24" instead of what you think it
has. The above procedure should help eliminate that possibility; at leastit has
helped me on numerous occasions! :)
 
S

scott

thanks, i fixed it.

On a continuous form that sorts columns when user clicks the column
headers.
I also have some embedded gif icons next to each column header caption that
display ascending and descending order and named them with the convention
of
icon_up_TheControlName and icon_down_TheControlName. I also have an
embedded
gif icon called "cmdRemove" on the form.

If I run my sub in the "USAGE" section below, my sub FlipSortImages
iterates
through the form and correctly changes the icon_up_firstName control
visible
property to true and sets all of the other control's visible property to
false. The problem is that I can't seem to exclude the "cmdRemove" icon
from
being affected.

In my code I have a simple message box that should fire when the sub
reaches
the "cmdRemove" icon, but it won't recognize it. Can someone setup a simple
test form with a few controls using my naming convention and help me find a
conditional test that will exclude any image controls that have the
"cmdRemove" name or for that matter, any image control name that begins
with
"cmd"?

I also tried using the InStr() function to no avail.



'USAGE *******************

Call FlipSortImages(Me.icon_up_firstName)



'EXAMPLE IMAGE CONTROL NAMES *******************

icon_up_firstName
icon_down_firstName
icon_up_lastName
icon_down_lastName
cmdRemove


'CODE ************************************

Sub FlipSortImages(ctlImageHot As Control)

Dim ctl As Control

For Each ctl In Me.Controls

If ctl.ControlType = acImage Then
If InStr(ctlImageHot.Name, "icon_up_") > 0 Or
InStr(ctlImageHot.Name, "icon_down_") > 0 Then
If ctl.Name <> ctlImageHot.Name Then
If ctl.Name = "cmdRemove" Then ' this is the part
MsgBox ctl.Name
Else
ctl.Visible = False
End If
Else
ctl.Visible = True
End If
End If
End If


Next ctl

End Sub

As an additional debugging help, I would suggest either moving the MsgBox
statement up to immediately after the first "If" statement, or else using
Debug.Print to print all of the control names to the immediate window in VBA
view.

Surprisingly often in situations such as this, the control you are
concentrating
on actually has a name like "Command23" or "Text24" instead of what you
think it
has. The above procedure should help eliminate that possibility; at least it
has
helped me on numerous occasions! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top