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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issuing DOS commands in VBA

Status
Not open for further replies.

RegionsRob

Programmer
Oct 18, 2001
50
US
I want to issue the "NET USE" command to link to a network drive. Can this be done within a VBA module?
 
You can use the Shell function to run a command line program. It runs asynchronously though (Access does not wait for the command run by shell to finish before continuing with the rest of the code). To wait you need to use outside help. I've seen the below recommended but have not tried it myself.

Goto...
Click Samples
And download Shell32.zip

Be sure to note all the licensing information... Free to use but not to distribute the source.
 
Taken from :
Hope this helps

Here's how to shell to another program from Access, stop your code while the shelled process operates and then resume your code once the process is finished. To do this you use the api functions "WaitforSingleObject", and "OpenProcess" to launch a shelled process and wait for it to complete. Listed below is the code to use.
1. On the declarations page of your module, add the following functions:

Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long

2. Try out this test function, which launches any app you want to and waits until it is finished to display a message box (Note an " _ " underscore means line continuation):

Function LaunchApp32 (MYAppname As String) As Integer
On Error Resume Next
Const SYNCHRONIZE = 1048576
Const INFINITE = -1&
Dim ProcessID&
Dim ProcessHandle&
Dim Ret&

LaunchApp32=-1
ProcessID = Shell(MyAppName, vbNormalFocus)
If ProcessID<>0 then
ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID&)
Ret = WaitForSingleObject(ProcessHandle, INFINITE)
Ret = CloseHandle(ProcessHandle)

MsgBox &quot;This code waited to execute until &quot; _
& MyAppName & &quot; Finished&quot;,64
Else
MsgBox &quot;ERROR : Unable to start &quot; & MyAppname
LaunchApp32=0
End If
End Function
3. It is important to note that your function must include the code to close the process handle after the shelled application is complete, otherwise you will have a memory leak until you shut down Windows.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top