Can I simulate a click event via code? (1 Viewer)

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
I have a form with a small navigation subform that provides Previous and Next arrow buttons for stepping through a recordset. I would like to program a KeyDown event for my main form that will trap presses of both the Z and C keys, using Z to trigger a simulated click of the Previous arrow button and using C to trigger a simulated click of the Next arrow button.

I tried the following code but it fails with an "Object doesn't support this property or method" error.

If KeyCode = vbKeyZ Then
Me![SFNavButtons].cmdPrevious_Click
End If

Is it possible to simulate key clicks in code and if so how can it be done?

David
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:22
Joined
Sep 12, 2006
Messages
15,738
If KeyCode = vbKeyZ Then
cmdPrevious_Click
End If

is cmdprevious_click a function in the same form's module.
if so, you can just do the above, i think
 

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
cmdPrevious_Click is the click event subroutine for the cmdPrevious button on my small navigation subform. The KeyDown code is on the main form and thus needs to reference the click event code in the separate subform module.

David
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:22
Joined
Sep 12, 2006
Messages
15,738
then you have to make it a public event

and the comand will be something like

forms!formname.cmdPrevious_Click

I had the same issue a couple of days ago!
 
Last edited:

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
I've already tried that and get the same error. I simplified the previous description of my form structure. In reality, my small navigation subform is a subform of a subform.

The actual code I have tried that fails is as follows:

Forms![ScoringForm]![ScoringSF]![SFfrmNavButtons].cmdPrevious_Click
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:22
Joined
Sep 12, 2006
Messages
15,738
not sure then without testing at trying, but there IS a syntax to do it!
 

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
Having failed to make my real app work as desired, I solved the problem by simply recreating my record navigation code for the required KeyDown events. However, I am still interested to find out if it is possible to simulate a button click via code.

I created a very simple test form that displays three fields from a table, one record at a time. I then added a couple of record navigation buttons, Move_Next and Move_Prev, which work fine. This time there are no subform complications to confuse matters.

I then created a KeyDown event to trap the right and left arrow keys and coded them to trigger the code for the above mentioned buttons.

e.g.
If KeyCode = vbKeyRight Then
Forms![TestKeyDown].MoveNext_Click
End If

This time, I get an "Application-defined or object-defined error". I'm beginning to think that Access will not let you simulate a button click via code.....

David
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:22
Joined
Sep 12, 2006
Messages
15,738
have you made the sub/function

movenext_click public

you may not need the square brackets

try just

forms!testkeydown.movenext_click
 

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
Dave,
You have solved the problem (at least for my test form). Making the button click subs 'Public' did the job. I should have worked that one out for myself, but many thanks for highlighting my error!

BTW, I use the square brackets out of habit because they are necessary if there is an embedded blank in the name of a form. Where there are no embedded blanks, it doesn't matter whether or not square brackets are used.

David
 

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
Unfortunately, this problem is not yet fully solved. When I tried to implement the above solution in my live app I just got the same "Object doesn't support this property or method" error message as before, despite changing my button click subs from Private to Public.

I returned to my simple test form and embedded it in a new blank form as a subform. The coding was modified as follows to reflect the subform structure:

If KeyCode = vbKeyRight Then
Forms![TestKeyDown]![TestKeyDownSF].MoveNext_Click
End If

This test form now failed with the "Object doesn't support this property or method" error message as soon as I hit the right arrow key. We have not yet found the correct way to refer to code contained in a subform or Access is unwilling to perform this sort of action.

David
 

SOS

Registered Lunatic
Local time
Yesterday, 20:22
Joined
Aug 27, 2008
Messages
3,514
Well, if you use correct syntax to refer to the subform:

Me.SubformControlName.Form.ControlName_Click


Subform syntax has to refer to the control that houses the subform (not the subform itself unless the subform control which houses the subform on the main form and the subform have the same exact name) and then the .Form. part tells Access you want something on that form.

So, in your case it would be:
Code:
If KeyCode = vbKeyRight Then
   Forms!TestKeyDown.TestKeyDownSF[COLOR=red].Form.[/COLOR]MoveNext_Click
End If
IF TestKeyDownSF is also the name of the subform CONTROL. If it isn't then you would use that name, so if the control is Child2 then it would be:

Code:
If KeyCode = vbKeyRight Then
   Forms!TestKeyDown[COLOR=red].Child2[/COLOR][COLOR=red].Form.[/COLOR]MoveNext_Click
End If

And if this code is actually on the main form you can simply use:

Code:
If KeyCode = vbKeyRight Then
   Me.TestKeyDownSF[COLOR=red].Form.[/COLOR]MoveNext_Click
End If

or


Code:
If KeyCode = vbKeyRight Then
   Me.[COLOR=red]Child2[/COLOR][COLOR=red].Form.[/COLOR]MoveNext_Click
End If
 

David Anderson

Registered User.
Local time
Today, 04:22
Joined
Nov 18, 2007
Messages
84
Thanks SOS. That was indeed the missing link. Adding .Form just before the name of the sub has fixed the problem in both my test form and my real world app.

I now realise that I used to know about the need for the .Form reference in certain circumstances but a year away from Access coding makes a lot of stuff disappear from your conscious memory!

Thanks again to both you and Dave for putting me straight.

David
 

Users who are viewing this thread

Top Bottom