Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Running DOS comand (A2000 SR1)

    I have an application that requires access to a mapped drive.
    But this drive should only be mapped while the application is open
    and be un-mapped when it is closed.

    Currenly I get the user to run a batch file which maps the drive
    using "net use s: servershare$ Password /u:user"
    then open the database and make any changes
    then run another batch file which un-maps the drive
    using "net use s: /d"

    Can I achieve this more elegantly within the Access application?
    Thanks

  2. #2
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Running DOS comand (A2000 SR1)

    You could try the shell command behind a command button:

    <pre>call Shell ( Environ$ ( "COMSPEC" ) & " /c c:PathAndYourBatchFile.bat", vbNormalFocus) </pre>


  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Mapping Network Drives using API (A2K)

    You can map and "unmap" network drives programatically by using some of the Windows "WNet" API functions that are exported by mpr.dll. For sample code, see the attached text file (exported code module; change extension to ".bas" and import file in VB Editor). To map drive, call MapNetworkConnection (wrapper function for WNetAddConnection2 API). Example:

    <code> Dim lngRtn As Long</code>
    <code> lngRtn = MapNetworkConnection("G:", "ServerNameShareName", "MarkD", "ABC123")</code>

    The function returns zero on success, or an error value on failure. See code module for typical network error values (constants). To "unmap" (or disconnect) network drive, call DisconnectNetConnection (wrapper for WNetCancelConnection2). Example:

    <code> Dim lngRtn As Long</code>
    <code> lngRtn = DisconnectNetConnection("G:", False)</code>

    The second parameter signifies whether or not to force a disconnection if the network device specified by first parameter is in use. I'd recommend "False" for this option unless there's a good reason for the contrary. If mapping/unmapping network drives, it may be a good idea to first determine what drives are available on target system for "temporary" mapping. The EnumDrives sub will enumerate all drives on user's system, the drive type, and if network drive, the share name (aka "remote name" - the drive letters are the "local name" when calling the API functions). The GetUserName function may be useful for determining current user's network user name. The user would probably have to provide the password where required.

    I tested these functions earlier today at work and they worked correctly, as long as the network was "up". Note that on my work system (WIN 2K network), the mapping functions only worked if a valid network user name and password were provided. Otherwise got a "Permission Denied" error.

    HTH

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Mapping Network Drives using API (A2K)

    PS: Another function that may be useful, that I neglected to include in the module prevously posted, is shown below. This is the opposite of the EnumDrives procedure; it is intended to enumerate available drive letters that may be used for mapping network drive. Sample code:

    <code>Public Function GetAvailableDrive(Optional sFirstDriveSpec As String = "A") As String</code>

    <code> Dim sDriveSpec As String</code>
    <code> Dim intDrive As Integer</code>
    <code> Dim lngRtn As Long</code>
    <code> Dim n As Long</code>
    <code> Dim i As Integer</code>

    <code> intDrive = Asc(UCase$(sFirstDriveSpec)) - 65</code>

    <code> If (intDrive >= 0) And (intDrive < 26) Then</code>
    <code> lngRtn = GetLogicalDrives()</code>
    <code> </code>
    <code> For n = intDrive To 25</code>
    <code> If (lngRtn And 2 ^ n) = 0 Then</code>
    <code> sDriveSpec = Chr$(n + 65)</code>
    <code> i = i + 1</code>
    <code> If i = 1 Then</code>
    <code> ' Assign return value, and optionally exit loop::</code>
    <code> GetAvailableDrive = sDriveSpec</code>
    <code> ' Exit For</code>
    <code> End If</code>
    <code> Debug.Print sDriveSpec</code>
    <code> End If</code>
    <code> Next n</code>
    <code> </code>
    <code> If i = 0 Then</code>
    <code> GetAvailableDrive = vbNullString</code>
    <code> MsgBox "No drives available.", vbExclamation, "NO DRIVE AVAILABLE"</code>
    <code> Else</code>
    <code> ' Test msg (if do not exit loop):</code>
    <code> Debug.Print "Available Drives: " & i & " (starting at " & UCase$(sFirstDriveSpec) & ")."</code>
    <code> Debug.Print "First Available Drive: " & GetAvailableDrive</code>
    <code> End If</code>
    <code> Else</code>
    <code> GetAvailableDrive = vbNullString</code>
    <code> MsgBox "Invalid drive specification.", vbExclamation, "ERROR"</code>
    <code> End If</code>

    <code>End Function</code>

    Test results on home system (no network):

    ? GetAvailableDrive("C")
    Available Drives: 21 (starting at C).
    First Available Drive: F

    (The available drive letters were also printed out.) Enumeration starts with letter specified by optional sFirstDriveSpec parameter (or "A" by default). (On many systems network drives typically begin with F: or G: at the "lowest".) The function's return value is first available drive found, or null string if none found. In actual use you would probably comment out some of the Debug Print statements & msgbox's, they are included for test/demo purposes only.

    HTH

  5. #5
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Mapping Network Drives using API (A2K)

    Thanks for the different options.
    As soon as I clear the swamp of alligators I'll get around to draining the swamp.
    At the moment I am swamped with wrestling a recalcitrant ERP/BI implementation.
    Michael

  6. #6
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Mapping Network Drives using API (A2K)

    Finally got around to trying your code and it worked like a dream.
    Thanks

  7. #7
    2 Star Lounger
    Join Date
    Feb 2003
    Location
    Melbourne, Victoria, Australia
    Posts
    139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Mapping Network Drives using API (A2K)

    In my database, I have a link to a text file (S:Users.txt)
    I have a form that displays on startup and I have included your mapping code, to map to S:, in the OnLoad event.
    At startup I am presented with an error dialog that the specified path is invalid (S.
    I click on the OK button and then the form loads and runs the mapping code and I can access the S:Users.txt without a problem.
    Where should I place the drive mapping code so that I don't receive the initial error dialog?
    Thanks in advance.
    Michael

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •