Microsoft Knowledge Base Email Alertz

KBAlertz.com: Advanced: Requires expert coding, interoperability, and multiuser skills.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 138905 - Last Review: January 19, 2007 - Revision: 3.4

ACC: Using Win32 API to Connect to Network Resources (95/97)

This article was previously published under Q138905

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

The Win32 application programming interface (API) can be used to connect and disconnect from network drives and printers using Visual Basic for Applications.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

To use the WIN32 application programming interface (API) to connect and disconnect from network drives and printers, follow these steps:
  1. On the Insert Menu, click Module to add a new module to your database.
  2. Type the following lines in the Declarations section:
          Option Explicit
    
    
          Declare Function WNetAddConnection Lib "mpr.dll" Alias _
           "WNetAddConnectionA" (ByVal lpszNetPath As String, _
           ByVal lpszPassword As String, ByVal lpszLocalName As String) _
           As Long
    
          Declare Function WNetCancelConnection Lib "mpr.dll" Alias _
           "WNetCancelConnectionA" (ByVal lpszName As String, _
           ByVal bForce As Long) As Long
    
          Const WN_SUCCESS = 0        ' The function was successful.
          Const WN_NET_ERROR = 2      ' An error occurred on the network.
          Const WN_BAD_PASSWORD = 6   ' The password was invalid.
    						
  3. Create the following function that establishes the connection:
          Function AddConnection(MyShareName$, MyPWD$, UseLetter$) As Integer
             On Local Error GoTo AddConnection_Err
             AddConnection = WNetAddConnection(MyShareName$, MyPWD$, _
              UseLetter$)
          AddConnection_End:
             Exit Function
          AddConnection_Err:
             AddConnection = Err
             MsgBox Error$
             Resume AddConnection_End
          End Function
    						
    NOTE: Some of the possible return values for the AddConnection() function include WN_SUCCESS, WN_NET_ERROR, and WN_BAD_PASSWORD. Other run-time errors could be returned from the function; therefore, error trapping should be implemented to handle any problems.
  4. Create the following function that cancels a connection. The parameter Force% specifies whether any open files or open print jobs on the device should be closed before the connection is canceled. If this parameter is False (numeric equivalent of 0) and there are open files or jobs, the connection will not be canceled.
          Function CancelConnection(UseLetter$, Force%) As Integer
             On Local Error GoTo CancelConnection_Err
             CancelConnection = WNetCancelConnection(UseLetter$, Force%)
          CancelConnection_End:
             Exit Function
          CancelConnection_Err:
             CancelConnection = Err
             MsgBox Error$
             Resume CancelConnection_End
          End Function
    						
    NOTE: Two of the most common return values for CancelConnection() are WN_SUCCESS and WN_NET_ERROR.
  5. To test these functions open the Debug Window, type the following lines, and then press ENTER
    ?AddConnection(<"\\servername\sharename">, <"MyPwd">, <"y:">)
    where <\\servername\sharename> is a valid server and sharename is your network organization and <MyPwd> is your password that provides permission to the resource.
    ?CancelConnection(<"y:">, 0)
    NOTE: Both functions should return WN_SUCCESS (numeric equivalent of 0), provided the functions run correctly.

REFERENCES

For more information about how to obtain an unused drive resource programmatically, see the following article in the Microsoft Knowledge Base:
138904  (http://kbalertz.com/Feedback.aspx?kbNumber=138904/EN-US/ ) Connecting to the First Available Network Drive (95/97)
For more information about Declare, search for "Declare," and then "Declare Statement" using the Microsoft Access Help Index.

APPLIES TO
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
Keywords: 
kbinfo kbprogramming KB138905
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please