?
Solved

Sendkeys works on one computer but not on another

Posted on 2009-04-29
5
Medium Priority
?
383 Views
Last Modified: 2012-05-06
I have a sendkeys program that sends a bunch of keystrokes to another program and then fills in a description and an ID, then adds another record and starts the process over again (until if finishes all the records in a database).

The Description is always less than 14 characters and the ID is always 4 digits. They are both read from a database.

The program works fine on a few computers, but on my Acer Aspire One, it writes one record, adds another, and then stops. If I put in a Wait, it keeps adding records but it doesn't put the data in the correct places and the keystokes are all messed up. I tried putting waits in different places and have not had any luck.

See code below.

Any help would be appreciated.
For i = 0 To lTotalRecords - 1
        SendKeys rsData!Description
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys "{RIGHT}"
        SendKeys "{TAB}"
        SendKeys "{TAB}"
        SendKeys rsData!ID
        SendKeys "{TAB}"
        SendKeys "{LEFT}"
        SendKeys "%R"
        SendKeys "A"
        SendKeys "{TAB}"
    
        rsData.MoveNext
    Next i

Open in new window

0
Comment
Question by:esmyhs
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
FER_G earned 2000 total points
ID: 24261100
esmyhs; it is documented that VB6 sendkeys method could fail in some environments, so it is recomended that you replace it with the "keybd_event" api call.

...see the code snippet below

Regards
FerG
--
Saludos
Ing. Fernando Giletta
San Francisco. Cordoba. Argentina.
Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1
 
'Api keybd_event
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
 
Sub sendKey_API(key As Long)
    Call keybd_event(key, 0, 0, 0)
    Call keybd_event(key, 0, KEYEVENTF_KEYUP, 0)
End Sub
 
'Example of sending tab key
Private Sub cmd1()
Call sendKey_API(vbKeyTab)
End Sub

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24261651
The keybd_event is not rock solid either...it just presses the keys and the currently focuses application receives them (just like SendKeys).  It is easy for the "automation" process to be interrupted by an errant mouse/key press and the whole process gets thrown out of whack.  =\

A more reliable approach would be to send messages directly to the controls with SendMessage().  You are then guaranteed to get the correct strings in the desired fields regardless of app focus and whether the user is doing something else with the system.

It is a more difficult approach, though, and requires alot more initial work to setup.  Start by downloading the free Winspector so you can determine the hierarchy of controls in the target form.  You have to start with the handle of the main form and then "drill down" thru the structure of controls to find the desired window handles:
http://www.windows-spy.com/

Let me know if you want to go this route...  (it may require much interaction from you)
0
 

Author Comment

by:esmyhs
ID: 24264923
FER G:

How would you send an ALT+R with your method?
0
 

Author Comment

by:esmyhs
ID: 24264952
FER G:

Also how would I send the record from the database. I'm getting a Type Mismatch on the following:

Call sendKey_API(rsData!Description)
0
 
LVL 7

Expert Comment

by:FER_G
ID: 24268893
to send a combination of keys, you must use the next code; for example to send ALT+R
'(THIS WILL BE INTERPRETED AS PRESS ALT, PRESS R, RELEASE R, RELEASE ALT)
            Call keybd_event(vbKeyMenu, 0, 0, 0) 'vbKeySelect
            Call keybd_event(vbKeyR, 0, 0, 0) 'vbKeySelect
            Call keybd_event(vbKeyR, 0, KEYEVENTF_KEYUP, 0)
            Call keybd_event(vbKeyMenu, 0, KEYEVENTF_KEYUP, 0)

but you can't send a string with "keybd_event", for that you must use the sendMessage api call.

Regards
FerG
--
Saludos
Ing. Fernando Giletta
San Francisco. Cordoba. Argentina.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question