Loop thru form controls that match records in listbox

H

heidi

Hello All:

Have not dealt with looping through controls or recordsets before and
need help getting started.

Here is what I have:

1. Main form - frmMap
2. List Box on Main form - lstMapBlocksAdded
3. Sub form that's source object changes based on textbox value on
Main form.
4. On Sub form linked unbound object frame showing pdf map. On top
of map picture are invisible buttons over different sections of my
map. Creating a clickable map. OnDblClick adds the corresponding
record to my a list box located on Main form that matches caption on
each button. After record is added to list box lbl with caption "X"
appears in red above map section and makes invisible button enabled =
false so it can not be selected again. This all works great when I am
first creating the record.

What I want to do is when someone needs to go back into that record
and pulls up the map, I want to loop through my list box and loop
through all the controls to remark the buttons as enabled = false or
lables as visible if matching my listbox records.

How do I loop through my listbox and controls that match.

My records in my list box have a column called RecMapTag. The value
in this column matches the captions on the buttons and label names.

Thanks
 
K

Klatuu

Here is the basic loop needed to go through all the controls on a form:

Dim ctl As Control

For Each ctl In Me.Controls
If ctl.ControlType = acCommandButton ThenEnd If
Next

Set ctl = Nothing
 
H

heidii

But how do I after the intial ctl loop check if that controls caption
matches any of the records listed in the listbox? That is where I am
stuck.
 
H

heidii

Well I have got something that works but have another issue I need
help with?

I keep getting Error 2164 You can't disable a control while it has the
focus. But I have changed the focus. Am I missing something??

Here is my code:

Dim ctl As Control
Dim ctlCaption As String
Dim ctlCaptionName As String
Dim strMapTag As String
Dim ctlLabel As String
Dim ctlLabelName As String
Dim i As Long

' check all command buttons
For Each ctl In [Forms]![frmMapEdit]![MapSubForm].Controls
If ctl.ControlType = acCommandButton Then
ctlCaption = ctl.Caption

'loop through list box
'THIS WORKS PERFECT. DON'T CHANGE
For i = 0 To Me.lstMapBlocksAdded.ListCount - 1
If Me.lstMapBlocksAdded.ItemData(i) = ctlCaption Then
strMapTag = Me.lstMapBlocksAdded.ItemData(i)
MsgBox strMapTag
ctlCaptionName = ctl.Name
MsgBox ctlCaptionName
Me.Command223.SetFocus
ctl.Enabled = False
ctl.Visible = True
Me.Command223.SetFocus
End If
Next

End If
Next

Set ctl = Nothing

'check all labels
For Each ctl In [Forms]![frmMapEdit]![MapSubForm].Controls
If ctl.ControlType = acLabel Then
ctlLabel = ctl.Tag

'loop through list box
'THIS WORKS PERFECT. DON'T CHANGE
For i = 0 To Me.lstMapBlocksAdded.ListCount - 1
If Me.lstMapBlocksAdded.ItemData(i) = ctlLabel Then
strMapTag = Me.lstMapBlocksAdded.ItemData(i)
MsgBox strMapTag
ctlLabelName = ctl.Tag
MsgBox ctlLabelName
Me.Command223.SetFocus
ctl.Visible = True
Me.Command223.SetFocus
End If
Next

End If
Next

Set ctl = Nothing
 
H

heidii

Ok here is a wrench. I only get this can't change focus error if I
have more than one record in the system with Block 1 listed in the
list box. aka command16 button.

I have checked all the command names, captions and tags that are
triggering my code. And I have other records that have duplicate
block selections in each record, but they don't produce this error.

At a loss??
 
H

heidii

Well I am not sure what was the trigger on that particular record.
But I copied the record and pasted back into table. Deleted original
record. Seems to have fixed the issue.
 
H

heidii

Ok I take that back. I forgot to remove the On Error Resume Next from
debugging.

The problem is still there everytime I had that one record.

Please someone step in and help.

Heidi
 
H

heidii

Solution:

That control was the first on the Tab Order List of my subform.
Created a dumby control and made it the first tab order. This
resolved the issue.
 
F

frances eyler

heidi said:
Hello All:

Have not dealt with looping through controls or recordsets before and
need help getting started.

Here is what I have:

1. Main form - frmMap
2. List Box on Main form - lstMapBlocksAdded
3. Sub form that's source object changes based on textbox value on
Main form.
4. On Sub form linked unbound object frame showing pdf map. On top
of map picture are invisible buttons over different sections of my
map. Creating a clickable map. OnDblClick adds the corresponding
record to my a list box located on Main form that matches caption on
each button. After record is added to list box lbl with caption "X"
appears in red above map section and makes invisible button enabled =
false so it can not be selected again. This all works great when I am
first creating the record.

What I want to do is when someone needs to go back into that record
and pulls up the map, I want to loop through my list box and loop
through all the controls to remark the buttons as enabled = false or
lables as visible if matching my listbox records.

How do I loop through my listbox and controls that match.

My records in my list box have a column called RecMapTag. The value
in this column matches the captions on the buttons and label names.

Thanks
 

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