An error shows if trying to copy a file to my A:drive if there is no disk present. can someone tell me how to code the error so that it doesnt shut my application down.
i am using the FileCopy function in vb5.
Printable View
An error shows if trying to copy a file to my A:drive if there is no disk present. can someone tell me how to code the error so that it doesnt shut my application down.
i am using the FileCopy function in vb5.
This is one way to do it:
in the sub that tries to do the filecopy do this:
This is off the top of my head - not from any code i have in front of me.
on error goto MySub_Err 'MySub_Err can be anything
...
...
MySub_Err:
if err.number = <insert error number for disk not being present> then
msgbox "insert a disk dude"
end if
this represents a basic way to catch an error in VB and so it doesnt crash. You don't really need the if statement but if you do not put it in it will execute that code on any error <not just when the disk is not present>. To find out what the error number is - go thru your code in debug mode and when the err is thrown - in the immediate window type:
?err.number
this will tell you the number...
hth
[Edited by funkyd77 on 06-16-2000 at 01:18 AM]
Take a look at the search option for the forumns to look up "error handling". ;) The error you get above has the error number as 71. You might run into other problems you might want to test for on this :Code:Private Sub Command1_Click()
On Error Goto Yer_Aving_A_Laugh
FileCopy "C:\AFile.exe", "A:\AFile.exe"
Yer_Aving_A_Laugh :
If Err.Number = 71 then
MsgBox "Disk not ready error shown - place a blloming disk in then !!!"
End Sub
End If
End Sub
- Is the disk write protected ?
- Is the disk full
For the moment, use the above with :
For your testing. Once you're happy that you've covered every angle (and you always miss some :rolleyes: )Code:Yer_Aving_A_Laugh :
Msgbox "Error number is : " & err.number & VBCrlf & _
"Description : " & err.description
'This one will return a messagebox of the error number & description,
'rather than crash your app, it just exits the SUB (procedure). ;)
You put all the errrors into one big select case statement so your app can't possibly crash....[can it?]
Code:Yer_Aving_A_Laugh :
Select Case err.number
'check the number of the error...
Case 0
'Do something to encounter it...
Case 1
'Do something else...
Case 2
'Do something else...
End Select