PDA

View Full Version : Trying to reference object of triggered event



Allaric
09-20-2011, 07:02 PM
Hello folks,

I am trying to change the color of a rectangle object on a form when that rectangle is clicked on. I have searched for a way to reference the object raising the CLICK event, and dug through 'Me' to see if there is an indicator of some kind on the object that was clicked without success. I had originally thought that Me.name would be the ticket, but it references the form and not the rectangle.

There seems to be no help from ActiveControls, my guess is because the rectangle is a shape and not considered a control.

I would think this would be something very simple, and it has driven me crazy to end up with nothing.

Any help would be greatly appreciated.

hansup
09-20-2011, 07:49 PM
Yes, "Me" is a reference to your current form object. You can refer to a control on the form with the control's name after "Me". So if your rectangle is named "Box0", either of these two statements should work:

MsgBox Me!Box0.Name
MsgBox Me.Box0.Name
Either should give you a message box which displays the name of the control ... Box0. The second statement, using the dot instead of bang (!) following Me, will allow you to use Intellisense to select from the form properties and methods.

If you want to change the rectangle's background color when it's clicked, open the form in design view, select the rectangle, then open the property sheet. (right-click it, then select properties) Select the "Event" tab, then click the 3 dots to the far right of "On Click". That should open a code module for your form with this code stub (assuming the control's name is Box0):

Private Sub Box0_Click()

End Sub
Add your custom code between those two lines. As an example, if you want the rectangle background color to cycle between red, green, and blue, you could use this code:

Private Sub Box0_Click()
Select Case Me.Box0.BackColor
Case vbRed
Me.Box0.BackColor = vbGreen
Case vbGreen
Me.Box0.BackColor = vbBlue
Case vbBlue
Me.Box0.BackColor = vbRed
Case Else
Me.Box0.BackColor = vbGreen
End Select
End Sub
You need to set the rectangle's Back Style to Normal (instead of Transparent) in order for the color to be displayed. Find that property on the "Format" tab of the property sheet.

Allaric
09-20-2011, 08:46 PM
Thanks for the response hansup, unfortunately I probably should have been more percise. I have a few hundred of these rectangles (think of a large grid where the squares go black if you click one) I need them to change color only when they are clicked, and I am trying to avoid hand-coding each event with the name of the object. That is why I am looking for a generic way to reference the object that triggered the click event.

I was considering creating a new class/event combo of rectangles to avoid coding all 400 at all, but to even try that I need to be able to solve the issue of knowing what object is raising an event without specifing the name in code. Heck, if i could even reference the name of the event that was being raised that would be enough, but that is an odd requirement and i dont know if that is possible.

Is there no way to tell what object raised an event during run-time without hard-coding it? The only other option would be to go the hand route and leave horribly ugly code that no one will ever want to update.

Thanks for the help

hansup
09-21-2011, 09:26 AM
Is there no way to tell what object raised an event during run-time without hard-coding it?

None that I'm aware of. If you find a way to do it, please share your solution.

hansup
09-22-2011, 11:12 AM
You might consider posting this question at a another forum. I see quite a bit of activity with Access questions on Stack Overflow.

http://stackoverflow.com/questions/tagged/ms-access