Shell Command

We need to run an Access database/program by clicking an action button in a Notes database. Unfortunately, Access has been installed in several different locations on various machines. There are at least 3 different paths that I know of. We tried using the following code:

Sub Click(Source As Button)

Dim taskId As Integer

On Error Goto trySecondDrive

taskId% = Shell("C:\Program Files\Microsoft Office\Office\MSACCESS.EXE N:\FileToRun /wrkgrp N:\workGroupToUse", 1)

Exit Sub

trySecondDrive:

taskId% = Shell(“C:\Program Files\Microsoft Office\Office2K\MSACCESS.EXE N:\FileToRun /wrkgrp N:\workGroupToUse”, 1)

End Sub

…but that doesn’t work. If the machine that tries to execute has access in the second location, we get an illegal function call, so I assume that we the shell command tries to execute and fails that it is not returning control to Notes. Is there some other way to write this?

Subject: Shell Command

You could locate the installation directory from the registry. At a glance, it looks like you could get the information needed with this key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE

Here are a few ideas for reading the registry key:

use the Win32 API (see function below),

use Windows Scripting Host - see here for more info (pretty easy stuff . . . cool too), or

use @RegQueryValue.

hth,

dgg

Declare Private Function W32RegOpenKeyEx Lib “advapi32.dll” Alias “RegOpenKeyExA” ( _

Byval hKey As Long, _

Byval lpSubKey As String, _

Byval ulOptions As Long, _

Byval samDesired As Long, _

phkResult As Long _

) As Long

Declare Private Function W32RegQueryValueEx Lib “advapi32.dll” Alias “RegQueryValueExA” ( _

Byval hKey As Long, _

Byval lpValueName As String, _

Byval lpReserved As Long, _

lpType As Long, _

Byval lpData As String, _

lpcbData As Long _

) As Long

Declare Private Function W32RegCloseKey Lib “advapi32.dll” Alias “RegCloseKey” ( _

Byval hKey As Long _

) As Long

Public Function getRegistryKeyValue(plngHMainKey As Long, pblnAppend As Boolean, pstrSubKey As String, pstrKeyName As String, pstrValOut As String) As Boolean

'// +++ GLOBAL VARIABLES +++

'// Constants:

'// none

'//

'// Class instances:

'// none

'//

'// Primitives:

'// none

'//

'// 06/28/2004 - Dallas Gimpel

'//

'// DESCRIPTION:

'// This function allows a registry key value to be retrieved via Win/32 API.

'//

'// NOTE:

'// These are the constants for the main registry to be passed by the calling module as

'// the first parameter:

'// Const HKEY_CLASSES_ROOT = &H80000000

'// Const HKEY_CURRENT_USER = &H80000001

'// Const HKEY_LOCAL_MACHINE = &H80000002

'// Const HKEY_USERS = &H80000003

'// Const HKEY_PERFORMANCE_DATA = &H80000004

'//

'// INPUT:

'// plngHMainKey - Long, the main key to be used for querying the registry

'// pblnAppend - Boolean, indicates whether or not to append results to the string passed

'// pstrSubKey - String, the sub key to be used for querying the registry

'// pstrKeyName - String, the key name whose value is to be retrieved

'//

'// OUTPUT:

'// pstrValOut - String, receives the registry value retrieved (if any)

'// Function returns a boolean (true if successfull, otherwise false).

On Error Goto errorHandler

'// These are for the W32 API

Const KEY_QUERY_VALUE = &H1

Const KEY_SET_VALUE = &H2

Const KEY_CREATE_SUB_KEY = &H4

Const KEY_ENUMERATE_SUB_KEYS = &H8

Const KEY_NOTIFY = &H10

Const KEY_CREATE_LINK = &H20



Const READ_CONTROL = &H20000

Const WRITE_DAC = &H40000

Const WRITE_OWNER = &H80000

Const SYNCHRONIZE = &H100000



Const STANDARD_RIGHTS_REQUIRED = &HF0000

Const STANDARD_RIGHTS_READ = READ_CONTROL

Const STANDARD_RIGHTS_WRITE = READ_CONTROL

Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL

Const STANDARD_RIGHTS_ALL = &H1F0000



Const KEY_READ = (STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And Not SYNCHRONIZE

Const KEY_WRITE = (STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And Not SYNCHRONIZE

Const KEY_EXECUTE = KEY_READ And Not SYNCHRONIZE

Const KEY_ALL_ACCESS = (STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And Not SYNCHRONIZE

Const REG_SZ = 1



Dim lngRetCode As Long

Dim lngHKey As Long 

Dim lngKeyType As Long

Dim lngBuffLen As Long

Dim strBuffer As String



getRegistryKeyValue = False

lngRetCode& = W32RegOpenKeyEx(Byval plngHMainKey&, Byval pstrSubKey$, 0&, KEY_ALL_ACCESS, lngHKey&) 

If lngRetCode& = 0 Then

	lngBuffLen& = 255

	strBuffer$ = String(lngBuffLen&, 0)

	lngRetCode& = W32RegQueryValueEx(lngHKey&, pstrKeyName$, lngKeyType&, 0&, strBuffer$, lngBuffLen&)

	lngRetCode& = W32RegCloseKey(lngHKey&)

	strBuffer$ = Strleftback(Left$(strBuffer$, lngBuffLen&), Chr(0))

	If pblnAppend Then

		pstrValOut$ = pstrValOut$ & strBuffer$

	Else

		pstrValOut$ = strBuffer$

	End If

	getRegistryKeyValue = True

End If

functionExit:

Exit Function

errorHandler:

Msgbox "Error & " & Err & ": " & Error$ & " encountered at line " & Erl & " of " & Getthreadinfo(1) & ".", , "Error encountered . . ."

Print "Error & " & Err & ": " & Error$ & " encountered at line " & Erl & " of " & Getthreadinfo(1) & " . . ."

Resume functionExit

End Function

Subject: RE: Shell Command

Thanks for all the info, Dallas. The last time I checked, no one had responded, and then I looked today and I was pleasantly surprised. Thanks again.

Kevin

PS

I did check that location in the registry and I was a little surprised at what I found, but I guess I shouldn’t have been. We use a labeling system that was written in Access. At first this system had to run under Access97. It included a runtime version. We moved up to Access2000, but we had to keep Access97 installed for the labeling system. Then we upgraded the system to Access2000. Not sure who, what or when populates that registry key, but it was pointing to the runtime version of access. Maybe that was the last version installed?