|
-
Feb 28th, 2009, 08:01 PM
#1
Thread Starter
Junior Member
[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
-
Feb 28th, 2009, 08:17 PM
#2
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.
-
Feb 28th, 2009, 09:14 PM
#3
Thread Starter
Junior Member
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?
-
Feb 28th, 2009, 10:48 PM
#4
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.
-
Mar 1st, 2009, 09:42 AM
#5
Thread Starter
Junior Member
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?
-
Mar 1st, 2009, 10:03 AM
#6
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
-
Mar 1st, 2009, 11:15 AM
#7
Thread Starter
Junior Member
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
-
Mar 1st, 2009, 11:24 AM
#8
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.
-
Mar 1st, 2009, 11:44 AM
#9
Thread Starter
Junior Member
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
-
Mar 1st, 2009, 11:47 AM
#10
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
-
Mar 1st, 2009, 12:21 PM
#11
Thread Starter
Junior Member
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!
-
Mar 1st, 2009, 12:31 PM
#12
Re: Error Handling when mapping a drive -easy
It's always appreciated when OP resolves his/her thread from the Thread Tools menu.
-
Mar 1st, 2009, 04:06 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|