ACC: Using the Shell() Function to Run MS-DOS CommandsID: Q116384
|
Moderate: Requires basic macro, coding, and interoperability skills.
This article describes and gives an example of how you can use the Shell()
function in Access Basic to run intrinsic MS-DOS commands, such as Copy,
Dir, Del, and so on.
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools provided
with Microsoft Access. For more information about Access Basic, please
refer to the "Building Applications" manual.
The Shell() function in Access Basic requires that you specify a file that
has an extension of .EXE, .COM, .BAT or .PIF. These file types are
registered as executable applications in the listed locations in the
following operating environments:
Windows 3.x and Windows for Workgroups 3.x
------------------------------------------
Filename: WIN.INI
Section: [windows]
Item: Programs=com exe bat pif
Windows NT 3.x
----------------------------------------------------------------------
Filename: REGEDT32.EXE
Registry Key:
HKEY_CURRENT_USER\Software\Microsoft\WindowsNT\CurrentVersion\Windows
Value: Programs
Type: REG_SZ
Item: com exe bat pif cmd
Option Explicit
Function ShellDOS_Exit() As Integer
On Local Error Goto ShellDOS_Exit_Err
Dim MyCommand As String
Dim TaskId As Integer
' Create command string to show the contents of current
' directory. Upon completion the window closes.
MyCommand = "COMMAND.COM /C DIR /P"
TaskId = Shell(MyCommand, 1)
ShellDOS_Exit = True
ShellDOS_Exit_End:
Exit Function
ShellDOS_Exit_Err:
MsgBox Error$
Resume ShellDOS_Exit_End
End Function
Function ShellDOS_Stay() As Integer
On Local Error Goto ShellDOS_Stay_Err
Dim MyCommand As String
Dim TaskId As Integer
' Create command string to show the contents of current
' directory. Upon completion the window remain opens
' at the MS-DOS prompt.
MyCommand = "COMMAND.COM /K DIR /P"
TaskId = Shell(MyCommand, 1)
ShellDOS_Stay = True
ShellDOS_Stay_End:
Exit Function
ShellDOS_Stay_Err:
MsgBox Error$
Resume ShellDOS_Stay_End
End Function
Microsoft Access "Building Applications," version 2.0, Chapter 5, "Access
Basic Fundamentals," pages 113-132
Microsoft Access "Language Reference," version 2.0, "Shell Function,"
pages 568-569
For more information about the Shell() function, search for "Shell," and
then "Shell Function" using the Microsoft Access Help menu.
For more information about using MS-DOS commands in Access Basic and on how
to add additional functionality to these routines, please see the following
article in the Microsoft Knowledge Base:
Q99940 ACC: How to Wait for a Shelled Process to Finish
Keywords : kbprg
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 3, 1999