How to Write Code?

H

hdfixitup

I have a command button that runs a series of queries and moves to a control
on the form. I want to run the Event Procedure "On Click" when the control
gets focus. How do I write the code using the DoCmd. >>>>> to trigger this
event? Any help would be appreciated.
 
S

Scott McDaniel

I have a command button that runs a series of queries and moves to a control
on the form. I want to run the Event Procedure "On Click" when the control
gets focus. How do I write the code using the DoCmd. >>>>> to trigger this
event? Any help would be appreciated.

You can run a query like this:

Currentdb.Execute "NameOfYourQuery"
or
DoCmd.OPenQuery "NameOfYourQuery"

to move to a control, use SetFocus: Me.YourControl.SetFocus

If you need to call the code in multiple places, you would be better off moving that code to a function or sub call,
then calling that function/sub as needed. For eg:

Sub RunQueries()

Currentdb.Execute "Query1"
Currentdb.Execute "Query2"
Currentdb.Exectue "Query3"

End Sub

Now call this as needed

Sub MyButton_Click()
RunQueries
Me.SomeControl.SetFocus
End Sub

Sub MyTextbox_GotFocus()
RunQueries
End Sub

Althoug I'd advise that you think long and hard about using GotFocus to run queries ... anytime you're manipulating
data, you're better off letting the user start the process. This isn't a hard and fast rule, mind you, just be careful
when doing something like this.

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
H

hdfixitup

MY BAD! I did a poor job of explaining what I want to do. I currently have
code running the queries and moving the control to a specific field.
Currently, when I get to my control, I click the mouse and another set of
code is run (like stopping the code and waiting for user input). I want to
automate this process. I want to get to my field AND then have the "On
Click" event begin to execute the remaining code. I don't know how to write
DoCmd. Run Ommand "On Click" to complete the task at hand. I hope I am a
little clearer this time.
 
M

missinglinq via AccessMonster.com

You just invoke the Click sub for your command button:

Private Sub YourCommandButton_GotFocus()
YourCommandButton_Click
End Sub

But you really need to pay attention to Scott's warning! Any code you have in


Private Sub YourCommandButton_GotFocus()

will not only be carried out when your queries have run and your code directs
the foucs to your command button, it'll be carried out anytime the user tabs
onto the command button; do you really want that?
 

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