|
-
Apr 30th, 2001, 02:48 PM
#1
Thread Starter
Member
Error trapping.. stupid question
when i open a file for output or input whats the function to make sure there were no problems opening it?
like
open "C:\Jayiscool.txt" for ouput as #1
?
i know its dumb but i cant seem to find an answer..
thanks in advance for any help
~Jay
-
Apr 30th, 2001, 02:55 PM
#2
Hyperactive Member
I believe that if there is a problem opening the file, VB will give you an error. I don't know the #, but it should say "File Already Open" or something similar...
-
Apr 30th, 2001, 03:01 PM
#3
I would use 'On Error' like this
Code:
private sub yoursub()
On Error GoTo ErrorHandler
open "C:\Jayiscool.txt" for ouput as #1 'If there is an error opening the file, it will go the the ErrorHandler
On Error Resume Next 'No error opening file, continue working as usual
'do stuff here
exit sub
ErrorHandler:
msgbox "There was a problem opening the file"
end sub
-
Apr 30th, 2001, 03:06 PM
#4
Addicted Member
I wrote this routine to get a list of servers from a text file for a project quite some time ago. I believe the error routine might be what you are looking for.
Code:
Public Sub GetFile()
Dim intCount
Dim strFilestream
Dim strSource, strDestination, MyPath, strINIFile, AppLocation
Dim strDATE, strDateStamp, errExplanation
Dim msg
Public colHolidays As New Collection ' Creating a collection for the Holiday dates.
On Error GoTo ErrorControl
AppLocation = App.Path
strINIFile = "\Chronos.cfg"
MyPath = AppLocation
MyPath = MyPath + strINIFile
Open MyPath For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Input #1, strFilestream ' Read line into variable.
If IsNumeric(strFilestream) Then
colHolidays.Add strFilestream 'Add a date into the Holiday collection
End If
Loop
Close #1 ' Close file.
Exit Sub
ErrorControl:
If Err.Number <> 0 Then ' Quick error description
msg = "Error #" & Str(Err.Number) & " was generated by " _
& Err.Source & vbCr & vbLf & Err.Description
End If
'Write Error to Error File.
Open AppLocation & "\error.log" For Append As #2 ' Open file.
Write #2, strDateStamp ' write date stamp to error log
Write #2, msg ' write complete error message to log.
Write #2, ' write blank line.
Close #2
' Error message being more descriptive.
If Err.Number = 75 Then
errExplanation = "Path or Filename may not exist."
End If
MsgBox msg & Chr(13) & errExplanation, vbCritical, "Program Error"
End Sub
You can get a list of error codes and their descriptions from the MSDN.
HTH
Stephen Boston
Onward!
 VB6 Pro SP4, VBScript
A+ Certified Techncian.
No matter where you go, there you are.
-
Apr 30th, 2001, 03:07 PM
#5
Code:
On Error Goto ERRH
'// code here
Exit Sub
ERRH:
MsgBox "Error opening file"
-
May 3rd, 2001, 12:52 PM
#6
Thread Starter
Member
Okay that code stephenb... was perfect!
thank you all..
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
|