Access: Shell cmd Open MDB

前端 未结 6 1147
野趣味
野趣味 2021-01-21 03:38

I have been using the following command to open another MDB Access file via VBA:

Shell \"cmd /c \" & Chr(34) & strNewFullPath & Chr(34), vbHide


        
相关标签:
6条回答
  • 2021-01-21 04:08

    You can use the Win32 API to find the EXE name associated with the file type and prepend it to your shell command like this:

    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
    
    Public Function GetExecutableForFile(strFileName As String) As String
       Dim lngRetval As Long
       Dim strExecName As String * 255
       lngRetval = FindExecutable(strFileName, vbNullString, strExecName)
       GetExecutableForFile = Left$(strExecName, InStr(strExecName, Chr$(0)) - 1)
    End Function
    
    Sub RunIt(strNewFullPath As String)        
       Dim exeName As String
    
       exeName = GetExecutableForFile(strNewFullPath)         
       Shell exeName & " " & Chr(34) & strNewFullPath & Chr(34), vbNormalFocus
    End Sub
    
    0 讨论(0)
  • 2021-01-21 04:14

    If you want want to use Access VBA to open a database in another Access application instance, you can do this:

    Dim objApp As Access.Application
    Set objApp = New Access.Application
    objApp.UserControl = True
    objApp.OpenCurrentDatabase "C:\Access\sample.mdb"
    Set objApp = Nothing
    

    Setting UserControl to True leaves the new application instance open after the procedure finishes.

    If you want the new Access instance hidden, include:

    objApp.Visible = False
    

    I'm suggesting this approach because it also gives you a way to automate the new application instance through the objApp object variable. But, if you're not interested in automating the new instance, this approach will probably only be useful if you can't make any other method work.

    0 讨论(0)
  • 2021-01-21 04:16

    Here is a slight revision I used to make it work with accdr, where it is required that there be a runtime switch used.

     strShellProg = q & SysCmd(acSysCmdAccessDir) & "msaccess.exe" & q & " /runtime"
    
     strShellProg = strShellProg & " " & q & strCurrentDir & "spfe.accdr" & q
    
     If Shell(strShellProg, vbNormalFocus) > 0 Then
        DoCmd.Hourglass False
        ' DoCmd.Quit
        Application.Quit
     Else
        ' code here for shell not ok
        MsgBox "Unable to run upgrade", vbCritical, AppName
        DoCmd.Hourglass False
        Application.Quit
     End If
    
    0 讨论(0)
  • 2021-01-21 04:17

    I use this function when working in Access 2003:

    Public Function RunExternalMDB(MDBName As String, WG As String, UsrNm As String, Pwd As String)
    
            Shell "MsAccess.exe " & """" & MDBName & """" & " /wrkgrp " & """" & WG & """" & " /user " & UsrNm & " /pwd " & Pwd
    
    End Function
    

    This does work in Runtime mode : )

    0 讨论(0)
  • 2021-01-21 04:18

    Try using Windows Scripting Host Object Model (WSHOM):

    Sub RunFile(filename As String)
    Dim oShell As Object
      Set oShell = GetShell
      If Not oShell Is Nothing Then
        oShell.Run filename
      End If
    End Sub
    Function GetShell() As Object   
      On Error Resume Next     
      Set GetShell = CreateObject("WScript.Shell")  
    End Function 
    

    The Windows file association should allow both types of files to open in their native application.

    Sample Usage:

    RunFile strNewFullPath
    

    Optional Arguments:

    There are two optional arguments for the Run method. Please note that much of this is copied from MSDN:

    1. intWindowStyle (integer) A number from 0 to 10:

      0 - Hides the window and activates another window.
      1 - Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
      2 - Activates the window and displays it as a minimized window.
      3 - Activates the window and displays it as a maximized window.
      4 - Displays a window in its most recent size and position. The active window remains active.
      5 - Activates the window and displays it in its current size and position.
      6 - Minimizes the specified window and activates the next top-level window in the Z order.
      7 - Displays the window as a minimized window. The active window remains active.
      8 - Displays the window in its current state. The active window remains active.
      9 - Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
      10 - Sets the show-state based on the state of the program that started the application.

      I am not aware of the default value for this parameter. Note that some programs simply ignore whatever value you set (I couldn't tell you which ones).

    2. bWaitOnReturn (boolean)

      Set to False for asynchronous code. The Run method returns control to the calling program before completing. Default is False.

    0 讨论(0)
  • 2021-01-21 04:23

    The problem with your shell command is the cmd prompt don't always support using the file extension to start a program. In fact, you better off to use

    Start "path to some file with .extension"

    The above is quite much the same as clicking.

    However, what you really want to do is launch the msacces.exe and SUPPLY the path name to the file for it to open. This is especially the case with a runtime install.

    So your code should look like this:

      Sub testjump()
    
         ' jumps to a mde file called "upgrade.mde"
         ' it exists in the same directly as the currently running program
    
         Dim strShellProg        As String
         Dim strCurrentDir       As String
         Const q                 As String = """"
    
         strCurrentDir = CurrentProject.path & "\"
    
        ' path to msaccess is required here
         strShellProg = q & SysCmd(acSysCmdAccessDir) & "msaccess.exe" & q
    
         strShellProg = strShellProg & " " & q & strCurrentDir & "RidesUpGrade.mdE" & q
    
         If Shell(strShellProg, vbNormalFocus) > 0 Then
            ' code here for shell ok
            Application.Quit
         Else
            ' code here for shell not ok
            MsgBox "Un able to run Rides upgrade", vbCritical, AppName
            Application.Quit
         End If
    
      End Sub
    

    So the above uses the full path name to msaccess.exe. It been tested on xp, vista, win7 etc, and it always worked for me.

    And in the case of more than one version of Access, or that of using a runtime, you may not want to use the extension to launch the file. So this ensures that you are using the SAME version and same .exe that you are currently running. So the above code pulls the current msaccess.exe path you are using, not one based on file extension.

    0 讨论(0)
提交回复
热议问题