Is there a way to add a macro action that would click on the control that has focus? Thanks.
Is there a way to add a macro action that would click on the control that has focus? Thanks.
What event would, then, trigger the Macro?
no; one can .SetFocus - but nothing more beyond that such as triggering the OnClick event of that control.... Microsoft is 'event driven' by its own fundamental self definition meaning that a human is there doing things... what you seek is typically designed via the use of Modules - whereby code is available to be called through out the application and is not limited to being inside a specific control.
Are you talking about clicking on a Command Button? If so, don't have a clue how to do it with a Macro; few serious developers use them, but using VBA code you don't even have to go to the Control to fire it, you just Call the OnClick Sub for the Control:
Call cmdCommandButtonName_Click
What, exactly, is your scenario? Are you opening a secondary Form, doing something, then closing it, and then wanting, on return to the primary Form, to fire the Command Button? If so, what is your code for doing this? What is the name of the Command Button?
Linq ;0)>
I am in form 1, I open a second form, add a description and amount, close this form, return to the first form, go to control calledCopy411, which runs code to transfer the description and amount to a row on the first form rom the second form. I don't want the user to forget to click on Copy411 thus completing the transfer. So I would like to make it happen for them. Thanks for your help.
As mentioned in post #4, you can use VBA from within the form's module to fire the On Click Event. If code is running within the form and you would like it to, seamlessly, fire the command buttons On Click event handler... Add this VBA to your existing code. The caveat is there needs to be VBA in the Copy_411 click event.
Call Copy411_Click
If you open the secondary form in Dialog mode, the code in the primary form stops until the secondary form is closed...so something like this should do it:
Code:DoCmd.OpenForm "SecondaryFormName", , , , , acDialog Call Copy411_Click
Just replace SecondaryFormName with the actual name of the secondary form.
Linq ;0)>
If this suits the occasion, it is a superb thought for a rarely used option. The way I am understanding it is the user comes back to the primary form and does something to fire other code that, then, needs the click event. Could be wrong, though.
I used the On Got Focus, as my macro has already moved the focus to Copy411, wrote Copy411_click ( and it worked spectacularly). Thanks everyone for our help.
Perhaps I am alone. But, I am having trouble understand the User's workflow. I am glad to hear you have had some success. Are you sure the On Focus is the correct event to use? What happens if the user sets focus on the control multiple times or before it is desired the code be executed?
Choosing the correct event is not the easiest thing to do and should be tested as if the User is trying to break things.
I don't really understand it, either, and it would be interesting to know. Also interesting to know if Copy411 is, in fact, a Command Button or something else.
Linq ;0)>
The first form has many such command buttons set up to transfer each row of data to the other side of the form. I have never had any problems with the way the user interfaces with the command buttons.
Copy411 is a command button, it calls the following code on click, and copies data from two fields, into 2 other fields. If the user clicked on it, it would do the transfer (I am using a DLookup field on the first form to look up the data on the second form). When I get back to the first form with the focus on Copy411, I call Copy411_click, and copy 412_click. I don't know if on focus is the best place, it seems to work at that point. The user could click it many times, it would still produce the desired result. They can always click on the command button before there is data in the fields being transferred, I think it would just transfer blank fields.
Function CopyFields(Source$, Destination$)
'Copy fields fromform Source$ to form Destination$.
'Only fields withmatching ControlSource names will be copied.
On Error ResumeNext 'Disable error trapping
For COUNTER = 1 ToForms(Source$).Count
'Try to get acontrol source for control Counter.
ControlField$ =Forms(Source$)(COUNTER).ControlSource
'Did we get acontrol source? (Err=0)
If Err = 0 Then
'We have asource. Try to copy it to the destination
Forms(Destination$)(ControlField$)= Forms(Source$)(ControlField$)
End If
Err = 0 'Clear any errors
Next COUNTER
End Function
Thanks for your help. I hope I have explained it sufficiently.
Then, as I said, there's no need to move Focus to it. It doesn't matter where the Focus is, on the Form, a simple
Call Copy411_Click
will cause its code to fire. But if what you're doing works for you, with no undesired effects, use it!
Glad we could help!
Linq ;0)>
Thanks again.