Results 1 to 13 of 13

Thread: [RESOLVED] Error Handling when mapping a drive -easy

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    Resolved [RESOLVED] Error Handling when mapping a drive -easy

    Hi,

    I'm looking at error handling for a drive mapping i'm doing. All i need is a two errors to be resolved...

    If a drive doesn't map due to an Access denied failure or because the drive's already mapped?

    Here's what i've got...

    Private Sub cmdConnect_Click()
    Dim objNetwork
    Set objNetwork = CreateObject("WScript.Network")
    strLocalDrive = "Z:"
    strRemoteShare = "\\servername\share"
    strUser = TxtUsername
    strpassword = txtPassword
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False, strUser, strpassword

    End Sub

    All ideas welcome, i want to learn how to do this...

    Thanks In Advance

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Error Handling when mapping a drive -easy

    How about creating very general error handler that looks similar to this?
    Code:
    Private Sub Command1_Click()
    
    On Error GoTo ErrHandler
    
        'MAIN CODE GOES HERE...
        
        Exit Sub
    
    ErrHandler:
    
        MsgBox Err.Description
        Err.Clear
        
        'now you'll have to decide what to do and your choices are:
        
        'resume
        'resume next
        'exit sub
        
        'so uncomment line that works for you best
    
    End Sub
    You can also check err.number directly in the error handler and proceed accordingly.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    Re: Error Handling when mapping a drive -easy

    On Error GoTo ErrHandler

    Private Sub cmdConnect_Click()
    Dim objNetwork
    Set objNetwork = CreateObject("WScript.Network")
    strLocalDrive = "Z:"
    strRemoteShare = "\\servername\share"
    strUser = TxtUsername
    strpassword = txtPassword
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False, strUser, strpassword

    Exit Sub

    ErrHandler:

    MsgBox Err.Description
    Err.Clear

    resume next

    End Sub

    Work's great but has little functionality, what i'm looking at doing is stating whether the drive's been mapped or access is denied with a msgbox to the user?

    How can this be done?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Error Handling when mapping a drive -easy

    Odds are that if you got to the Exit Sub then no errors, so you can place a "success" messagebox there.

    If an error does occur, the Err.Description should provide what you are asking for? If not, take note of the Err.Number and then you can test for that error number and provide your own description in a messagbox vs the standard Err.Description. Understand? You may want to post which Err.Description are being displayed. For testing purposes, recommend changing your msgbox a little, so you can see both the description & number.
    Code:
    MsgBox Err.Description, vbExclamation + vbOkOnly, "Err: " & Err.Number
    Also, Resume Next in this case is probably not a wise choice. If the routine fails due to an error, you probably don't want to continue as if nothing happened, rather you may just want to exit the routine.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    Re: Error Handling when mapping a drive -easy

    That makes sense, and i've been able to change the context so that the error that appears is to re-login but i'm looking at showing one of two messages....

    If i receive error A, i want to show MsgBox A
    If i receive error B, i want to show MsgBox B

    Can that be done?

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Error Handling when mapping a drive -easy

    Of course it can be done - like I said you can check error number:
    Code:
    Private Sub Command1_Click()
    Dim errorMessage As String
    
    On Error GoTo ErrHandler
    
        'MAIN CODE GOES HERE...
        
        Exit Sub
    
    ErrHandler:
    
        ''MsgBox Err.Description
        
        'PLEASE NOTE THAT 1 AND 2 ARE NOT ACTUAL ERROR NUMBERS
        'DETERMINE WHICH ONES YOU'RE LOOKING FOR AND REPLACE THEM
        
        Select Case Err.Number
            Case 1
                errorMessage = "Do this..."
            Case 2
                errorMessage = "Do that..."
        End Select
        
        MsgBox errorMessage, vbExclamation + vbOKOnly, "Error: " & Err.Number
        
        Err.Clear
        Exit Sub
    
    End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    Re: Error Handling when mapping a drive -easy

    That work perfectly thank you so much!

    The only issue i'm having now is that the app isn't closing when the shell executes.....can an If function be used on this? e.g. If Shell executes Then Goto Exit or Unload?

    Private Sub cmdConnect_Click()
    Dim errorMessage As String

    On Error GoTo ErrHandler
    Dim objNetwork
    Set objNetwork = CreateObject("WScript.Network")
    strLocalDrive = "Z:"
    strRemoteShare = "\\servername\share"
    strUser = txtusername
    strpassword = txtpassword
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False, strUser, strpassword
    Shell "explorer.exe http://www.vbforums.com"
    Exit Sub


    ErrHandler:

    Select Case Err.Number
    Case -2147024891
    errorMessage = "Password Incorrect, Please Retype"
    Case -2147024811
    errorMessage = "Connection Made, please proceed to site"
    End Select

    MsgBox errorMessage, vbExclamation + vbOKOnly, "Login"

    Err.Clear
    If Err.Number = -2147024811 Then GoTo Unload

    Unload:
    Dim frm As Form
    For Each frm In Forms
    If frm.hWnd <> Me.hWnd Then
    Unload frm
    Set frm = Nothing
    End If
    Next frm
    Unload Me
    End Sub

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Error Handling when mapping a drive -easy

    Maybe
    Code:
    Shell "explorer.exe http://www.vbforums.com"
    Unload Me
    Exit Sub
    
    Select Case Err.Number
    Case -2147024891
        errorMessage = "Password Incorrect, Please Retype"
        MsgBox errorMessage, vbExclamation + vbOKOnly, "Login"
        Err.Clear
    Case -2147024811
        errorMessage = "Connection Made, please proceed to site"
        MsgBox errorMessage, vbExclamation + vbOKOnly, "Login"
        Err.Clear
        Unload Me
    Case Else
        MsgBox Err.Description, vbExclamation + vbOKOnly, "Login"
        Err.Clear
    End Select
    
    End Sub
    You are getting an error when the connection is successful?
    Last edited by LaVolpe; Mar 1st, 2009 at 11:28 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    Re: Error Handling when mapping a drive -easy

    That works fine, so simple too!

    Everything works as it should except the app closes when errorMessage = password incorrect is typed in, i need to keep the app open if i receive this error.

    Can you see where i'm going wrong?

    Private Sub cmdConnect_Click()
    Dim errorMessage As String

    On Error GoTo ErrHandler
    Dim objNetwork
    Set objNetwork = CreateObject("WScript.Network")
    strLocalDrive = "Z:"
    strRemoteShare = "\\servername\share"
    strUser = txtusername
    strpassword = txtpassword
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False, strUser, strpassword
    Shell "explorer.exe http://www.vbforums.com"
    Unload Me
    Exit Sub

    ErrHandler:

    Select Case Err.Number
    Case -2147024891
    errorMessage = "Password Incorrect, Please Retype"
    Case -2147024811
    errorMessage = "Connection Made, please proceed to site"
    End Select

    MsgBox errorMessage, vbExclamation + vbOKOnly, "Login"

    Err.Clear
    If Err.Number = -2147024811 Then GoTo Unload

    Unload:
    Dim frm As Form
    For Each frm In Forms
    If frm.hWnd <> Me.hWnd Then
    Unload frm
    Set frm = Nothing
    End If
    Next frm
    Unload Me
    End Sub

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Error Handling when mapping a drive -easy

    Because you didn't follow/use my example in the error trap. As is, your current error trap will always fall thru to the Unload: part of your code.

    P.S. You should not place your unload code in other routines other than the form's unload event. Recommend moving the following to Form_Unload
    Code:
    Dim frm As Form
    For Each frm In Forms
        If frm.hWnd <> Me.hWnd Then
            Unload frm
            Set frm = Nothing
        End If
    Next frm
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    Re: Error Handling when mapping a drive -easy

    All work's perfectly now!

    Thanks everyone for there help, i've only been with this 2 days and have learnt a fortune!

    If anyone's got any tutorials for noobs i'm really up for learning them

    Thanks again!

  12. #12

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Error Handling when mapping a drive -easy

    For Tutorials etc, check out our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page).

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