Access 2003 .net interop send command button click

D

Damian

I'm trying to use the .Net interop assembles as a test-bed for an Access 2003
application. This appears to be the best tool since I can traverse the
objects on a form, etc.

BUT - how the heck do I click a command button?? Here's my code so far:

private void ClickOneSourceButton(string strFormName, string strButtonName)
{
//find the form
Microsoft.Office.Interop.Access.Form myAccessForm = null;
foreach (Microsoft.Office.Interop.Access.Form tempForm in oAccess.Forms)
{
if (tempForm.Name == strFormName)
{
myAccessForm = tempForm;
break;
}
}
if (myAccessForm != null)
{
//find the control
Microsoft.Office.Interop.Access.CommandButton myButton = null;
foreach (Microsoft.Office.Interop.Access.Control tempControl in
myAccessForm.Controls)
{
try
{
Microsoft.Office.Interop.Access.CommandButton tempButton =
(Microsoft.Office.Interop.Access.CommandButton)tempControl;
if (tempButton.Name == strButtonName)
{
//we've got our freakin button
myButton = tempButton;
}
}
catch {}
if (myButton != null) break;
}
//click the freakin button
if (myButton != null)
{

..
..
..

My best attempt at clicking the button is:

oAccess.Run("OneSource" + "." + myAccessForm.Name + "." + myButton.OnClick

EVEN if I test this by putting in a literal string with the name of the
button click event it doesn't work...

Any ideas?

Failing that am I using the wrong tool as testing tool??

Thanks!!

Damian
 
T

Tom van Stiphout

On Wed, 5 Mar 2008 18:27:00 -0800, Damian

You can click a button by using the SendMessage or PostMessage Windows
API, with the WM_CLICK argument.

-Tom.
 
D

Damian

Tom - this is an interesting idea. I played around with it for a couple of
hours but I still don't have a full solution. I THINK the problem is that
MS-Access doesn't give its buttons Window-Handles / at least I cannot find
one. I am thinking about sending a click message to the button's parent form
and use the button's location and size to have the click be on the actual
button... Not sure if this will work or not..

Damian
 
T

Tom van Stiphout

On Thu, 6 Mar 2008 10:04:01 -0800, Damian

Rats, I forgot about that. Yes, you are correct: controls only have a
window handle when they have focus.
Idea: set focus, then post a click event...

If I had to drive Access like that, I would use the Automation
interface.
Another possibility is to write an Add-In, which by definition has
access to the application itself.

-Tom.
 
D

Damian

Thanks Tom - I wasn't aware of that but was suspicious...

I got a button click system working by using two SendMessages - one keyup
and one keydown with a VK_SPACE key.. It works!

BUT as I've tried to expand beyond this (using the same code for different
forms and using similiar code to drive controls like textboxes and
comboboxes) it's been pretty flaky. It seems that as windows (in Access)
lose focus I have problems - does Access re-gen window handles for all
windows/controls as they regain focus??

I'm going to restructure my code such that it re-gets the window handle
every time I try to drive Access through the Windows API...

BTW writing an add-in isn't a bad idea but this product is at end-of-life
and I'm not sure it's worth it. It seems there isn't a 'generic' Access API
that will give me the kind of control I need / I had hoped microsoft's .NET
PIA would have done the trick but it's weak (at least for this purpose).

I'll keep you posted. Let me know if you have any other thoughts.

Thanks again.

Damian
 

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