Access: Shell cmd Open MDB

前端 未结 6 1187
野趣味
野趣味 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: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.

提交回复
热议问题