Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to simulate clicking a command button on an opened form using VBA? 1

Status
Not open for further replies.

surotkin

Programmer
Dec 10, 2003
103
CA
Hello everyone,
I use VBA code to open a bound form (RecordSource property is not empty).
Then I want to run code attached to a command button on that form.
Does anybody know if is it possible?

Thanks.
surotkin
 
I don't know exactly what you mean by running a command button. However, vba offers two events for the form while it is opening, Form_Open and Form_Load. Form_Open fires before the controls and data are loaded, and Form_Load fires after. If you add the command button code to either of these the function will fire.

private sub Form_Load()
cbDoSomething_click
end sub

Of course you would have to change the call to your command button name

Stix 42
Long Live Rock and Roll
Pop is for drinking
 
Hi stix4t2,
thanks for the reply.

What you suggest is not exactly what I meant, sorry if I was not clear.

Assuming the form was opened already and it doesn't have a focus at a moment when I want to simulate a click event on the form's button.

Any ideas?

Thanks.
surotkin

 
Couldn't you just call the button's OnClick Sub?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi EBGreen,
thanks for your reply.

No, you can not just call the button's OnClick Sub.

Is it what you mean:
Code:
Public Function Test()
 DoCmd.OpenForm FormName:="frmMyForm"
 Call cbDoSomething_Click
End function

If you do that the compiler will not compile it.

Any ideas?
surotkin

 
Make the Click event procedure Public and then you may call it just like any method of the form:
Forms!frmMyForm.cbDoSomething_Click

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,
thanks a lot.
It works!!!
I defined Click event as PRIVATE. It works after I changed it to PUBLIC!!!

surotkin
 
Hey Surotkin,

What I meant was to put the form_load event on the frmMyForm module. Then the call does not have to be public. Also if the form is opened from anywhere else in your code you don't have to make the second call. It also is a better use toward encapsulation.



Stix 42
Long Live Rock and Roll
Pop is for drinking
 
Hi stix4t2,
thanks for your response.
I see, however I don't need the code run when I open the form. I need it to be run at another moment.

Thanks.
surotkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top