You are currently offline, waiting for your internet to reconnect

ACC: Using the Shell() Function to Run MS-DOS Commands

This article was previously published under Q116384
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.
SUMMARY
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 withcreating Microsoft Access applications using the programming tools providedwith Microsoft Access. For more information about Access Basic, pleaserefer to the "Building Applications" manual.
MORE INFORMATION
The Shell() function in Access Basic requires that you specify a file thathas an extension of .EXE, .COM, .BAT or .PIF. These file types areregistered as executable applications in the listed locations in thefollowing 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				

Using the Shell() function to run MS-DOS commands requires the use ofthe COMMAND.COM program. The COMMAND.COM program supports two optionalparameters that you can use to run an intrinsic (or built in) MS-DOSfunction:
  • COMMAND /C: This syntax runs a built in function, and then closes the MS-DOS session.
  • COMMAND /K: This syntax runs a built in function and returns the MS-DOS command prompt.

Example

To create a sample application that demonstrates the use of thesetechniques, follow these steps:
  1. Create a module and type the following line in the Declarations section:
          Option Explicit						
  2. Type the following two functions:
          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						
  3. To test the first function, type the following line in the Immediate window, and then press ENTER:

    ? ShellDOS_Exit()

    Note that the MS-DOS window displays the contents of the current directory (prompting you to press a key if the contents of the directory exceeds one display page) and returns True as a result of the function.
  4. To test the second function, type the following line in the Immediate window, and then press ENTER:

    ? ShellDOS_Stay()

    Note that the MS-DOS window displays the contents of the current directory (prompting you to press a key if the contents of the directory exceeds one display page) and then keeps the MS-DOS session open and active, displaying the MS-DOS command prompt. The function also returns True as a result.
REFERENCES
Microsoft Access "Building Applications," version 2.0, Chapter 5, "AccessBasic 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," andthen "Shell Function" using the Microsoft Access Help menu.

For more information about using MS-DOS commands in Access Basic and on howto add additional functionality to these routines, please see the followingarticle in the Microsoft Knowledge Base:

99940 ACC: How to Wait for a Shelled Process to Finish
Properties

Article ID: 116384 - Last Review: 05/06/2003 17:02:46 - Revision: 2.0

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • kbhowto kbprogramming KB116384
Feedback