Access VBA Macro Programming

(Joao Candeias) #1
The other command to use before you send keys isAppActivate. If the application is
already loaded, you do not need to use theShellfunction to load it in again, but you do need
to switch the focus over to that application so it can send the keys. This allows your code to
activate another application already loaded by use of the title in the header bar:

AppActivate "Microsoft Word"

In this way, you can perform simple automation of other applications:

Sub test_sendkeys()

x = Shell("calc.exe")

For n = 1 To 10

SendKeys n & "{+}", True

Next n

MsgBox "Press OK to close calculator"

AppActivate "calculator"

SendKeys "%{F4}", True

End Sub

In this example, the Windows Calculator is loaded, and then a For..Next loop makes it add up
the numbers from 1 to 10.
The message box will appear on the Access application, because that is where the code is
running from. The Access icon will flash on the Windows toolbar. Select Access and click
OK, and the calculator will close.
The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses (( )) have special
meanings toSendKeys. To specify one of these characters, enclose it in braces ({}). For
example, to specify the plus sign, type{+}. Brackets ([ ]) have no special meaning toSendKeys,
but you must enclose them in braces.
To specify special characters that aren’t displayed when you press a key, such asENTERor
TA B, and keys that represent actions rather than characters, use the codes shown in Table 5-7.
To specify keys combined with any combination of theSHIFT,CTRL, andALTkeys, precede
the key code with one or more of the following codes:

Key Code
SHIFT +
CTRL ^
ALT %

64 Microsoft Access 2010 VBA Macro Programming

Free download pdf