What is an alternative to using sendkeys?

Hey all,

I've been looking for methods to "click" buttons on navigation forms programmatically and have come across sendkeys "~".

However, this comes with a warning of "a solution, but would advise against using this!". What is an alternative that can be used instead?

Thanks,

Sarith
Sarith GadaData AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Sarith,

   Depends on the situation, but the only alternative is to execute the code directly.   For example, calling the OnClick event rather than "clicking" the button with SendKeys.

Jim.
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Oh and the reason using SendKeys is a bad idea is that at the moment it's executed, it gets sent to the current window.   If your user switches windows in the middle of your code executing, the sendkeys may go to the wrong place.

Jim.
Bill PrewIT / Software Engineering ConsultantCommented:
One other point.  Using SENDKEYS is all based on assumptions about how the application / form you are trying to control is built and organized.  For example if today FIELD1 and FIELD2 are right next to each other, then you might send a single TAB character to get from FIELD1 to FIELD2 and send values.  But if the form design changes and now there is an extra field between those two, your code breaks and starts filling the wrong data into the wrong places.  This is a simple example of the risks of SENDKEYS, it can go very bad if everything isn't exactly as it was when the key sequences were first constructed.


»bp
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Bill PrewIT / Software Engineering ConsultantCommented:
What application / form are you trying to automate?  The answer to that question could drive an answer to your question about what are the alternatives.


»bp
Shaun VermaakTechnical SpecialistCommented:
Back in the day I used Kixtart scripts
http://www.kixtart.org/
Sarith GadaData AnalystAuthor Commented:
Hey all,

Thank you very much for your responses.

I have built a navigation form housing other navigation forms within it.

Depending on the level of a user, I want certain navigation buttons to be visible.

What currently happens is that the button is invisible, but the underlying data is visible.

I used a sendkeys to "click" on the first available button.


The main navigation form is called frmNavMaster and one of the sub-navigation forms is called frmNavMasterStage3. Within this, some buttons will be visible and I want the first button with code visible to be displayed when a user selects frmNavMaster3 from frmNavMaster.

Here is some code I've used but have now gotten stuck on the browse to for frmBlank, a subform in frmNavMaster3.

Private Sub Form_Load()

If TempVars!TempLevel = 1 Then

    Me.NB1.Visible = True
    Me.NB1.SetFocus
    SendKeys "~"
    Me.NB2.Visible = True
    Me.NB3.Visible = True
    Me.NB4.Visible = True
    Me.NB5.Visible = True
    Me.NB6.Visible = True
    Me.NB7.Visible = True
    Me.NB8.Visible = False

ElseIf TempVars!TempLevel = 2 Then

    Me.NB1.Visible = True
    Me.NB1.SetFocus
    SendKeys "~"
    Me.NB2.Visible = True
    Me.NB3.Visible = True
    Me.NB4.Visible = True
    Me.NB5.Visible = True
    Me.NB6.Visible = True
    Me.NB7.Visible = True
    Me.NB8.Visible = False

ElseIf TempVars!TempLevel = 3 Then

    Me.NB1.Visible = False
    Me.NB2.Visible = False
    Me.NB3.Visible = False
    Me.NB4.Visible = False
    Me.NB5.Visible = False
    Me.NB6.Visible = False
    Me.NB7.Visible = False
    Me.NB8.Visible = True
    Me.NB8.SetFocus
    'SendKeys "~"
    DoCmd.BrowseTo acBrowseToForm, "frmBlank", "frmNavMaster.Navigationsubform>frmNavMasterStage3.frmBlank"
    
    

Else

    Me.NB1.Visible = True
    Me.NB1.SetFocus
    SendKeys "~"
    Me.NB2.Visible = True
    Me.NB3.Visible = True
    Me.NB4.Visible = True
    Me.NB5.Visible = True
    Me.NB6.Visible = True
    Me.NB7.Visible = True
    Me.NB8.Visible = True
    
End If

End Sub

Open in new window


I hope that makes it a little clearer...

Thanks,

Sarith
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Just do:

 Call Me.NB1_Click

 or simply:

 Me.NB1_Click


 and note that the procedure must be public, not private.

Jim.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sarith GadaData AnalystAuthor Commented:
Hey all,

Thanks for all the assistance.

Jim, your solution was the simplest to adapt for my needs.

Thanks again!

Sarith
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.