Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2006 Dec : Click a button on an Access form

www.cryer.info
Managed Newsgroup Archive

Click a button on an Access form

Subject:Click a button on an Access form
Posted by:"Colin" (col..@opms.com.au)
Date:Fri, 22 Dec 2006 10:19:27

Hi All

I want to click a button on an Access form. The following code works fine if
I create an application with a button, but will not do the same for an
Access wuindow.
What am I missing?

Function EnumWindowsFunc(HWND : Thandle; Param : Integer): BOOL; stdcall;
var
Caption : array[0..255] of char;
Button_Caption:string;
s:PAnsiChar;
begin
if GetWindowText (HWND, Caption, SizeOf(Caption)-1) <> 0 then
begin
  form1.ListBox1.items.Add(Caption) ;
  if ansicontainstext(form1.listbox1.items[form1.listbox1.count-1],'Test
Form') then begin //vary the name of the window to find here
   setforegroundwindow(HWnd); //set it as the foreground window
   Button_Caption:='Run Excel'; //the caption of the button I want to click
   ClickTheButton(HWND,Button_Caption);
   result := False;
   exit;
end;
end;
result :=True;
end;

Then I call my click button function which enumerates the child windows of
this window. :

function ClickTheButton(ParentWindow: Hwnd; ButtonCaption: string): Boolean;
var
  SL: TStringList;
  H:  hWnd;
begin
  SL := TStringList.Create;
  try
    SL.AddObject(ButtonCaption, nil); // First item in list is text to find
    EnumChildWindows(ParentWindow, @EnumChildProc, Longint(SL));
    H := 0;
    case SL.Count of
      1: ShowMessage('Window text not found.');
      2: H := hWnd(SL.Objects[1]);
      else
        ShowMessage('Ambiguous text detected.');
    end;
  finally
    SL.Free;
  end;
  Result := H <> 0;
  if Result then PostMessage(H, BM_CLICK, 0, 0);
end;

Which calls:
function EnumChildProc(Wnd: hWnd; SL: TStrings): BOOL; stdcall;
var
  caption: array[0..MAX_PATH] of Char; //Buffer for window caption
begin
  Result := Wnd <> 0;
  if Result then begin
    if GetWindowText(Wnd, caption, SizeOf(caption)-1)<>0 then begin// put
window text in buffer
     form1.listbox1.items.add(caption);
     if (SL.IndexOfObject(TObject(Wnd)) < 0) then // Test for duplicate
handles
      if caption=sl.Strings[0] then begin
        SL.AddObject(ansiString(caption), TObject(Wnd)); // Add item to list
        result:=False;
      end;
    end;
  end;
end;

regards

Colin

Replies:

www.cryer.info
Managed Newsgroup Archive