Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Locate Access' executable 3

Status
Not open for further replies.

Lascar

Technical User
Oct 17, 2003
29
CA
How can I locate Access' executable file, msaccess.exe, by VB6?
 
If running IE5 or higher the easiest way to find any executable, given you know a registered file extension, is the AssocQueryString API function.
Code:
Const ASSOCF_INIT_NOREMAPCLSID = &H1 'do not remap clsids to progids
Const ASSOCF_INIT_BYEXENAME = &H2 'executable is being passed in
Const ASSOCF_OPEN_BYEXENAME = &H2  'executable is being passed in
Const ASSOCF_INIT_DEFAULTTOSTAR = &H4  'treat "*" as the BaseClass
Const ASSOCF_INIT_DEFAULTTOFOLDER = &H8 'treat "Folder" as the BaseClass
Const ASSOCF_NOUSERSETTINGS = &H10  'dont use HKCU
Const ASSOCF_NOTRUNCATE = &H20 'dont truncate the return string
Const ASSOCF_VERIFY = &H40  'verify data is accurate (DISK HITS)
Const ASSOCF_REMAPRUNDLL = &H80  'actually gets info about rundlls target if applicable
Const ASSOCF_NOFIXUPS = &H100  'attempt to fix errors if found
Const ASSOCF_IGNOREBASECLASS = &H200  'dont recurse into the baseclass

Const ASSOCSTR_COMMAND = 1 'return the shell\verb\command string
Const ASSOCSTR_EXECUTABLE = 2 'return the the executable part of command string

Const S_OK = 0
Const E_POINTER = &H80004003

Declare Function AssocQueryString Lib "shlwapi.dll" _
Alias "AssocQueryStringA" (Byval flags As Long, _
Byval pstr As Long, Byval pszAssoc As String, _
Byval pszExtra As String, Byval pszOut As String, _
Byval pcchOut As Long) As Long

Function fGetAppPath(Byval strExt As String) As String

 '--- Returns the path to an executable or an empty string if an error occurs

 '--- Input
 ' strExt is the three character file extension associated with the executable

 Dim lngRtn As Long
 Dim lngBuffLen As Long
 Dim strAppPath As String
 Dim lngFlags As Long

 'Check strExt isn't empty
 If strExt = vbNullString Then
  Exit Function
 End If

 'Set the flags
 lngFlags =  ASSOCF_NOTRUNCATE Or ASSOCF_REMAPRUNDLL
 'Null terminate the file extension
 strExt = "." & strExt & Chr$(0)

 'Size the buffer for the return value
 strAppPath = Space(255)
 lngBuffLen = 255

 'Get the exe path
 lngRtn = AssocQueryString(lngFlags, ASSOCSTR_EXECUTABLE, _
 strExt, "", strAppPath, lngBuffLen)
 'Check the result
 Select Case lngRtn
 Case S_OK
  'Success, do nothing
 Case E_POINTER
  'Buffer was too small - resize it and try again
  strAppPath = Space(lngBuffLen)
  lngRtn = AssocQueryString(lngFlags, ASSOCSTR_EXECUTABLE, _
  strExt, "", strAppPath, lngBuffLen)
 Case Else
  'An error occurred - exit
  Exit Function
 End Select

 'Strip the terminating null char and remaining spaces
 fGetAppPath = Left$(strAppPath, Instr(1, strAppPath, Chr$(0)) -1)

End Function

Then call it, passing xls as the parameter:

strXLPath = fGetAppPath("xls")

Paul Bent
Northwind IT Systems
 
I want to specifically locate msaccess.exe with my VB program. Since I'm using access' databases also, isn't there a way for me to get that from the properties, objects, events or something?
 
Just use "mdb" as the parameter in the function I gave you. The path to the access executable isn't stored in an mdb file. You can only get the path to the mdb, not msaccess.exe.

Paul Bent
Northwind IT Systems
 
Where should I look for the findexecutable API?
 
Details of most API calls are here:

or you could use Google!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
FindExecutable requires a file associated with the application which usually involves getting the temp folder, creating a file, calling FindExecutable then deleting the file.

AssocQueryString merely requires a string specifying the registered file extension.

Paul Bent
Northwind IT Systems
 

paulbent,

Not really. You dont have to use the temp folder you can use app.path if you want or if Lascar knows the path to the MDB they could use that.

 
So I can use the app.path & put 'msaccess.exe' as the file to find?
 

No, you need a file with the .mdb extension (could be a dummy file as long as it has the .mdb extension) to find the path to the handling exe. For example, if you wanted to find the handling exe for *.txt files you would pass it in the path and the file name to a text file.

Good Luck

 
I don't see where I can specify to return the path of the handling exe. Can you give me an exemple of what the line would look like in VB.
 

Start a new project and add this to form1...
[tt]
Option Explicit

Private Sub Command1_Click()
MsgBox GetHandlingExeForFileType("txt")
MsgBox GetHandlingExeForFileType(, "somepathtoyourfile")
End Sub
[/tt]

Then add a module and add the following...
[tt]
Option Explicit

Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Function GetHandlingExeForFileType(Optional FileTypeExtension As String = vbNullString, Optional PathFileName As String = vbNullString) As String

On Error GoTo GetHandlingExeForFileTypeError

Dim TempPath As String, PathLength As Long, FileNumber As Integer, TempResult As String

If Trim(FileTypeExtension) = vbNullString And Trim(PathFileName) = vbNullString Then

GetHandlingExeForFileType = vbNullString
Exit Function

ElseIf Trim(FileTypeExtension) <> vbNullString Then

TempPath = String(2048, vbNullChar)
PathLength = GetTempPath(2048, TempPath)

If PathLength = 0 Then
GetHandlingExeForFileType = vbNullString
Exit Function
End If

If InStr(1, FileTypeExtension, &quot;.&quot;) > 0 Then FileTypeExtension = Mid(FileTypeExtension, 2)

TempPath = Left(TempPath, InStr(1, TempPath, vbNullChar) - 1)
If Right(TempPath, 1) <> &quot;\&quot; Then TempPath = TempPath & &quot;\&quot;

TempPath = TempPath & &quot;temp.&quot; & FileTypeExtension

FileNumber = FreeFile

Open TempPath For Output As #FileNumber

Close #FileNumber

ElseIf Trim(PathFileName) <> vbNullString Then

TempPath = PathFileName

End If

TempResult = String(3072, vbNullChar)

If FindExecutable(TempPath, vbNullChar, TempResult) > 31 Then

If InStr(1, TempResult, vbNullChar) > 1 Then

GetHandlingExeForFileType = Left(TempResult, InStr(1, TempResult, vbNullChar) - 1)

Else

GetHandlingExeForFileType = vbNullString

End If

End If

If Trim(FileTypeExtension) <> vbNullString Then Kill TempPath

Exit Function
GetHandlingExeForFileTypeError:

MsgBox Err.Description

End Function
[/tt]

Good Luck


 
And last, but not least, you can also use the registry to get the path of MS Access executable from the 'App Paths' key.

The following code segment uses the Windows Scripting Host to query the path of MSACCESS.EXE from the registry. You can also use registry access APIs to retrieve this value if you wish.
___
[tt]
MsgBox CreateObject(&quot;WScript.Shell&quot;).RegRead(&quot;HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE\&quot;)[/tt]
 
Oh yes, I found the file! Thanks everybody for your help!
 

Well... how or who??? And do you think the poster was helpful? or was it an expert post in your opinion?


 
Lascar, What is meant is that it is important to identify what actually works and was useful.
This helps other members, who may someday have the same question, to identify what the solution was.

And, using the option:
&quot;Mark this post as a helpful/expert post!&quot;

which you will fine at the bottom of each post, will mark the post (with a star) as such.

Read FAQ222-2244

And, Welcome to Tek-Tips!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top