Access 2003, Determining which control has been clicked

J

Jerry Pine Pollen

I've been searching discussion groups (here and elsewhere) for days but can't
find anyone who has addressed my question.

In Access 2003, I've got a form with a mosaic of rectangles representing
various locations in our organization. I change the background color of the
rectangle or text within a rectangle depending on the specific data for that
location. When a user clicks on one of the location boxes, I want to bring
up a data form specific to that location. However, the only method I can
figure out so far is to have an onclick event for every single rectangle.
I've got that working for a representative rectangle and it works great
except that I've got 300 rectangles and counting. That makes for a program
construction and maintenance nightmare.

Conceptually, what I would like to do is to have the form determine which
rectangle was clicked and then pass the parameters to a common data
management routine to decide what form and data to load. I can poll through
all the rectangles but don't see a way to tell if one has been
selected/clicked, since rectangles don't have an "onGotFocus" property.

Any advice?
 
S

Stuart McCall

Jerry Pine Pollen said:
I've been searching discussion groups (here and elsewhere) for days but
can't
find anyone who has addressed my question.

In Access 2003, I've got a form with a mosaic of rectangles representing
various locations in our organization. I change the background color of
the
rectangle or text within a rectangle depending on the specific data for
that
location. When a user clicks on one of the location boxes, I want to
bring
up a data form specific to that location. However, the only method I can
figure out so far is to have an onclick event for every single rectangle.
I've got that working for a representative rectangle and it works great
except that I've got 300 rectangles and counting. That makes for a
program
construction and maintenance nightmare.

Conceptually, what I would like to do is to have the form determine which
rectangle was clicked and then pass the parameters to a common data
management routine to decide what form and data to load. I can poll
through
all the rectangles but don't see a way to tell if one has been
selected/clicked, since rectangles don't have an "onGotFocus" property.

Any advice?

Try this on for size. Create a public function in a standard module which
takes a form object as a parameter, eg:

Public Function FunctionName(frm As Access.Form)

Inside the function, use frm.ActiveControl to determine which rectangle was
clicked. Also do the color thing in here.

On your form in design view, select all the rectangles, open the property
sheet and go to the Events tab. Put this into the OnClick property:

=FunctionName([Form])

That sets the property for all selected controls.
 
R

Rick Brandt

Stuart said:
Try this on for size. Create a public function in a standard module
which takes a form object as a parameter, eg:

Public Function FunctionName(frm As Access.Form)

Inside the function, use frm.ActiveControl to determine which
rectangle was clicked. Also do the color thing in here.

On your form in design view, select all the rectangles, open the
property sheet and go to the Events tab. Put this into the OnClick
property:
=FunctionName([Form])

That sets the property for all selected controls.

Problem though is that since a rectangle cannot have focus it will never be
the active control. One could use TextBoxes instead though and then it
would work.
 
M

Marshall Barton

Jerry Pine Pollen <Jerry Pine
I've been searching discussion groups (here and elsewhere) for days but can't
find anyone who has addressed my question.

In Access 2003, I've got a form with a mosaic of rectangles representing
various locations in our organization. I change the background color of the
rectangle or text within a rectangle depending on the specific data for that
location. When a user clicks on one of the location boxes, I want to bring
up a data form specific to that location. However, the only method I can
figure out so far is to have an onclick event for every single rectangle.
I've got that working for a representative rectangle and it works great
except that I've got 300 rectangles and counting. That makes for a program
construction and maintenance nightmare.

Conceptually, what I would like to do is to have the form determine which
rectangle was clicked and then pass the parameters to a common data
management routine to decide what form and data to load. I can poll through
all the rectangles but don't see a way to tell if one has been
selected/clicked, since rectangles don't have an "onGotFocus" property.


If the rectangles are laid out in a regular pattern, then
you can place a large, transparent command button on top of
all of them. Then you can use the button's MouseUp event's
X,Y coordinates to calculate which rectangle was under the
mouse. For example, if your rectangles make up a uniform 3
by 3 grid and the rectangles are named box11, box12, box13,
box21, etc, then the clicked rectangle is:
Me("box" & (X \ box11.Width +1) & (Y \ box11.Height))

This concept gets real messy real fast if the rectangles are
not laid out in a uniform grid. In a worst case scenario,
you would have to cover the whole form section with the
button and loop through all the rectangle controls (named
box1, box2, ..., box300) comparing X and Y to the control's
Left, Width, Top and Height properties:

For k = 1 To 300
With Me("box" & k)
If X >= .Left And X <= .Left+.Width And _
Y >= .Top And Y <= .Top+.Height _
Then
'do your thing
. . .
Exit For
End If
End With
Next k
 
S

Stuart McCall

Rick Brandt said:
Stuart said:
Try this on for size. Create a public function in a standard module
which takes a form object as a parameter, eg:

Public Function FunctionName(frm As Access.Form)

Inside the function, use frm.ActiveControl to determine which
rectangle was clicked. Also do the color thing in here.

On your form in design view, select all the rectangles, open the
property sheet and go to the Events tab. Put this into the OnClick
property:
=FunctionName([Form])

That sets the property for all selected controls.

Problem though is that since a rectangle cannot have focus it will never
be the active control. One could use TextBoxes instead though and then it
would work.

Of course. Yes they would have to be textboxes. I'd forgotten the focus
problem.

Thanks for jumping in.
 

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