Results 1 to 7 of 7

Thread: Mapping network drive

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    10

    Mapping network drive

    I am trying to map a network drive, and I am trying to use the following:
    VB Code:
    1. Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    2.  
    3.     Private Sub Form_Load()
    4.         WNetAddConnection("\\server1\folderA", "", "G:")
    5.        WNetAddConnection("\\server1\folderB", "", "H:")
    6.     End Sub

    I need this to be executed when the form loads, however, when I do this under the declaration
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    I get errors for "Declare", "Private Sub", and for "WNetAddConnection". The erros are as follows:
    "Declare" = Keyword is not valid as an identifier
    "Private Sub" = Statement cannot appear within a method body. End of method assumed
    "WNetAddConnection" = Name 'WNetAddConnection' is not declared.

    I have to warn you guys, I am fairly new to VB so if I am doing something stupid, please take it easy on me. I appreciate your help.

    FMNeo25

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Mapping network drive

    Why do you have to create a map, rather than using UNC names? Also, be careful when hardcoding path names. Usually I keep those in a config file.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mapping network drive

    Show us the entire section of code. It sounds like you are trying to make declarations in a method that need to be made at the class level.

    Also, do take heed to what wild_bill says. You cannot know that those drive letters do not already exist on the current machine without checking first, and if they do then you would need to choose a different letter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    10

    Re: Mapping network drive

    Quote Originally Posted by jmcilhinney
    Show us the entire section of code. It sounds like you are trying to make declarations in a method that need to be made at the class level.

    Also, do take heed to what wild_bill says. You cannot know that those drive letters do not already exist on the current machine without checking first, and if they do then you would need to choose a different letter.
    I understand about the drive. However, the reason I am doing it like that is because the drive is assigned for our use and no one else will use it. It has been reserved. Now, granted, a user can map anything that he/she wants manually to the drive I need, so in that case what I would need is to check if the drive is mapped, give a notice that the drive needs to be unmapped, then remapped to the server that houses the engineering applications. Now, for the code, what I include here is a simple new form with nothing on it but the function to map the drive when the form loads. Here is the complete code. I cut out the code below the "Windows Form Designer generated code" for brevity, denoted by "Snipp...."
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6. Snipp...
    7.      
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9. Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    10.  
    11.     Private Sub Form_Load()
    12.         WNetAddConnection("\\server1\folderA", "", "H:")
    13.         WNetAddConnection("\\server1\folderB", "", "G:")
    14.     End Sub
    15.     End Sub
    16. End Class

    Thanx for your help.

    VBNeo25

  5. #5
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: Mapping network drive

    Don't know if its a type mistake but I think your code should look like this: i.e. the Declare should be at class level instead of inside a method.
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3. Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    4.  
    5. #Region " Windows Form Designer generated code "
    6.  
    7. Snipp...
    8.      
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.  
    11.    
    12.         WNetAddConnection("\\server1\folderA", "", "H:")
    13.         WNetAddConnection("\\server1\folderB", "", "G:")
    14.    
    15.     End Sub
    16. End Class

    Why do you have Form_Load within Form1_Load ?
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2004
    Posts
    10

    Re: Mapping network drive

    Thanx for your suggestion, I entered the code as you suggested and the errors disappeared, however, the drive is not getting mapped. I also tried to put the statement in a button, but even after clicking the button the drive doesn't show up under My Computer. Here's how I did it.
    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    4.  
    5. #Region " Windows Form Designer generated code "
    6.  
    7.  Snipp.....
    8.    
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         WNetAddConnection("\\server1\folderA", "", "H:")
    15.     End Sub
    16. End Class

    VBNeo25

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mapping network drive

    WNetAddConnection is a function that returns an error code. You should test that return value to see what is happening. Here is the MSDN topic for that function and it lists the error codes. Unfortunately, like most MSDN Win32 topics, it gives the names of the constants but not the numerical values. In this situation I usually do a Google search for the constant names and have always been able to find a site somewhere that gives their numerical values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width