VBA: Drawing in a form || Mouseevent in report

I'm developing an Access2000 application and I need either to DRAW RECTANGLES IN A FORM (by VBA) or to CATCH MOUSEEVENTS (specificly the mouse coordinates) IN A REPORT (by VBA)

I managed to draw rectangles in a report and I managed to get the mouse coordinated in a form, but I need both, drawing rectangles and getting mouse coordinated - either in a form or in a report.

By the way: How can the X and Y coordinates of MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) be converted to Pixel?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There is a very good reason reports do not have mouse events - you won't be able to do anything with them once you got them.  Reports are static displays of data.  The only time you can can change than or add to them is when the report is opening and formatting - the on open and format events.  Once it is viewed on the screen (and you can move your mouse over it), it is fixed.

To draw rectangles:
You can use the create control method:
Dim ctlRect As Control
    Set ctlRect = CreateControl("YourFormNameHere", acRectangle [, section[, parent[, columnname[, left[, top[, width[, height]]]]]]])
this has to be done in design mode so reading between the lines of your question, I guess this won't help you.

Depending upon what you are trying to accomplish (always a good thing to include in your question), you might be able to have a rectangle on your form and adjust it with the mouse.

How can the X and Y coordinates of MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) be converted to Pixel?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

As Nelson said, you can't get the mouse move events on a report. What's more you can't draw or create rectangles on forms. Nelson's suggestion of CreateControl() works only in design mode, so that it's probably useless in your case.

When I need to "create" a rectangle or a line on a form dynamically, I create it at design time and set its "Visible" property to false. I then make it visible through code when needed.

As for pixels and twips, I confess that I usually get lazy and simply use 15 twips = 1 pixel, this corresponding to the default 96 pixel/inch resolution. Formally, you would have to ask Windows what the current metrics are, using Nelson's link, or this one which seems a little less complex:


Also note that all mouse events coordinates are relative to the object, (0,0) being the top left corner of the object. You can use a control's .Top and .Left properties to convert them to coordinates relative to the form. If you need absolute screen coordinates, it gets complex again, as you need  more API calls to get the form's position on the screen.

Good luck
Hmm, a search on MS shows that my link above is merely a copy of an article of the knowlege base. Best to go to the source, I believe:

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

TheFunkSoulBrotherAuthor Commented:
Thanks very much,
I think it will work fine whith the CreateControl method, if it's possible to assign eventhandlers to a so created control.

Specificly I'd need to assign a Click() eventhandler to the ctRect
Well. The CreateControl method is available only in design view. So you can adjust all properties, including the strings for the event handlers. Use "=SomeFunction()" or "[Event procedure]" to call a VB handler. The handlers can of course be written through code as well, which is exactly what some of the control wizards do.

But again, there is no way to create any control on a form in form view. If you need a rectangle to pop-up as a response to some user interaction, that rectangle must exist beforehand.

TheFunkSoulBrotherAuthor Commented:
In my case it doesn't matter that CreateControl is only available in design view because I always redraw the whole form - so first, I open the form in design view, create rectangles an add eventhandlers, then I save the form, close it and open it in normal view.

Thanks a lot!
Glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.