
Originally Posted by
Sense
Okay thanks! So it's better to use functions. Is there any reason why I should use subs instead of Functions?
Some developers only use functions even where a sub will work - the idea being it gives maximum flexibility
Other developers of course think that is totally inappropriate.
The code below allows you to easily use the Shell command in Access
Place the code in a standard module
Code:
Option Compare Database
Option Explicit
'API declarations for 32-bit/64-bit Access
'###############################################
#If VBA7 Then 'use PtrSafe & LongPtr
Private Declare PtrSafe Function apiShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hWnd As LongPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
#Else '32-bit Office
Private Declare Function apiShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
#End If
'###############################################
Public Sub ShellEx(ByVal Path As String, Optional ByVal Parameters As String, Optional ByVal HideWindow As Boolean)
If Dir(Path) > "" Then
apiShellExecute 0, "open", Path, Parameters, "", IIf(HideWindow, 0, 1)
Else
MsgBox "Can't find program"
End If
End Sub
Here are 3 examples of it in use that you can try
Code:
Public Function OpenMagnifier()
ShellEx "c:\windows\system32\magnify.exe", , 0
End Function
Public Function RunOSK()
'opens on screen keyboard
apiShellExecute 0, vbNullString, "osk.exe", vbNullString, "C:\", 1
' ShellEx "c:\windows\system32\osk.exe", , True
End Function
Public Function OpenTabTip()
'opens tablet screen keyboard
ShellEx "C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe", , True
End Function