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
Printable View
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
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...
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
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.
You can get a list of error codes and their descriptions from the MSDN.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
HTH
Code:On Error Goto ERRH
'// code here
Exit Sub
ERRH:
MsgBox "Error opening file"
Okay that code stephenb... was perfect!
thank you all..